[英]Running into a Null Pointer Exception need a 3rd pair of eyes to assist
[英]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.