我正在玩一些我目前还不太了解的东西,因为在机器人探索未知区域(有效位置和阻挡位置的网格)上的JAVA项目。无论如何,我试图改进我们给出的基本GUI作为如何继续的示例,它已经完成了文本字段和各种unicode字符来表示地图和机器人。我尝试通过覆盖绘制方法并将彩色矩形绘制到屏幕来表示地图。当程序启动时,我会看到下面的运行时异常,但是窗口加载了(虽然由于某种原因,顶部的JButton都是隐形的,直到我将鼠标滑到它们上面)并且从那时开始运行良好。
我通过反复试验得到了这一点并且玩了我在网上找到的例子,但我想这里有一些基本的东西我没有在这里找到。
感谢你给我的任何建议。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Controller.paint(Controller.java:156)
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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Controller.paint(Controller.java:156)
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)
答案 0 :(得分:1)
我建议你先阅读Swing tutorial。它包含一个解释如何完成绘画的部分。首先,您应该重写paintComponent()方法。从上面的错误看,你的代码出现在paint()方法中。
要学习一些东西,你应该从小处开始,然后当遇到问题时,你可以发布一个显示问题的SSCCE。