Exception in thread "main"
java.lang.NullPointerExceptionpackage yan1;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
//vist BBS java
public class Customer extends JFrame {
// variables of labels
JLabel Heading;
JLabel labelCustNo;
JLabel labelCustName;
JLabel labelCustSex;
JLabel labelCustAge;
// variables for data entry controls
JTextField textCustNo;
JTextField textCustName;
JComboBox comboCustSex;
JTextField textCustAge;
Customer() {
super( "Customer ");
Container con;
con = this.getContentPane();
con.setLayout(new FlowLayout());
// Initializing textfield
textCustNo = new JTextField(20);
textCustName = new JTextField(25);
textCustAge = new JTextField();
String Sex[] = { "Male ", "Female " };
comboCustSex = new JComboBox(Sex);
// Adding controls for Customer Number
con.add(labelCustNo);
con.add(textCustNo);
// Adding controls for Customer Name
con.add(labelCustName);
con.add(textCustName);
// Adding controls for Customer Sex
con.add(labelCustSex);
con.add(comboCustSex);
// Adding controls for Customer Age
con.add(labelCustAge);
con.add(textCustAge);
// close the program when close button is clicked
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 250);
show();
}
public static void main(String[] args) {
Customer cust = new Customer();
}
}
错误提示为:
Exception in thread "main " java.lang.
NullPointerException at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at yan1.Customer. <init> (Customer.java:42)
at yan1.Customer.main(Customer.java:61)
请哪一位高手帮忙看啊~我调试了好久,小弟现在在学java开发环境是eclipse+myEclipse,jdk1.4
------解决方案--------------------try 一下!!1
------解决方案--------------------看不懂 。 不过看异常的类型 我觉得可能是你用了一个没有指向任何对象的引用吧。。
------解决方案--------------------labelXXX都没有初始化,也要labelXXX = new JLabel( "xxx ")的
------解决方案--------------------con.add(labelCustNo);这里没有new一下的都会
空指针