GUI构造错误

时间:2013-11-18 00:56:12

标签: java swing nullpointerexception jbutton jlist

我仍然是Java的新手,我不确定为什么我在编译时遇到错误(之前没有错误显示)。错误是:

Exception in thread "main" java.lang.NullPointerException
at Lab10.<init>(Lab10.java:21)
at Lab10.main(Lab10.java:55)

我假设这表明数组为空?我尝试在构造函数之前移动它,并且无论我做了什么都会出错。在我实际使用用户输入作为数组之前,我只用空格初始化之前有数百个错误。我想要做的就是一个简单的GUI,如果你单击“添加课程”按钮,它将提示你输入一个课程,这将添加到JList。我很感激任何输入!谢谢!

import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Lab10 extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    int count = 0;
    String subject;
    private JList list;
    private JButton button;
    private JButton button2;
    Scanner input = new Scanner(System.in);
    String [] Courses;

    @SuppressWarnings("unchecked")
    public Lab10() {
        JPanel p1 = new JPanel();
        for (int j = 0 ; j < 100 ; j++) {
            Courses[j] = " ";
        }
        p1.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
        button = new JButton("Add Course");
        button2 = new JButton("Close");
        button.addActionListener(this);
        button2.addActionListener(this);
        add(button);
        add(button2);

        setLayout(new BorderLayout(10, 10));
        list = new JList(Courses);
        add(list, BorderLayout.CENTER);
        add(p1, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Add Course")) {
            System.out.println("Enter your new subject: ");
            subject = input.next();
            count++;
            for (int i = 0 ; i <= count ; i++) {
                if (Courses[i].equals(" ")) {
                    Courses[i] = subject;
                }
            }
        }
        else if (e.getActionCommand().equals("Close")) {
            System.exit(0);
        }
    }

    public static void main(String [] args) {
        Lab10 frame = new Lab10();
        frame.setTitle("Java");
        frame.setSize(500, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

2 个答案:

答案 0 :(得分:1)

将其添加到构造函数的顶部:

this.Courses = new String[100];

更好的是,你应该将那个100和for循环条件中的100移动到:

private static final int ARRAY_SIZE = 100;

然后将其他100个实例更改为ARRAY_SIZE

另外,请注意编译时NullPointerException没有发生;当你运行程序时就会发生这种情况。

答案 1 :(得分:0)

基本上,你已经声明了一个名为Courses的数组,但你从来没有初始化它......

String[] Courses; 

@SuppressWarnings("unchecked")
public Lab10() {
    JPanel p1 = new JPanel();
    for(int j = 0; j < 100; j++){
        Courses[j] = " ";
    }

确保在尝试使用之前将所需数量的元素分配给数组,例如......

String[] Courses; 

@SuppressWarnings("unchecked")
public Lab10() {
    JPanel p1 = new JPanel();
    Courses = new String[100];
    for(int j = 0; j < Courses.length; j++){
        Courses[j] = " ";
    }

您可能还希望仔细阅读Code Conventions for the Java Programming Language,因为它会让您为其他人编写代码以便阅读

相关问题