• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanel appears to be null.  RSS feed

 
Xander Jacob
Greenhorn
Posts: 13
Java Netbeans IDE Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've a JPanel declared as a field in a JFrame:

I initialize it in a method (Well, Netbeans' GUI builder does. :P)

it works and all; the arc is drawn.
I want to draw on it dynamically by instantiating its Graphics (Don't worry, guys. I call the "public Graphics getGraphics()" method.). This has worked for me before, taking a JPanel's Graphics and drawing with it through a different method than public void paint(Graphics g);
But when I do, it comes up with a NullPointerException. WAIT! Don't go rambling on about initializing the variable because I've gotten past that NPE newbie's blockade.

Upon further investigation:
Exception in thread "main" java.lang.NullPointerException
at physics.Renderer.render(Renderer.java:30) <-- This line is "JPanel jp = Frame.pane;"
at physics.Renderer.renderall(Renderer.java:24) <--This line calls public static void render(Something s)
at physics.Updater.update(Updater.java:47) <--Renderer is just a tool used by the Updater. Sounds like a game loop, right?
at physics.Physics.main(Physics.java:31) <-- The game loop.

it says that Line 30, making jp and pointing it to pane itself is the problem.
Conclusion:
The JPanel in the JFrame is created, initialized and overrides a parent method.
The JPanel is not initialized, meaning that it is null. There is obviously a problem.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xander Jacob wrote:it says that Line 30, making jp and pointing it to pane itself is the problem.

No. that would NOT cause a NullPointerException (NPE).

There is one, and ONLY one cause for an NPE - something is null when it shouldn't be.

Conclusion:...

I hate to say, but if your premise is wrong, I'd doubt any conclusions you draw from it.

Again: there is only ONE reason for an NPE - something is null, and you are trying to use it. So you need to think about WHY that might happen.

There are a raft of ways to avoid this, but I hesitate to go into them since it wasn't the subject of the post.

Winston
 
Xander Jacob
Greenhorn
Posts: 13
Java Netbeans IDE Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Winston. A quick look at my code reveals that the Graphics is actually the null. I've been messing with the code so much that I mixed up two error reports.

Now, armed with barrelfuls of debugging tidbits (And a new way to approach this. I make g1 in Frame and just use that from my other methods.):

I have found out that
Nuttin' in Something.slist
Somethings:
x: 6
y: 7
UPDATING!!#!#$!#%$
INSTANTIATING GRAPHICS <--The code gets to this part
GRAPHICS INSTANTIATED... <--And gets through it.
Exception in thread "main" java.lang.NullPointerException
at physics.Renderer.render(Renderer.java:31)
at physics.Renderer.renderall(Renderer.java:25)
at physics.Updater.update(Updater.java:47)
at physics.Physics.main(Physics.java:31)

g1 is instantiated to the Graphics of pane. The println "TO NOTHING! MUAHAHAHA" is not shown.
but it does not actually draw to the panel.
When I use g1 from another file (Renderer), it raises the NPE.
THIS MAKES NO SENSE
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't any reason you should want to get the Graphics object of a JPanel, so your code is useful for learning about NPEs but not much else. If you want to paint on a JPanel then you should override the paintComponent(Graphics) method, which is just about what you did in your original code. (Overriding paint(Graphics) works too, but overriding paintComponent is the preferred way to do it for some technical reasons we don't have to go into now.)
 
Xander Jacob
Greenhorn
Posts: 13
Java Netbeans IDE Netscape
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know about the overriding paint(Graphics g) method, but it's not practical (For me) to have to call these method when I want to repaint. Painting on the canvas should be done dynamically, and that's what I did in Plotter.


As you can see in processCoords, I have been able to use this code:

but it's outside of the overridden paint(Graphics g).
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Xander Jacob wrote:I know about the overriding paint(Graphics g) method, but it's not practical (For me) to have to call these method when I want to repaint. Painting on the canvas should be done dynamically...


Yes, that part is correct. But it's not up to you to call the paint(Graphics) method, it's up to the Swing code to do that. The short answer is that when you want to redraw the panel, you call its repaint() method and its paint() method will be called by the Swing code. But the long answer is in the custom painting tutorial: Lesson: Performing Custom Painting.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!