简体   繁体   中英

My program is throwing a null pointer exception and I cannot figure out why. Anybody got a pair of fresh eyes for me?

Hello people of the internet. I am writing a program in Java for my intro to cs class that creates a gui with 12 buttons and a text field. When the user presses one of the buttons, the corresponding character is displayed in the text field. Basically, I have created a method that uses an array of 12 button objects to populate the button panel with those buttons. This method is used in another class where the array is created and filled with the button objects. When I run the program, I get this error:

Exception in thread "main" java.lang.NullPointerException
    at TextButtonsHWPanel.<init>(TextButtonsHWPanel.java:26)
    at TextButtonsHW.<init>(TextButtonsHW.java:56)
    at TextButtonsHW.main(TextButtonsHW.java:77)

Here are my two classes:

public class TextButtonsHWPanel extends JPanel {
    private JPanel buttonPanel;
    private JScrollPane scrollPane;

/**
 * Constructor
 * @param buttons array of JButtons to appear in a grid for text input
 * @param textArea JTextArea for display of text in response to pressed buttons
 */
public TextButtonsHWPanel(JButton[] buttons, JTextArea textArea) {
    //TODO: Create a sub-panel with a 4 row, 3 column GridLayout
    buttonPanel.setLayout(new GridLayout(4,3));
    //TODO: Populate the grid with buttons
    for (int i = 0; i < 12; i++) {
        buttonPanel.add(buttons[i]);
    }
    //TODO: Add the grid panel to this panel
    this.add(buttonPanel);
    //TODO: Create a JScrollPane containing textArea
    scrollPane = new JScrollPane(textArea);
    //TODO: Set the preferred size of the scroll pane to 80x120
    scrollPane.setPreferredSize(new Dimension(80, 120));
    //TODO: Add the scroll pane to this panel
    this.add(scrollPane);
}



public class TextButtonsHW extends JFrame implements ActionListener {
    private JButton[] buttons; //Do not change
    private JTextArea textArea; //Do not change
    private String[] buttonNames = {"a", "b", "c", "1", "2", "3", "x", "y", "z", "Enter", "Space", "Clear"};

//Assign values for these constants in the constructor
private final int ENTER;    //Index of Enter button in buttons
private final int SPACE;    //Index of Space button in buttons
private final int CLEAR;    //Index of Clear button in buttons

/**
 * Set up this frame and its contents.
 * @param title Title to appear in JFrame title bar
 */
public TextButtonsHW(String title) {
    super(title); //call parent JFrame constructor to set the title

    //TODO: instantiate all JButtons, add them to the buttons array,
    //  and register "this" as the ActionListener for each button.
    buttons = new JButton[12];
    for (int j = 0; j < buttons.length; j++) {
        buttons[j] = new JButton(buttonNames[j]);
        buttons[j].addActionListener(this);
    }

    //TODO: assign values to ENTER, SPACE, and CLEAR constants to
    //  indicate the indexes of those buttons in the buttons array
    ENTER = 9;
    SPACE = 10;
    CLEAR = 11;

    //TODO: create the JTextArea textArea
    textArea = new JTextArea();

    //TODO: set its "editable" property to false
    textArea.setEditable(false);

    //Create a TextButtonsHWPanel to display the buttons and textArea
    TextButtonsHWPanel mainPanel = new TextButtonsHWPanel(buttons, textArea);

    this.getContentPane().add(mainPanel);
    this.pack();
}

/* (non-Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
@Override
public void actionPerformed(ActionEvent e) {
    //TODO: update the text of textArea according to which
    //  button generated the ActionEvent.

}

/**
 * Create this JFrame 
 * @param args not used
 */
public static void main(String[] args) {
    final TextButtonsHW f = new TextButtonsHW("Text Buttons");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null); //centers frame on screen
    f.setVisible(true);
}

}

And at this point I feel like I should be able to run the program and display a bunch of buttons that do nothing and a blank text field. Can anyone see where I've gone wrong?

edit: resolved

Yes, look here:

private JPanel buttonPanel;

That will leave buttonPanel with its default value of null .

Now the first line of the constructor:

buttonPanel.setLayout(new GridLayout(4,3));

... you're dereferencing buttonPanel , which is still null. Bang. You need to assign a non-null reference to buttonPanel , eg

private JPanel buttonPanel = new JPanel();

You seem to be assigning all the other variables values - although doing so in the constructor body, which is also fine - it's only this one which is missing.

The stack trace should have shown you which line was throwing the exception though - that should have given you enough information to fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM