2

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.

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Scott Hetrick
  • 161
  • 1
  • 10
  • 1
    What happens here: `SButton.java:82`? – Kai Oct 02 '13 at 07:21
  • 5
    That's one of the most confusion questions I've had to read recently...It would be greatly helped by a [SSCCE](http://sscce.org/) – MadProgrammer Oct 02 '13 at 07:22
  • 2
    show your source code catalog –  Oct 02 '13 at 07:23
  • @user714965, SButton.java:82 is this: `public void fill(Graphics g) {g.fillRect(myPoint.x,myPoint.y,myLength,myHeight);}` It's supposed to take the top left point of the SButton and fill a rectangle of the length and width of the button. – Scott Hetrick Oct 02 '13 at 07:36
  • 1
    `g` is `null`. So figure out why you are passing `null` here. I would figure this out by setting an [Exception Breakpoint](http://stackoverflow.com/questions/15758519/eclipse-conditional-breakpoint-howto-check-if-exception-occurs) for a `NullPointerException`. – Kai Oct 02 '13 at 07:39
  • 2
    @MadProgrammer, there is a lot of code, it's a bit all over the place, but I'll see if I can replicate the problem in a SSCCE & I'll post that. – Scott Hetrick Oct 02 '13 at 07:40
  • @user714965, sounds good. I'll set up an Exception Breakpoint & see what happens. – Scott Hetrick Oct 02 '13 at 07:44
  • @user714965 It showed that `myPoint` is null when the exception occurrs, not at all the `Graphics` object, like I had thought! I set `myPoint` to be initialized as a new `Point` object as soon as each SButton is created, and that seems to have stopped the error message. I can't at all understand why that is read as `null` only sometimes & not all of the time, but whatever the reason, it doesn't happen any more. Thanks a lot for the help! – Scott Hetrick Oct 02 '13 at 08:42
  • 1
    Also, @MadProgrammer, the problem was fixed while I was working on making a SSCCE for it, but thanks to you I now know what an SSCCE is, so if I post another question I'll be sure to use one when I ask. Thanks! – Scott Hetrick Oct 02 '13 at 08:43
  • *"the problem was fixed while I was working on making a SSCCE for it,"* That often happens. Hopefully the difference between the SSCCE and actual code will narrow down the source of the problem. – Andrew Thompson Oct 02 '13 at 10:42

1 Answers1

1

You can:

  • Try to use breakpoints and the debug mode of eclipse. There you will see the current value of your variables at the time of the breakpoint
  • Debug it by yourself: Print out every single object that is in use. If you get null somewhere, you know where you might want to take a look at!
chvolkmann
  • 524
  • 2
  • 9