?
Solved

Null Pointer Exception

Posted on 2000-04-06
9
Medium Priority
?
843 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 100 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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