我有一个带有JTextFields的JFrame,它从用户那里获取输入以添加到数组中。

这是它的ActionListener

class ButtonHandlerSub implements ActionListener{
        public void actionPerformed(ActionEvent ae){if(ae.getSource() == add2){
            //add new student here
            Student temp = new Student(Integer.parseInt(tId.getText()),tName.getText(),tCourse.getText(),
                                        Integer.parseInt(tYear.getText()),tGender.getText());
            sLogic.addStudentArray(temp); // this is line 168


addStud.dispose();
    }

这是声明sLogic的构造器

 public StudentGUI() throws Exception{
    ButtonHandler bh = new ButtonHandler();
    //Instantiate Main frame buttons
    add = new JButton("Add Student");
    add.addActionListener(bh);
    del = new JButton("Delete Student");
    edit = new JButton("Edit Student");
    edit.addActionListener(bh);
    save = new JButton("Save Changes");
    //Main frame buttons added to JPanel actions
    actions = new JPanel(new GridBagLayout());
    GridBagConstraints b = new GridBagConstraints();
    b.insets = new Insets(5,0,5,0);
    b.fill = GridBagConstraints.HORIZONTAL;
    actions.add(add,b);
    b.gridy = 1;
    actions.add(del,b);
    b.gridy = 2;
    actions.add(edit,b);
    b.gridy = 3;
    actions.add(save,b);

    sLogic = new StudentLogic();
    model = new  DefaultTableModel(sLogic.getStudentArray(),sLogic.getColumnLabel());
    table = new JTable(model){
        public boolean isCellEditable(int row,int col){
            return false;
        }
    };
    //add Jtable to scroller
    JScrollPane scroller = new JScrollPane(table);

    main = new JFrame("Student Database");
    main.setLayout(new GridBagLayout());
    GridBagConstraints a = new GridBagConstraints();
    a.insets = new Insets(10,5,10,5);
    main.add(scroller,a);
    a.gridx=1;
    a.weighty = 1;
    main.add(actions,a);
    main.pack();
    main.setVisible(true);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //set JButtons edit and del disabled as default
    edit.setEnabled(false);
    del.setEnabled(false);
}


这是方法addStudentArray()

public void addStudentArray(Student studArr){
        studs =  Arrays.<Student>copyOf(studs,studs.length+1);
        studs[studs.length-1]=studArr;
    }


在构造函数的类中:

public StudentLogic() throws Exception {
    output = new ObjectOutputStream(new FileOutputStream("students_updated.dat"));
    input = new ObjectInputStream(new FileInputStream("students.dat"));
    studs = (Student[]) input.readObject();
    studs2 = new Object[studs.length][5];
    for(int i = 0; i < studs.length; i++){
        studs2[i][0]=studs[i].getID()+"";
        studs2[i][1]= studs[i].getName();
        studs2[i][2]= studs[i].getCourse();
        studs2[i][3]= studs[i].getYr();
        studs2[i][4]= studs[i].getGender();
    }
    input.close();
}


我被这个异常困住了,我不知道为什么它返回npe。我很确定我已经宣布。如果需要,我可以发布更多代码。提前致谢 :)

至于控制台

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at StudentGUI$ButtonHandlerSub.actionPerformed(StudentGUI.java:168)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


第168行是sLogic.addStudentArray(temp);

最佳答案

ButtonHandlerSub对象中的sLogic可能与StudentGUI中已初始化的sLogic不同。

这是你写的:

public StudentGUI() throws Exception{
    ButtonHandler bh = new ButtonHandler();
    ...
    sLogic = new StudentLogic();  // <- this is not the bh.sLogic!
    ...

关于java - 数组中的NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11330182/

10-09 09:04