2014-04-26 100 views
0

我想完成一個學校的項目,涉及到10個jlabel,10個jtexts和10個jbuttons到一個jpanel和他們的輸出到另一個jpanel。問題是我不知道如何使用jpanels,我無法弄清楚我遇到的這個問題。我試圖在jtexts和的JLabel添加到JPanel的,但我不斷收到此錯誤:不能添加任何東西到我的JPanel

Exception in thread "main" java.lang.NullPointerException 
     at CP235_A8_Application_Fitzgerald.create_interface(CP235_A8_Application_Fitzgerald.java:89) 
     at CP235_A8_Application_Fitzgerald.<init>(CP235_A8_Application_Fitzgerald.java:253) 
     at CP235_A8_Application_Fitzgerald.main(CP235_A8_Application_Fitzgerald.java:263) 

我發現的地方,這是因爲什麼我加入了一個空值,這是什麼原因造成的,所以我在將它們添加到jpanel之前給了我的每個jtext和jlabels,但那不起作用。我讀了一些關於沒有實例化jpanel的東西(但我不太明白,所以我不確定這是否適用)無論如何,我很難接受一些幫助。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class CP235_A8_Application_Fitzgerald extends JFrame 
{ 

private JTextField text1; 
private JLabel label1; 
private JTextField text2; 
private JLabel label2; 
private JTextField text3; 
private JLabel label3; 
private JTextField text4; 
private JLabel label4; 
private JTextField text5; 
private JLabel label5; 
private JTextField text6; 
private JLabel label6; 
private JTextField text7; 
private JLabel label7; 
private JTextField text8; 
private JLabel label8; 
private JTextField text9; 
private JLabel label9; 
private JTextField text10; 
private JLabel label10; 

private JButton exit; 
private JButton sort; 
private JButton clear; 
private JButton display; 

private JPanel inputpane = new JPanel(); 

private void create_interface() 
{ 
System.out.println("create_interface is running."); 

int a; 
int b; 
int c; 
int d; 
int e; 
int f; 
int g; 
int h; 
int i; 
int j; 

a = 15; 
b = 10; 
c = 18; 
d = 7; 
e = 25; 
f = 2; 
g = 14; 
h = 27; 
i = 33; 
j = 11; 

Container GUI = getContentPane(); 
GUI.setBackground(Color.red); 

System.out.println("still running"); 

GUI.add(inputpane); 
inputpane.setLayout(new BorderLayout()); 
inputpane.setBackground(Color.green); 


System.out.println("still running"); 

//inputpane.add(sort, BorderLayout.SOUTH); 
//inputpane.add(clear, BorderLayout.SOUTH); 
//inputpane.add(exit, BorderLayout.SOUTH); 
//inputpane.add(display, BorderLayout.SOUTH); 

text1.setText(String.valueOf(a)); 
inputpane.add(text1, BorderLayout.EAST); 
text2.setText(String.valueOf(b)); 
inputpane.add(text2, BorderLayout.EAST); 
text3.setText(String.valueOf(c)); 
inputpane.add(text3, BorderLayout.EAST); 
text4.setText(String.valueOf(d)); 
inputpane.add(text4, BorderLayout.EAST); 
text5.setText(String.valueOf(e)); 
inputpane.add(text5, BorderLayout.EAST); 
text6.setText(String.valueOf(f)); 
inputpane.add(text6, BorderLayout.EAST); 
text7.setText(String.valueOf(g)); 
inputpane.add(text7, BorderLayout.EAST); 
text8.setText(String.valueOf(h)); 
inputpane.add(text8, BorderLayout.EAST); 
text9.setText(String.valueOf(i)); 
inputpane.add(text9, BorderLayout.EAST); 
text10.setText(String.valueOf(j)); 
inputpane.add(text10, BorderLayout.EAST); 
label1.setText("Integer 1"); 
inputpane.add(label1, BorderLayout.WEST); 
label2.setText("Integer 2"); 
inputpane.add(label2, BorderLayout.WEST); 
label3.setText("Integer 3"); 
inputpane.add(label3, BorderLayout.WEST); 
label4.setText("Integer 4"); 
inputpane.add(label4, BorderLayout.WEST); 
label5.setText("Integer 5"); 
inputpane.add(label5, BorderLayout.WEST); 
label6.setText("Integer 6"); 
inputpane.add(label6, BorderLayout.WEST); 
label7.setText("Integer 7"); 
inputpane.add(label7, BorderLayout.WEST); 
label8.setText("Integer 8"); 
inputpane.add(label8, BorderLayout.WEST); 
label9.setText("Integer 9"); 
inputpane.add(label9, BorderLayout.WEST); 
label10.setText("Integer 10"); 
inputpane.add(label10, BorderLayout.WEST); 

System.out.println("still running"); 
System.out.println("still running"); 
System.out.println("still running"); 


//exit.setBounds(650, 350, 300, 100); 
//inputpane.add(exit); 
exit.setText("EXIT NOW!"); 
exit.addActionListener(new ActionListener() 
{ 
    public void actionPerformed (ActionEvent event) 
    { 
     ExitButtonEventHandler(event); 
    } 
} 
); 


    /* 
SP1.setBounds(220, 350, 300, 100); 
GUI.add(SP1); 
SP1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
area51.append("Type stuff here, pal!"); 
    */ 

setTitle("Display variables."); 
setLocation(100, 100); 
setSize(1200, 800); 
setVisible(true); 

System.out.println(a); 
System.out.println(b); 
System.out.println(c); 
System.out.println(d); 
System.out.println(e); 
System.out.println(f); 
System.out.println(g); 
System.out.println(h); 
System.out.println(i); 
System.out.println(j); 


System.out.println("create_interface is ending."); 
    } 

    private void ExitButtonEventHandler(ActionEvent event) 
    { 
System.exit(0); 
    } 

    public CP235_A8_Application_Fitzgerald() 
    { 
System.out.println("Constructor is running."); 

create_interface(); 

System.out.println("Constructor is running."); 
    } 


    public static void main(String[] args) 
    { 
System.out.println("Hello from Assignment 2."); 

CP235_A8_Application_Fitzgerald Peter = new CP235_A8_Application_Fitzgerald(); 

System.out.println("Goodbye from Assignment 2."); 

    } 

    /* 
    private void DisplayListJButtonActionPerformed(ActionEvent event) 
    { 
int count = 0 

RefillAL(); 

SortedListJTextArea.setText(""); 
SortedListJTextArea.append("Sorted Integer List\n"); 

for (IntegersArrayList) 

if (count == 0) 
{ 
    SortedListJTextArea.append("No Integers to sort\n"); 
} 

    } 


    RefillAL() 
    { 
Integer i; 
String str; 

IntegersArrayList.clear(); 

str = text1.getText(); 
if (! str.equals("")) 
{ 
    try 
    { 
     i = Integer.parseInt(str); 
     IntegersArrayList.add(i); 
    } 
    catch (NumberFormatException exception) 
    { 

    } 
} 
*/ 
    } 

