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)

解决方案 »

  1.   

    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()
      {
      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都没有实例化,就往上面放东西当然会报空指针异常了。
      

  2.   

    你那些JPanel jp1,jp2,jp3;都没实例化啊.加上,Test1()方法开始处加上
    jp1=new JPanel();jp2=new JPanel();
    jp3=new JPanel();
      

  3.   

    然后你的JPanel也没setVisible(true);
    正确的代表帮人改好了,如下:
    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);
    }
      

  4.   

    jp1,jp2,jp3使用时没有实例化,造成错误,楼上正解。。Java除了基本数据类型外,凡是对象在使用前一定要先实例化,一定要先new一个,这是初学常犯的错误,我也犯过,这也是Java语言的特点。。
      

  5.   

    空指针异常属于最普遍出现的异常,应该自己能揪出来
    空指针异常说明你的引用是null;根据出错信息很容易定位到具体的那一行。一个程序员应该培养独立解决问题的能力,自学的能力。
    楼主加油。
      

  6.   

    jp1,jp2,jp3先实例化再使用啊,这种错误不该犯。
      

  7.   

    jp1,jp2,jp3使用时没有实例化
      

  8.   

    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()
      {
      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);
      }
    }