日期:2014-05-20  浏览次数:20885 次

请教大牛,我写的程序编译没有错。解释时老是出现错误:Exception in thread "main" java.lang.NullPointerExcept
Java code
import java.awt.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TestDemo extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;
    private JLabel lbname, lbaddr, lbTel;
    private JTextField jfname, jfaddr, jfTel;
    private JTextArea taCollect;
    private JPanel inputpanel, collectpanel;
    private GridBagLayout inputlayout;
    private GridBagConstraints constraints;
    private Container container;
    public TestDemo()
    {
        super("个人信息");
        setSize(300,260);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        container=this.getContentPane();
        container.setLayout(new GridLayout(2,1));
    }
    //以设定的网格包布局向面板中添加组件
    private void AddComponent(JPanel panel, GridBagLayout Layout, GridBagConstraints gbc,
            Component com, int row, int column, int numRows, int numColumns, int Weightx, int Weighty )
    {
        gbc.gridx = row;
        gbc.gridy = column;
        gbc.weightx = Weightx;
        gbc.weighty = Weighty;
        gbc.gridheight = numColumns;
        gbc.gridwidth = numRows;
        Layout.setConstraints(com, gbc);
        panel.add(com);
        
    }
    @SuppressWarnings("deprecation")
    public void setLayout()
    {
        inputpanel = new JPanel();
        inputlayout = new GridBagLayout();
        constraints = new GridBagConstraints();
        inputpanel.setLayout(inputlayout);
        
        //创建姓名标签并添加到输入面板
        constraints.anchor = GridBagConstraints.CENTER;
        lbname = new JLabel("姓名");
        constraints.fill = GridBagConstraints.NONE;
        AddComponent(inputpanel, inputlayout, constraints, lbname, 0,0,1,1,20,0);
        //创建姓名文本框并添加到输入面板
        jfname = new JTextField();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        AddComponent(inputpanel, inputlayout, constraints, jfname, 1,0,1,1,80,100);
        //创建住址标签
        lbaddr = new JLabel("住址");
        constraints.fill = GridBagConstraints.NONE;
        AddComponent(inputpanel, inputlayout, constraints, lbaddr, 0,1,1,1,20,0);
        //创建住址文本框
        jfaddr = new JTextField();
        constraints.fill = GridBagConstraints.HORIZONTAL;
        AddComponent(inputpanel, inputlayout, constraints, jfaddr, 1,1,1,1,80,100);
        //创建电话标签
        lbTel = new JLabel("电话");
        constraints.fill = GridBagConstraints.NONE;
        AddComponent(inputpanel, inputlayout, constraints, lbTel, 0,2,1,1,20,0);
        //创建电话文本框
        jfaddr = new JTextField();
        jfaddr.addActionListener(this);
        constraints.fill = GridBagConstraints.HORIZONTAL;
        AddComponent(inputpanel, inputlayout, constraints, jfTel, 1,2,1,1,80,100);
        //添加汇总信息面板
        collectpanel = new JPanel();
        collectpanel.setLayout(new BorderLayout());
        taCollect = new JTextArea("汇总列表");
        taCollect.enable(false);//直接对汇总信息编辑 
        collectpanel.add(taCollect);
        //将输入面板和汇总信息面板添加到框架中
        container.add(inputpanel);
        container.add(collectpanel);
        }
        @SuppressWarnings("deprecation")
        public static void main(String[] args)
        {
            TestDemo frame = new TestDemo();
            frame.setLayout();
            frame.show();
            
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() instanceof JTextField)
            {
                //想文本框中添加信息
                taCollect.append("\n");
                taCollect.append(jfname.getText()+"\n");
                taCollect.append(jfaddr.getText()+"\n");
                taCollect.append(jfTel.getText()+"\n");
                
            }
            
        }

}