Solved

Null Pointer Exception

Posted on 2000-04-06
9
831 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
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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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