What is this thread/paint exception about?
I'm playing with things I don't really understand at the moment for a JAVA project on a Robot exploring an unknown territory (a grid of valid positions and blocked positions). Anyway, I tried to improve the basic GUI we were given as an example of how to proceed, it had been done with a text field and various unicode characters to represent the map and the robot. I tried to do it by overriding the paint method and drawing coloured rectangles to the screen to represent the map. When the program starts I'm presented with the runtime exception below but the window loads up (although for some reason the JButtons at the top are all invisible until i roll my mouse over them) and functions fine from then on.
I've got this far by trial and error and playing with examples I've found online but I guess there's something fundamental I haven't grasped here.
Thanks for any advice you can give me here.
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 Sour开发者_JAVA技巧ce)
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)
I suggest you start by reading the Swing tutorial. It contains a section that explains how painting is done. For one thing you should be overriding the paintComponent() method. From the above error it appears your code is in the paint() method.
To learn something you should start small, then when you have problems you can post a SSCCE showing the problem.
精彩评论