Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The local variable g may not have been initialized 1

Status
Not open for further replies.

jslmvl

Vendor
Jan 26, 2008
268
GB
Hi,

I try to drawOval in the JFrame as below (I want it as simple as possible). However, it not works. Can you tell me what I am missing?

import javax.swing.*;
import java.awt.*;
public class MyFrameClass {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500,300);
frame.setVisible(true);
Graphics g;
g.drawOval(100,100,400,200); //error: The local variable g may not have been initialized.
}
}
 
You have indeed defined the variable g as being a Graphic, but not filled it with an instance (using 'new') before you try to draw the oval at it.
 
Thank TonHu.

Perhaps, I need to tell Java that I need to draw on the frame, but I don't know how.

I have just tried
Graphics g = new Graphics();
but got error: Cannot instantiate the type Graphics
 
Thanks Dian, I am reading your link which must be usefull!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top