Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组中的NullPointerException_Java_Arrays_Nullpointerexception - Fatal编程技术网

Java 数组中的NullPointerException

Java 数组中的NullPointerException,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,我有一个JFrame和JTextFields,它们从用户那里获取输入并添加到数组中 这是它的ActionListener class ButtonHandlerSub implements ActionListener{ public void actionPerformed(ActionEvent ae){if(ae.getSource() == add2){ //add new student here Student temp

我有一个JFrame和JTextFields,它们从用户那里获取输入并添加到数组中

这是它的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的contsructor

 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()方法


第168行是sLogic.addStudentArray(temp)

以下内容将暂时防止
螺柱中出现空值

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

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

这就是你写的:

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

sLogic=new StudentLogic();//您应该发布控制台输出,以便所有人都知道哪一行发生异常,哪一行抛出NPE?NPE位于第168行,即sLogic.addStudentArray(temp);这意味着sLogic是空的。也许
sLogic
是空的。你在哪里初始化它?我现在似乎得到了它。但是如果我在类中实例化它为
private StudentLogic sLogic;
它不应该在类中可访问吗?我现在看不到层次结构。ButtonHandler类是在Stude中声明的吗ntGUI?ButtonHandler是否有sLogic变量?我将if语句从ButtonHandlerSub传输到ButtonHandler,现在似乎工作正常。谢谢先生!
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)
public synchronized void addStudentArray(Student studArr){
    Student[] enlargedStuds = Arrays.copyOf(studs, studs.length + 1);
    enlargedStuds[enlargedStuds.length - 1] = studArr;
    studs = enlargedStuds;
}
public StudentGUI() throws Exception{
    ButtonHandler bh = new ButtonHandler();
    ...
    sLogic = new StudentLogic();  // <- this is not the bh.sLogic!
    ...