在线程中获得异常" AWT-EventQueue-0" Plot3DPanel中的java.lang.NullPointerException

时间:2014-03-12 00:06:12

标签: java swing plot awt

使用Plot3DPanel时,我不断收到以下错误

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at org.math.plot.render.Projection.screenProjection(Unknown Source)
            at org.math.plot.render.AWTDrawer.drawLine(Unknown Source)
            at org.math.plot.plotObjects.Line.plot(Unknown Source)
            at org.math.plot.plotObjects.Axis.plot(Unknown Source)
            at org.math.plot.plotObjects.BasePlot.plot(Unknown Source)
            at org.math.plot.canvas.PlotCanvas.paint(Unknown Source)
            at javax.swing.JComponent.paintToOffscreen(Unknown Source)
            at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
            at javax.swing.RepaintManager$PaintManager.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$3.run(Unknown Source)
            at javax.swing.RepaintManager$3.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
            at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
            at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
            at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
            at javax.swing.RepaintManager.access$1000(Unknown Source)
            at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
            at java.awt.event.InvocationEvent.dispatch(Unknown Source)
            at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
            at java.awt.EventQueue.access$200(Unknown Source)
            at java.awt.EventQueue$3.run(Unknown Source)
            at java.awt.EventQueue$3.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

当我在循环中调用removeAllPlots();方法并尝试连续刷新面板时,我得到了错误。但是,如果我为每次迭代使用新的JFrame,则没有错误,只有多个窗口。

这是我的循环:

if(unlocalizedPlanes.size() > 0)
            {
                buildPlaneAdjacents();
                planeLocalized(seed);
                seedSensors.addAll(seed.getSensors());
                //System.out.println("Seed plane found");

            }

            Set<Plane> ext = new TreeSet<>();
            System.out.println("Localizing planes");
            do
            {
                plot();
                ext.clear();
                for(Plane p : recentlyLocalized)
                    ext.addAll(p.findLocalizableAdjacents());
                localizedPlanes.addAll(recentlyLocalized);
                recentlyLocalized.clear();
                planesLocalized(ext);
                System.out.println(ext.size() + " planes localized");

            }
            while(ext.size() > 0);
            localizedPlanes.addAll(recentlyLocalized);

这是我的plot()功能:

public void plot()
    {

        int i=0;
        double[] xu = new double[unlocalizedSensors.size()+1];
        double[] yu = new double[unlocalizedSensors.size()+1];
        double[] zu = new double[unlocalizedSensors.size()+1];
        double[] xl = new double[localizedSensors.size()+1];
        double[] yl = new double[localizedSensors.size()+1];
        double[] zl = new double[localizedSensors.size()+1];
        double[] xs = new double[seedSensors.size()+1];
        double[] ys = new double[seedSensors.size()+1];
        double[] zs = new double[seedSensors.size()+1];



        xs[0] = 0;
        ys[0] = 0;
        zs[0] = 0;
        xl[0] = 0;
        yl[0] = 0;
        zl[0] = 0;
        xu[0] = 0;
        yu[0] = 0;
        zu[0] = 0;
        for(Sensor s : seedSensors)
        {
            xs[i] = s.getLocation().x;
            ys[i] = s.getLocation().y;
            zs[i] = s.getLocation().z;
            i++;
        }
        i = 0;
        for(Sensor s : unlocalizedSensors)
        {
            xu[i] = s.getLocation().x;
            yu[i] = s.getLocation().y;
            zu[i] = s.getLocation().z;
            i++;
        }
        i = 0;
        for(Sensor s : localizedSensors)
        {
            xl[i] = s.getLocation().x;
            yl[i] = s.getLocation().y;
            zl[i] = s.getLocation().z;
            i++;
        }

        plotXYZ.addScatterPlot("Localized", Color.GREEN, xl, yl, zl);

        plotXYZ.addScatterPlot("Unlocalized", Color.RED, xu, yu, zu);


        plotXYZ.addScatterPlot("Seed", Color.BLUE, xs, ys, zs);


        plotXYZ.setFixedBounds(0, 0, length);
        plotXYZ.setFixedBounds(1, 0, width);
        plotXYZ.setFixedBounds(2, 0, height);


        plotXYZ.removeAllPlots();

    }

2 个答案:

答案 0 :(得分:0)

我通过制作课程Runnable并使用SwingUtils.invokeLater()功能解决了这个问题。这样,重绘的同步错误就不会发生。

答案 1 :(得分:0)

这是一个很难解决的问题。我运行了execute函数,删除了throws子句,并使GradientDescent类实现了Runnable。这与以下教程有关:http://rodrigoaraujo.me/articles/2015-01/Making-Your-Machine-Think-Learn-And-Predict-Gradient-Descent-Algorithm-in-Java