线程“AWT-EventQueue-0”中的异常 - 尝试填充ArrayList时抛出NullPointerException

时间:2017-11-03 00:54:35

标签: java swing arraylist nullpointerexception

我已经在Java中编程了大约一年左右,现在是在入门级别,这是我第一次使用Swing或任何Java GUI。我不打算成为这些GUI的专家,只是试图创建一个简单的按钮窗口,其中包含2个网格,人们可以用它来选择音符和音质(我正在重新排列的程序)爵士和弦。)导致问题的部分是我试图用不同的字符串填充ArrayList<String>的部分,具体取决于按下哪些按钮。这是有问题的代码,它只是对GUI的测试:

package chordvoicings;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;

public class GUI extends JFrame implements ActionListener {
    private Button[] notes, qualities;
    ArrayList<String> noteArray, qualityArray;

    public GUI() {
        setLayout(new GridLayout(4, 3, 5, 5));

        notes = new Button[12];
        notes[0] = new Button("C");
        notes[1] = new Button("C#/Db");
        notes[2] = new Button("D");
        notes[3] = new Button("D#/Eb");
        notes[4] = new Button("E");
        notes[5] = new Button("F");
        notes[6] = new Button("F#/Gb");
        notes[7] = new Button("G");
        notes[8] = new Button("G#/Ab");
        notes[9] = new Button("A");
        notes[10] = new Button("A#/Bb");
        notes[11] = new Button("B");

        for (int i = 0; i <= 11; i++)
            notes[i].addActionListener(this);
        for (int i = 0; i <= 11; i++)
            add(notes[i]);

        setSize(300, 300);
        setVisible(true);

    }

    public static void main(String[] args) {
        new GUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if (command.equals("C")) {
            noteArray.add("C");
        }
        if (command.equals("C#/Db")) {
            noteArray.add("CS");
        }
        if (command.equals("D")) {
            noteArray.add("D");
        }
        if (command.equals("D#/Eb")) {
            noteArray.add("DS");
        }
        if (command.equals("E")) {
            noteArray.add("E");
        }
        if (command.equals("F")) {
            noteArray.add("F");
        }
        if (command.equals("F#/Gb")) {
            noteArray.add("FS");
        }
        if (command.equals("G")) {
            noteArray.add("G");
        }
        if (command.equals("G#/Ab")) {
            noteArray.add("GS");
        }
        if (command.equals("A")) {
            noteArray.add("A");
        }
        if (command.equals("A#/Bb")) {
            noteArray.add("AS");
        }
        if (command.equals("B")) {
            noteArray.add("B");
        }
    }
}

这是错误文本:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at chordvoicings.GUI.actionPerformed(GUI.java:52)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(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)

我必须做一些非常错误的事情,因为我只是学习/复制了足够的代码段以实现我的目的(或者我认为。)我做了很多研究,没有一个回答我的问题,所以任何人都可以提供有关此问题的建议,是否是使用String创建ActionListener数组的更好方法,还是仅针对此问题的不同解决方案?如果需要的话,我很乐意提供我正在使用的项目的其他代码段。 谢谢!

0 个答案:

没有答案