user2758501
user2758501

Reputation: 71

GUI construction error

I'm still fairly new to Java, and am not sure why I am getting an error when I compile (no errors show prior to that). The error is:

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

I was assuming that is indicating that the array is null? I tried moving it before the constructor, and it came out with errors in whatever I did. There were hundreds of errors before I initialized it with just a space before I actually used the user input as the array. All I am trying to make is a simple GUI where if you click on the button "Add Course" it will prompt you to type in a course, and that will add to the JList. I would appreciate any input! Thank you!

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);
    }

}

Upvotes: 0

Views: 72

Answers (2)

musical_coder
musical_coder

Reputation: 3896

Add this to the top of your constructor:

this.Courses = new String[100];

Even better, you should actually move that 100, and the 100 in your for loop condition, to something like:

private static final int ARRAY_SIZE = 100;

Then change the other instances of 100 to ARRAY_SIZE.

Also, note that the NullPointerException isn't happening when you compile; it's happening when you run the program.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347204

Basically, you've declared an array called Courses but you've never initialised it...

String[] Courses; 

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

Make sure you allocate the required number of elements to the array before you try and use it, for example...

String[] Courses; 

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

You may also wish to take a read through Code Conventions for the Java Programming Language, as it will make you code eaiser for others to read

Upvotes: 0

Related Questions