Solved

Null Pointer Exception

Posted on 2000-04-06
9
837 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
Industry Leaders: 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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

740 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