Wednesday, 2 October 2013

Occasional nullpointerexception when initializing nonstatic JPanels

Occasional nullpointerexception when initializing nonstatic JPanels

Usually, I get errors from me not understanding basic aspects of Java, but
no matter how much I research I can't seem to find out the problem behind
this. Basically, if I initialize a number of nonstatic JPanels, or any
class that extends JPanel (or JFrame), then I occasionally get a
java.lang.NullPointerException. Even if I don't use the panels at all &
never call the paintComponent method once, just the act of them being
initialized causes the exception, & even then it only happens
occasionally. It's strange to me because the program still paints
everything without a visible issue, & nothing I've noticed malfunctions.
Maybe one in every six or seven times this happens, & when it does, it
happens only when the program starts up, never in the middle of using it.
I feel like it has to do with me setting up a basic GUI incorrectly or
something, & if anyone needs further explanations of what the code does I
can post more. This is the first time I've posted on here so I'm not sure
what to show, but I've noticed people often include the error message, so
I've included it. In the error message it shows the classes SButton,
Table, TableGrid, DelegateManager, Mouse, mainPanel, and mainFrame. These
are classes I coded, & I feel like the NullPointerException might lie in
some Graphics object that fails to be initialized or something. It's just
a guess, really. Where I think the error lies is in that mainFrame, which
extends JFrame, when the paintComponents(Graphics g) method is called,
sends g to mainPanel, which extends JPanel & uses its
paintComponent(Graphics g) method, which then sends g to Mouse, which uses
a draw(Graphics g) method that I defined, which then sends g to TableGrid,
which uses draw(Graphics g), which sends g to Table, which also has its
own draw(Graphics g) and fill(Graphics g) methods, which extends SButton,
which also has a draw(Graphics g) and fill(Graphics g) methods. Each time
the Graphics object is passed down, the class that receives it draws
something, & then passes it down to the next class. Since the exception
includes all of those classes, as well as the methods of painting that I
defined, I think the exception might lie there. When I don't initialize
the nonstatic panels, the error does not occur at all. I've also
initialized & used about four static classes which all extend JPanel, but
they never seem to cause an error. Also, if "passing" down a Graphics
object so each object can draw itself on the screen based on attributes it
contains is a horrible way to make a program, I apologize. I couldn't
think of another way to do it. The problem might even lie in that, & I
just can't see it. Also, there could be an error arising from that to
repaint the screen I call the repaint() method of the mainFrame, even
though I feel like that shouldn't be right since a JFrame is not a JPanel.
It seems to work, but my lack of understanding could lead me to just be
using a faulty redraw method. Still, no error occurs if I just don't
initialize nonstatic objects that extend JPanel or JFrame. Anyway, aside
from the fact that I'm just really confused, here is the error code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SButton.fill(SButton.java:82)
at Table.fill(Table.java:8)
at SButton.draw(SButton.java:75)
at Table.draw(Table.java:49)
at TableGrid.draw(TableGrid.java:74)
at DelegateManager.draw(DelegateManager.java:66)
at Mouse.draw(Mouse.java:61)
at mainPanel.paintComponent(mainPanel.java:49)
at javax.swing.JComponent.paint(JComponent.java:1037)
at javax.swing.JComponent.paintChildren(JComponent.java:870)
at javax.swing.JComponent.paint(JComponent.java:1046)
at javax.swing.JComponent.paintChildren(JComponent.java:870)
at javax.swing.JComponent.paint(JComponent.java:1046)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:870)
at javax.swing.JComponent.paint(JComponent.java:1046)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
at
sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1791)
at java.awt.Window.paint(Window.java:3390)
at javax.swing.RepaintManager$3.run(RepaintManager.java:836)
at javax.swing.RepaintManager$3.run(RepaintManager.java:802)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:802)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:745)
at
javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:725)
at javax.swing.RepaintManager.access$1000(RepaintManager.java:46)
at
javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1680)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:715)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:676)
at java.awt.EventQueue$2.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:685)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
If any more information is needed, I can gladly supply it. For now, that's
all I can think of, though. If anyone can make sense of what's happening,
I'd appreciate it greatly.
Also, if I've done something incorrectly in the way I posted this
question, feel free to let me know.

No comments:

Post a Comment