请帮忙看看下面代码错在哪里,请指出出错的原因,谢谢!
编译可以通过,但运行时报NullPointerException 位置是25行,也就是代码中红色字体那行。
代码如下
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class N extends JFrame implements ItemListener,ActionListener
{
String score[][];
String colNames[]={"学号","姓名","课程名","成绩"};
String strCname,strTname;
JButton Bconfirm,Bsubmit,BsetNull,Bexit,Bmodify;
JComboBox CBcname,CBteacher;
Container con=getContentPane();
JLabel Lcname,Lteacher;
JPanel topPanel,downPanel; public void Init()
{
this.setSize(600,400);
Lcname=new JLabel("请选择课程:");
Lteacher=new JLabel("请选择教师名");
CBcname=new JComboBox();
CBteacher=new JComboBox();
Bconfirm=new JButton("确定");
Bexit=new JButton("退出");
GridBagLayout gridbag=new GridBagLayout();
GridBagConstraints gridbagcon;
topPanel.setLayout(gridbag);
gridbagcon=new GridBagConstraints();
gridbagcon.gridx=0;gridbagcon.gridy=0;
        gridbagcon.insets=new Insets(10,1,10,30);
gridbag.setConstraints(Lcname,gridbagcon);
topPanel.add(Lcname); gridbagcon.gridx=1;gridbagcon.gridy=0;
        gridbagcon.insets=new Insets(10,1,10,30);
gridbag.setConstraints(CBcname,gridbagcon);
topPanel.add(CBcname);
CBcname.addItemListener(this); gridbagcon.gridx=2;gridbagcon.gridy=0;
        gridbagcon.insets=new Insets(10,30,10,1);
gridbag.setConstraints(Lteacher,gridbagcon);
topPanel.add(Lteacher); gridbagcon.gridx=3;gridbagcon.gridy=0;
        gridbagcon.insets=new Insets(10,1,10,30);
gridbag.setConstraints(CBteacher,gridbagcon);
topPanel.add(CBteacher);
CBteacher.addItemListener(this);
con.add(topPanel,BorderLayout.CENTER);
        
downPanel.setLayout(new FlowLayout());
        downPanel.add(Bconfirm);
Bconfirm.addActionListener(this);
downPanel.add(Bexit);
Bexit.addActionListener(this);
con.add(downPanel,BorderLayout.SOUTH);
} public void itemStateChanged(ItemEvent ie)
{
} public void actionPerformed(ActionEvent ae)
{
} public static void main(String args[])
{
N mgra=new N();
mgra.Init();
mgra.pack();
mgra.setVisible(true);
}
}

解决方案 »

  1.   


    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class N extends JFrame implements ItemListener, ActionListener {
    String score[][]; String colNames[] = { "学号", "姓名", "课程名", "成绩" }; String strCname, strTname; JButton Bconfirm, Bsubmit, BsetNull, Bexit, Bmodify; JComboBox CBcname, CBteacher; Container con = getContentPane(); JLabel Lcname, Lteacher; JPanel topPanel, downPanel; public void Init() {
    this.setSize(600, 400);
    Lcname = new JLabel("请选择课程:");
    Lteacher = new JLabel("请选择教师名");
    CBcname = new JComboBox();
    CBteacher = new JComboBox();
    Bconfirm = new JButton("确定");
    Bexit = new JButton("退出");
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints gridbagcon;
    //只是定义了topPanel为JPanel类型,但并没有构造实际对象给它,所以topPanel指向null
    //new 个JPanel对象
    topPanel = new JPanel();
    topPanel.setLayout(gridbag);
    gridbagcon = new GridBagConstraints();
    gridbagcon.gridx = 0;
    gridbagcon.gridy = 0;
    gridbagcon.insets = new Insets(10, 1, 10, 30);
    gridbag.setConstraints(Lcname, gridbagcon);
    topPanel.add(Lcname); gridbagcon.gridx = 1;
    gridbagcon.gridy = 0;
    gridbagcon.insets = new Insets(10, 1, 10, 30);
    gridbag.setConstraints(CBcname, gridbagcon);
    topPanel.add(CBcname);
    CBcname.addItemListener(this); gridbagcon.gridx = 2;
    gridbagcon.gridy = 0;
    gridbagcon.insets = new Insets(10, 30, 10, 1);
    gridbag.setConstraints(Lteacher, gridbagcon);
    topPanel.add(Lteacher); gridbagcon.gridx = 3;
    gridbagcon.gridy = 0;
    gridbagcon.insets = new Insets(10, 1, 10, 30);
    gridbag.setConstraints(CBteacher, gridbagcon);
    topPanel.add(CBteacher);
    CBteacher.addItemListener(this);
    con.add(topPanel, BorderLayout.CENTER);
    //只是定义了downPanel为JPanel类型,但并没有构造实际对象给它,所以downPanel指向null
    //new 个JPanel对象
    downPanel = new JPanel();
    downPanel.setLayout(new FlowLayout());
    downPanel.add(Bconfirm);
    Bconfirm.addActionListener(this);
    downPanel.add(Bexit);
    Bexit.addActionListener(this);
    con.add(downPanel, BorderLayout.SOUTH);
    } public void itemStateChanged(ItemEvent ie) {
    } public void actionPerformed(ActionEvent ae) {
    } public static void main(String args[]) {
    N mgra = new N();
    mgra.Init();
    mgra.pack();
    mgra.setVisible(true);
    }
    }
    哥们,你只是定义了变量,但并没有构造对象。