Solved

Null Pointer Exception

Posted on 2000-04-06
9
839 Views
Last Modified: 2010-05-18
I have created a JPanel, which just happens to be a tab, though this is irrelevant. Inside the JPanel I have a JButton and a Canvas (This happenes to be a graphing program). If I overwrite "paintComponent(Graphics g)" and include the line "Graphics canvas_g = canvas.getGraphics()" I can draw, etc...If I put this line anywhere else, such as in a "draw()" function I get a "NullPointerException". Is there a reason why I can only get the graphics object for the canvas within the "paintComponent(Graphics g)" function (which, by the way, is used to re-draw the JPanel). [oh, I can also pass the canvas_g object to another function and then use it with no problem. I just can't call the getGraphics() function from another function.]
0
Comment
Question by:streaks619
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Author Comment

by:streaks619
ID: 2691775
Oh, one more thing, the reason why I don't want to call it from the "paintComponent" function is that ideally the user would hit the button, which would then graph a specified function.

As it is now, the graph will be displayed whenever the panel is refreshed.
0
 
LVL 1

Expert Comment

by:Yawgmoth
ID: 2692152
your best bet is to note where the nullpointer is being generated and to trace it from there
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2692188
NEVER NEVER NEVER use getGraphics().

put all your rendering code inside paintComponent() and call repaint() when needed


(as you can see yourself, getGraphics is UNSAFE - it may return null Graphics object sometimes)
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Expert Comment

by:wizcool
ID: 2693026
I'm not sure, but you might have to add the line:
Graphics canvas_g= new Graphics();

before you call the 'getGraphics()'.

That is because 'getGraphics()' returns a pointer, and if there is no object 'canvas_g' created, that might cause a 'Null Pointer Exception'.
0
 

Author Comment

by:streaks619
ID: 2693027
yes, but paintComponent is called whenever the Panel is repainted, which I cannot control. I want it only to be repainted when I press the button.
0
 

Author Comment

by:streaks619
ID: 2693035
You cannot make a Graphics object using new Graphics(), graphics cannot be instantiated.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 2693097
>> yes, but paintComponent is called whenever the Panel is repainted

well you SHOULD call panel.repaint() when you want it to repaint itself ... Java VM will call your paint() / paintComponent() with the appropriate Graphics object, when the system event Thread is free ...

as I already said NEVER, NEVER, NEVER use getGraphics().

I have used that method only once in my live - to implement a workaround over TERRIBLE java.awt.PopupMenu bug ... and I wouldn't use it if there was another possibility.
0
 

Author Comment

by:streaks619
ID: 2693762
You are comfusing something. I have to use getGraphics, how else would I get the graphis object for the canvas?

Remember, paintComponent gets the graphics object for the panel, NOT  for the canvas.
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 2695701
I'm not confusing anything and my code does not throw NullPointerException. :)

once again - put all your rendering code inside paint() (for AWT components) or paintComponent() (Swing) and call repaint when needed.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question