Solved

Null Pointer Exception

Posted on 2000-04-06
9
828 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now