import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Test1 extends JFrame
{
JPanel jp1,jp2,jp3;
JLabel jl1;
JLabel jl2;
JTextField jt1;
JRadioButton jrb1, jrb2;
ButtonGroup bg;
JButton jb1,jb2;
public static void main(String[] args)
{
new Test1();
}
public Test1()
{
this.setLayout(new GridLayout(3,1));
jl1=new JLabel("姓名:");
jl2=new JLabel("性别:");
jt1=new JTextField(10);
jrb1=new JRadioButton("男");
jrb2=new JRadioButton("女");
bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jb1=new JButton("确定");
jb2=new JButton("取消");
jp1.add(jl1);
jp1.add(jt1);
jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2);
jp3.add(jb1);
jp3.add(jb2);
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setSize(300,150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Exception in thread "main" java.lang.NullPointerException
at Demo1.Test1.<init>(Test1.java:41)
at Demo1.Test1.main(Test1.java:22)
import javax.swing.*;
import java.util.*;
public class Test1 extends JFrame
{
JPanel jp1,jp2,jp3;
JLabel jl1;
JLabel jl2;
JTextField jt1;
JRadioButton jrb1, jrb2;
ButtonGroup bg;
JButton jb1,jb2;
public static void main(String[] args)
{
new Test1();
}
public Test1()
{
this.setLayout(new GridLayout(3,1));
jl1=new JLabel("姓名:");
jl2=new JLabel("性别:");
jt1=new JTextField(10);
jrb1=new JRadioButton("男");
jrb2=new JRadioButton("女");
bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jb1=new JButton("确定");
jb2=new JButton("取消");
jp1.add(jl1);
jp1.add(jt1);
jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2);
jp3.add(jb1);
jp3.add(jb2);
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setSize(300,150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Exception in thread "main" java.lang.NullPointerException
at Demo1.Test1.<init>(Test1.java:41)
at Demo1.Test1.main(Test1.java:22)
import javax.swing.*;
import java.util.*;
public class Test1 extends JFrame
{
JPanel jp1,jp2,jp3;
JLabel jl1;
JLabel jl2;
JTextField jt1;
JRadioButton jrb1, jrb2;
ButtonGroup bg;
JButton jb1,jb2;
public static void main(String[] args)
{
new Test1();
}
public Test1()
{
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel(); this.setLayout(new GridLayout(3,1));
jl1=new JLabel("姓名:");
jl2=new JLabel("性别:");
jt1=new JTextField(10);
jrb1=new JRadioButton("男");
jrb2=new JRadioButton("女");
bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jb1=new JButton("确定");
jb2=new JButton("取消");
jp1.add(jl1);
jp1.add(jt1);
jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setSize(300,150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}改成这样,就不会有错。
你jp1,jp2,jp3都没有实例化,就往上面放东西当然会报空指针异常了。
jp1=new JPanel();jp2=new JPanel();
jp3=new JPanel();
正确的代表帮人改好了,如下:
import java.awt.*;import javax.swing.*;import java.util.*;public class Test extends JFrame {
JPanel jp1, jp2, jp3; JLabel jl1; JLabel jl2; JTextField jt1; JRadioButton jrb1, jrb2; ButtonGroup bg; JButton jb1, jb2; public static void main(String[] args) {
new Test();
} public Test() {
jp1 = new JPanel(); jp2 = new JPanel();
jp3 = new JPanel(); this.setLayout(new GridLayout(3, 1));
jl1 = new JLabel("姓名:");
jl2 = new JLabel("性别:"); jt1 = new JTextField(10);
jrb1 = new JRadioButton("男");
jrb2 = new JRadioButton("女");
bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2); jb1 = new JButton("确定");
jb2 = new JButton("取消"); jp1.add(jl1);
jp1.add(jt1); jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2); jp3.add(jb1);
jp3.add(jb2); jp1.setVisible(true);
jp2.setVisible(true);
jp3.setVisible(true);
this.add(jp1);
this.add(jp2);
this.add(jp3); this.setSize(300, 150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics arg0) {
// TODO Auto-generated method stub
super.paint(arg0);
}
空指针异常说明你的引用是null;根据出错信息很容易定位到具体的那一行。一个程序员应该培养独立解决问题的能力,自学的能力。
楼主加油。
import javax.swing.*;
import java.util.*;
public class Test1 extends JFrame
{
JPanel jp1,jp2,jp3;
JLabel jl1;
JLabel jl2;
JTextField jt1;
JRadioButton jrb1, jrb2;
ButtonGroup bg;
JButton jb1,jb2;
public static void main(String[] args)
{
new Test1();
}
public Test1()
{
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel(); this.setLayout(new GridLayout(3,1));
jl1=new JLabel("姓名:");
jl2=new JLabel("性别:");
jt1=new JTextField(10);
jrb1=new JRadioButton("男");
jrb2=new JRadioButton("女");
bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jb1=new JButton("确定");
jb2=new JButton("取消");
jp1.add(jl1);
jp1.add(jt1);
jp2.add(jl2);
jp2.add(jrb1);
jp2.add(jrb2);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setSize(300,150);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}