P.S.您可以看到很多代碼已被註釋掉並且不完整,現在我並不太在意,現在我專注於JPanel不工作(除非它當然是其中的一部分)原因jpanel不工作)。預先感謝誰幫助我。

+0

您需要了解如何調試NPE(NullPointerException)的一般概念。你應該仔細檢查引發它的行,找出哪個變量爲空,然後追溯到你的代碼,看看爲什麼。你會一次又一次遇到這些,相信我。 –

+0

哪條線是#89? – csmckelvey

回答

1

你正在聲明一個組件的混亂,但在使用它們之前沒有初始化任何組件。您必須初始化變量,在嘗試使用變量之前創建WhatNot whatnot = new WhatNot()。否則你將使用空變量。

或者更具體地說,對於您的情況,您必須在使用它們之前將有效的JTextField對象分配給text1以及所有其他文本和標籤組件。

例如,更改:

private JTextField text1; 

// 10 just represents whatever column size you prefer 
private JTextField text1 = new JTextField(10); 
// ... 
// the same with all the other components. 

這無關與Swing和所有做基本的Java,你會想通過介紹的Java因爲所有這些在前幾章都有很好的解釋。


根據我的評論,您需要了解如何調試NPE(NullPointerException)的一般概念。你應該仔細檢查引發它的行,找出哪個變量爲空,然後追溯到你的代碼,看看爲什麼。你會一次又一次遇到這些,相信我。

相關問題