2 u013054364 u013054364 于 2013.12.25 11:09 提问

JAVA小程序开发中遇到一个问题

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Studentguanli.actionPerformed(Studentguanli.java:111)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

页面代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.util.*;

public class Studentguanli extends JPanel implements ActionListener {
Hashtable 基本信息表 = null;

JTextField 学号, 姓名, 政治面貌, 家庭住址, 出生日期,电话号码,宿舍号码;

JRadioButton 男, 女;

Student 学生 = null;

ButtonGroup group = null;

JButton 录入, 重置;

FileInputStream inOne = null;

ObjectInputStream inTwo = null;

FileOutputStream outOne = null;

ObjectOutputStream outTwo = null;

File file = null;

public Studentguanli(File file) {
    this.file = file;
    学号 = new JTextField(10);
    姓名 = new JTextField(10);
    政治面貌 = new JTextField(10);
    家庭住址 = new JTextField(10);
    出生日期 = new JTextField(10);
    电话号码 = new JTextField(10);
    宿舍号码 = new JTextField(10);
    group = new ButtonGroup();
    男 = new JRadioButton("男", true);
    女 = new JRadioButton("女", false);
    group.add(男);
    group.add(女);
    录入 = new JButton("录入");
    重置 = new JButton("重置");
    录入.addActionListener(this);
    重置.addActionListener(this);
    Box box1 = Box.createHorizontalBox();
    box1.add(new JLabel("学号:", JLabel.CENTER));
    box1.add(学号);
    Box box2 = Box.createHorizontalBox();
    box2.add(new JLabel("姓名:", JLabel.CENTER));
    box2.add(姓名);
    Box box3 = Box.createHorizontalBox();
    box3.add(new JLabel("性别:", JLabel.CENTER));
    box3.add(男);
    box3.add(女);
    Box box4 = Box.createHorizontalBox();
    box4.add(new JLabel("政治面貌:", JLabel.CENTER));
    box4.add(政治面貌);
    Box box5 = Box.createHorizontalBox();
    box5.add(new JLabel("家庭住址:", JLabel.CENTER));
    box5.add(家庭住址);
    Box box6 = Box.createHorizontalBox();
    box6.add(new JLabel("出生日期:", JLabel.CENTER));
    box6.add(出生日期);
    Box box7 = Box.createHorizontalBox();
    box7.add(new JLabel("电话号码:", JLabel.CENTER));
    box7.add(电话号码);
    Box box8 = Box.createHorizontalBox();
    box8.add(new JLabel("宿舍号码:", JLabel.CENTER));
    box8.add(宿舍号码);
    Box boxH = Box.createVerticalBox();
    boxH.add(box1);
    boxH.add(box2);
    boxH.add(box3);
    boxH.add(box4);
    boxH.add(box5);
    boxH.add(box6);
    boxH.add(box7);
    boxH.add(box8);
    boxH.add(Box.createVerticalGlue());
    JPanel pCenter = new JPanel();
    pCenter.add(boxH);
    setLayout(new BorderLayout());
    add(pCenter, BorderLayout.CENTER);
    JPanel pSouth = new JPanel();
    pSouth.add(录入);
    pSouth.add(重置);
    add(pSouth, BorderLayout.SOUTH);
    validate();
}

public void actionPerformed(ActionEvent e) {  //学生信息录入
    if (e.getSource() == 录入) {
        String number = "";
        number = 学号.getText();
        if (number.length() > 0) {
            try {
                inOne = new FileInputStream(file);
                inTwo = new ObjectInputStream(inOne);
                基本信息表 = (Hashtable) inTwo.readObject();
                inOne.close();
                inTwo.close();
            } catch (Exception ee) {
            }

            if (基本信息表.containsKey(number)) {
                String warning = "该生基本信息已存在,请到修改页面修改!";
                JOptionPane.showMessageDialog(this, warning, "警告",
                        JOptionPane.WARNING_MESSAGE);
            } else {
                String m = "基本信息将被录入!";
                int ok = JOptionPane.showConfirmDialog(this, m, "确认",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.INFORMATION_MESSAGE);
                if (ok == JOptionPane.YES_OPTION) {
                    String name = 姓名.getText();
                    String zhengzhi = 政治面貌.getText();
                    String dizhi = 家庭住址.getText();
                    String borth = 出生日期.getText();
                    String phone = 电话号码.getText();
                    String sushe = 宿舍号码.getText();
                    String sex = null;
                    if (男.isSelected()) {
                        sex = 男.getText();
                    } else {
                        sex = 女.getText();
                    }
                    学生 = new Student();
                    学生.setNumber(number);
                    学生.setName(name);
                    学生.setzhengzhi(zhengzhi);
                    学生.setdizhi(dizhi);
                    学生.setBorth(borth);
                    学生.setSex(sex);
                    学生.setphone(phone);
                    学生.setsushe(sushe);
                    try {
                        outOne = new FileOutputStream(file);
                        outTwo = new ObjectOutputStream(outOne);
                        基本信息表.put(number, 学生);
                        outTwo.writeObject(基本信息表);
                        outTwo.close();
                        outOne.close();
                        学号.setText(null);
                        姓名.setText(null);
                        政治面貌.setText(null);
                        家庭住址.setText(null);
                        出生日期.setText(null);
                        电话号码.setText(null);
                        宿舍号码.setText(null);
                    } catch (Exception ee) {
                        System.out.println(ee);
                    }
                }
            }
        } else {
            String warning = "必须要输入学号!";
            JOptionPane.showMessageDialog(this, warning, "警告",
                    JOptionPane.WARNING_MESSAGE);
        }
    }
    if (e.getSource() == 重置) {
        学号.setText(null);
        姓名.setText(null);
        政治面貌.setText(null);
        家庭住址.setText(null);
        出生日期.setText(null);
        电话号码.setText(null);
        宿舍号码.setText(null);
    }
}

2个回答

u013208438
u013208438   2013.12.25 13:19

报了空指针错误,在Studentguanli.java 第111行

csshanli
csshanli   2015.08.14 18:13

怎么改啊?改好了吗?

Csdn user default icon
上传中...
上传图片
插入图片