aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFrame" Watch "JFrame" New topic
Author

JFrame

Suela Smith
Greenhorn

Joined: Apr 20, 2006
Posts: 11
Hi everyone
any idea why i got this runtime errors

code:

import java.awt.*;
import javax.swing.*;

public class Exercise extends JFrame {

public Exercise(){
super("Exercise3");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createGUI();
}

public void createGUI(){
JLabel label1 = null;
JLabel label2 = null;
JLabel label3 = null;
JLabel label4 = null;
JLabel label5 = null;

JTextField textfield1 = null;
JTextField textfield2 = null;
JTextField textfield3 = null;
JTextField textfield4 = null;
JTextField textfield5 = null;

JButton jbutton1 = null;
JButton jbutton2 = null;
JButton jbutton3 = null;
JButton jbutton4 = null;

JPanel jpanel1 = null;
JPanel jpanel2 = null;

jpanel1 = new JPanel();
jpanel2 = new JPanel();

label1 = new JLabel("First Name");
textfield1 =new JTextField("20");

label2 = new JLabel("Middle Name");
textfield1 =new JTextField("20");

label1 = new JLabel("Last Name");
textfield1 =new JTextField("20");

label1 = new JLabel("Gender");
textfield1 =new JTextField("20");

label1 = new JLabel("Age");
textfield1 =new JTextField("20");

jbutton1 = new JButton("Load");
jbutton2 = new JButton("Save");
jbutton3 = new JButton("Clear");
jbutton4 = new JButton("Next");

jpanel1.setLayout(new GridLayout(5,2,0,0));
jpanel1.add(label1);
jpanel1.add(textfield1);
jpanel1.add(label2);
jpanel1.add(textfield2);
jpanel1.add(label3);
jpanel1.add(textfield3);
jpanel1.add(label4);
jpanel1.add(textfield4);
jpanel1.add(label5);
jpanel1.add(textfield5);

jpanel2.setLayout(new GridLayout(3,3,0,0));
jpanel2.add(jbutton1);
jpanel2.add(jbutton2);
jpanel2.add(jbutton3);
jpanel2.add(jbutton4);

this.getContentPane().setLayout(new GridLayout(3,1,0,0));
this.getContentPane().add(jpanel1);
this.getContentPane().add(jpanel2);

this.setSize(400, 300);
this.setLocation(200, 200);
this.show();

}

public static void main(String[] arg) {
JFrame frame = new Exercise();
frame.pack();
frame.setVisible(true);
}
}

run time errors:

Exeption in thread "main" java.lang.NullPointerExeption
at java.awt.Container.addImpl<Uknown Source>
at java.awt.Container.add<Uknown Source>
at Exercise.createGUI<Exercise.java:60>
at Exercise.<int><Exercise.java:9>
at Exercise.main<Exercise.java:85>

thnak you in advance

Suela
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
go to this line
at Exercise.createGUI<Exercise.java:60>

ensure every object on that line has been instantiated (new ...)
whatever w = new whatever();
and it must be before line 60
 
Don't get me started about those stupid light bulbs.
 
subject: JFrame