I've been programming in Java for a year or so now at an introductory level, and this is my first time working with Swing
or any Java GUI. I'm not looking to become an expert with these kinds of GUIs, just trying to create a simple window of buttons with 2 grids that people can use to select a note and a quality for a chord (The program I'm working on rearranges jazz chords.) The segment causing the problem is one where I'm trying to fill an ArrayList<String>
with different strings depending on which buttons are pressed. Here is the code in question, which is just a test of the 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");
}
}
}
And here's the error text:
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)
I must be doing something very wrong, as I only learned/copied code segments enough to fulfill my purpose (or so I thought.) I've done a lot of research, none of which answered my question, so could anyone offer advice on this matter, whether it's a better way to create an array of String
s using the ActionListener
or just a different solution to this problem? I would be happy to provide other code segments of the project I'm working with if they're needed.
Thanks!