当前位置: 代码迷 >> Eclipse >> Exception in thread "main" java.lang.NullPointerException这个异常怎么解决
  详细解决方案

Exception in thread "main" java.lang.NullPointerException这个异常怎么解决

热度:34   发布时间:2016-04-23 13:59:19.0
Exception in thread "main" java.lang.NullPointerException这个错误如何解决
import javax.swing.*;
import java.awt.*;
class FrameTest extends JFrame
{
  public FrameTest()
  {
super("客户信息界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(406,195);
 
  }
}
class PanelTest extends FrameTest
{
  JPanel panelObj;
  public PanelTest()
  {
panelObj=new JPanel();
getContentPane().add(panelObj);
  }
}
class ComponentTest extends PanelTest
{
  JLabel labelName;
  JLabel labelTelNo;
  JLabel labelSex;
  JLabel labelAge;
  JTextField textName;
  JTextField textTelNo;
  JRadioButton rButObj1,rButObj2;
  JPanel subPanelObj;
  JTextField textAge;
  JButton buttonObj;
  public ComponentTest()
  {
labelName=new JLabel("客户名称:");
labelTelNo=new JLabel("电话号码:");
labelSex=new JLabel("性别:");
labelAge=new JLabel("年龄:");
textName=new JTextField(25);
textName =new JTextField(15);
rButObj1=new JRadioButton("男",true);
rButObj2=new JRadioButton("女",false);
subPanelObj=new JPanel();
subPanelObj.add(rButObj1);
subPanelObj.add(rButObj2);
ButtonGroup buttonGroupObj=new ButtonGroup();
buttonGroupObj.add(rButObj1);
buttonGroupObj.add(rButObj2);
textAge=new JTextField(3);
buttonObj=new JButton("录入(L)");
panelObj.add(labelName);
panelObj.add(textName);
panelObj.add(labelTelNo);
panelObj.add(textTelNo);
panelObj.add(labelSex);
panelObj.add(subPanelObj);
panelObj.add(labelAge);
panelObj.add(textAge);
panelObj.add(buttonObj);
  }
}
public class CustomerFream01 
{
  public static void main(String args[])
  {
new ComponentTest();
  }
}
大家帮忙看一下我这个有什么错误?

------解决方案--------------------
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:365)
at testJava.ComponentTest.<init>(CustomerFream01.java:57)
at testJava.CustomerFream01.main(CustomerFream01.java:68)

看错误提示呀,
panelObj.add(textTelNo);

textTelNo, 这个属性还没有初使化,
  相关解决方案