繁体   English   中英

我的程序抛出了空指针异常,我无法弄清原因。 有人给我新鲜的眼睛吗?

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

互联网的大家好 我正在使用Java为CS类简介编写程序,该类创建具有12个按钮和一个文本字段的gui。 当用户按下按钮之一时,相应的字符显示在文本字段中。 基本上,我创建了一个方法,该方法使用12个按钮对象组成的数组用这些按钮填充按钮面板。 此方法用在另一个创建数组并用按钮对象填充的类中。 运行程序时,出现以下错误:

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)

这是我的两节课:

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

}

在这一点上,我觉得我应该能够运行该程序并显示一堆不起作用的按钮和一个空白文本字段。 谁能看到我哪里出问题了?

编辑:解决

是的,请看这里:

private JPanel buttonPanel;

这将使buttonPanel的默认值为null

现在构造函数的第一行:

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

...您正在取消引用buttonPanel ,它仍然为null。 砰。 您需要为buttonPanel分配一个非null的引用,例如

private JPanel buttonPanel = new JPanel();

您似乎正在分配所有其他变量值-尽管这是在构造函数主体中这样做的,这也很好-仅缺少此变量。

堆栈跟踪应该已经向您显示了哪一行引发了异常-这应该已经为您提供了足够的信息来解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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