2

我正在尝试做一些事情来追查问题,但在paintContents 之前我无能为力,并且通过我的调试器看起来一切都很好,但我会仔细检查以确保我没有错过任何东西。至少,我想知道如何静默处理这些问题(例如捕获它们并能够输出有意义的错误消息,因为一旦它被抛出,GUI 就会卡顿并冻结一点)。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at sun.java2d.pipe.DuctusShapeRenderer.renderPath(Unknown Source)
        at sun.java2d.pipe.DuctusShapeRenderer.draw(Unknown Source)
        at sun.java2d.pipe.PixelToParallelogramConverter.draw(Unknown Source)
        at sun.java2d.pipe.PixelToParallelogramConverter.draw(Unknown Source)
        at sun.java2d.SunGraphics2D.draw(Unknown Source)
        SNIP - MY CALL TO PAINT THE LAYER
        at com.jhlabs.map.layer.Layer.paintContents(Layer.java:70)
        at com.jhlabs.map.layer.Layer.paint(Layer.java:59)
        at com.jhlabs.map.layer.Layer.paintLayers(Layer.java:76)
        at com.jhlabs.map.layer.Layer.paintContents(Layer.java:68)
        at com.jhlabs.map.layer.Layer.paint(Layer.java:59)
        at com.jhlabs.Globe.paint(Globe.java:305)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
        at javax.swing.RepaintManager.paint(Unknown Source)
        at javax.swing.JComponent._paintImmediately(Unknown Source)
        at javax.swing.JComponent.paintImmediately(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

以下源代码来自Java 地图投影库

Layer.paint 内容:

public void paintContents(MapGraphics g) {
    if (g != null) {
        paintLayers(g);
        paintFeatures(g);
        paintLayer(g);
    }
}

图层.油漆:

public void paint(MapGraphics g) {
    if (isVisible()) {
        Graphics2D g2d = g.getGraphics2D();
        AffineTransform saveTransform = g2d.getTransform();
        Composite saveComposite = g2d.getComposite();
        Projection saveProjection = g.getProjection();
        Style saveStyle = g.getStyle();
        if (composite != null)
        g2d.setComposite(composite);
        if (transform != null)
        g2d.transform(transform);
        if (style != null)
        g.setStyle(style);
        if (projection != null)
        g.setProjection(projection);
        paintContents(g);
        g.setStyle(saveStyle);
        g.setProjection(saveProjection);
        g2d.setComposite(saveComposite);
        g2d.setTransform(saveTransform);
    }
}

图层.paintLayers:

public void paintLayers(MapGraphics g) {
for (Iterator<Layer> it = getLayersIterator(); it.hasNext();) {
    Layer l = (Layer) it.next();
    l.paint(g);
}
}

地球仪.paint:

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;

// Turn on antialiasing - otherwise it looks horrible
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

// Put the origin at bottom left
g2.translate(0, getHeight());
g2.scale(1, -1);

// Put the globe in the middle
g2.translate(getWidth() / 2, getHeight() / 2);

Point2D.Float p = new Point2D.Float(1, 0);
transform.deltaTransform(p, p);
float rscale = 1.0f / (float) Math.sqrt(p.x * p.x + p.y * p.y);
g2.setStroke(new BasicStroke(rscale * 0.5f));

MapGraphics mg = MapGraphics.getGraphics(g2, new Rectangle(getSize()));
seaLayer.setVisible(showSea);
tissotLayer.setVisible(showTissot);
worldLayer.setVisible(showWorld);
graticuleLayer.setVisible(showGraticule);
map.paint(mg);

if (showNight) {
    Color c = new Color(0, 0, 0, 0.5f);
    GeneralPath gc = new GeneralPath();
    ProjectionPainter.smallCircle(45, 5, 87, 180, gc, true);
    gc.closePath();
    ProjectionPainter pp = ProjectionPainter.getProjectionPainter(projection);
    pp.drawPath(g2, gc, null, c);

}

}
4

4 回答 4

1

我在您的堆栈跟踪中看到由以下行定义的空白区域:

SNIP - MY CALL TO PAINT THE LAYER

该代码调用SunGraphics2D.draw(Shape). 我在您编辑的代码中没有看到该调用。但是,看起来Shape您传递的那个SunGraphics2D.draw()是空的。

于 2009-07-17T13:21:10.083 回答
0

这意味着在处理事件时(在这种情况下看起来像重绘)会引发 NullPointerException。这意味着您尝试调用对象上的方法,结果结果为空。

com.jhlabs.map.layer.Layer 类的 paintContents() 方法的第 70 行有一个空引用。

于 2009-07-17T13:07:53.447 回答
0

我不知道您使用的是哪个 IDE,但在 Eclipse 中,您可以告诉调试器在特定类型的异常(例如 NPE)处中断。

您可以在打开此选项的情况下在 Debugmodus 中运行您的应用程序并尝试重现错误,以便您可以检查您的调试器出了什么问题。

于 2009-07-17T13:21:07.353 回答
0

检查 sun lib 的源代码是否可用。如果是这样:指示调试器在抛出 NPE 时暂停执行。然后您可以检查为空的对象并猜测(或回溯)哪个对象应该在该引用中

于 2009-07-17T13:32:16.717 回答