0

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 Strings 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!

  • 1
    Based on the available code, `noteArray` is never initialised, neither is `qualityArray` – MadProgrammer Nov 03 '17 at 00:56
  • Lord. I guess I should've checked for obvious errors. I got so caught up in the huge list of exceptions that I never bothered to. Thank you kind sir – Hiroshiman Nov 03 '17 at 01:20
  • You’ve just about described by life – MadProgrammer Nov 03 '17 at 01:29
  • `I got so caught up in the huge list of exceptions` - this is how all Exceptions are reported. All you need to do is look at the first/second line. It will tell you the statement that caused the problem. Then you figure out why that statement is in error. – camickr Nov 03 '17 at 01:54

0 Answers0