2012-03-30 57 views
1

嘿傢伙我試圖讓一個GUI,可以導航通過JTextAreas時,推下一個或上一個按鈕。我使用CardLayout在JTextArea之間切換。我可以讓程序編譯,但每次我按下下一個或上一個按鈕時,我總是會得到一個NullPointerException。我不知道爲什麼CardLayout對象在下一個和之前的方法中爲null,並且讓我感到困惑。我得到NullPointerException使用CardLayout

錯誤是從這段代碼

cl.show(cardPanel, "" + (currentCard)); 

任何幫助表示讚賞THX的傢伙來了!

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 


public class AsciiCanvas extends JTextArea 
{ 

private JPanel jp1; 
private JPanel jp2; 
private JPanel jp3; 
private JPanel jp4; 
private JPanel jp5; 
private JPanel jp6; 
private JPanel jp7; 
private JPanel jp8; 
private JPanel jp9; 
private JPanel jp10; 
private JTextArea text1; 
private JTextArea text2; 
private JTextArea text3; 
private JTextArea text4; 
private JTextArea text5; 
private JTextArea text6; 
private JTextArea text7; 
private JTextArea text8; 
private JTextArea text9; 
private JTextArea text10; 

private JPanel cardPanel; 
private CardLayout cl; 
private int currentCard = 1; 

private JFileChooser fc; 


public AsciiCanvas() 
{ 
    Font mono = new Font("Monospaced", Font.PLAIN, 12); 

    cardPanel = new JPanel(); 
    CardLayout cl = new CardLayout(); 
    cardPanel.setLayout(cl); 


    JTextArea text1 = new JTextArea(); 
    JTextArea text2 = new JTextArea(); 
    JTextArea text3 = new JTextArea(); 
    JTextArea text4 = new JTextArea(); 
    JTextArea text5 = new JTextArea(); 
    JTextArea text6 = new JTextArea(); 
    JTextArea text7 = new JTextArea(); 
    JTextArea text8 = new JTextArea(); 
    JTextArea text9 = new JTextArea(); 
    JTextArea text10 = new JTextArea(); 



    JPanel jp1 = new JPanel(); 
    JPanel jp2 = new JPanel(); 
    JPanel jp3 = new JPanel(); 
    JPanel jp4 = new JPanel(); 
    JPanel jp5 = new JPanel(); 
    JPanel jp6 = new JPanel(); 
    JPanel jp7 = new JPanel(); 
    JPanel jp8 = new JPanel(); 
    JPanel jp9 = new JPanel(); 
    JPanel jp10 = new JPanel(); 

    jp1.add(text1); 
    jp2.add(text2); 
    jp3.add(text3); 
    jp4.add(text4); 
    jp5.add(text5); 
    jp6.add(text6); 
    jp7.add(text7); 
    jp8.add(text8); 
    jp9.add(text9); 
    jp10.add(text10); 

    cardPanel.add(jp1, "1"); 
    cardPanel.add(jp2, "2"); 
    cardPanel.add(jp3, "3"); 
    cardPanel.add(jp4, "4"); 
    cardPanel.add(jp5, "5"); 
    cardPanel.add(jp6, "6"); 
    cardPanel.add(jp7, "7"); 
    cardPanel.add(jp8, "8"); 
    cardPanel.add(jp9, "9"); 
    cardPanel.add(jp10, "10"); 

    setBorder(BorderFactory.createTitledBorder("Animat ion here")); 
    setFont(mono); 

    fc = new JFileChooser(); 
} 


public void Next() 
{ 
    if(currentCard < 10) 
    { 

    currentCard +=1; 
    cl.show(cardPanel, "" + (currentCard)); 
    } 


} 

public void Previous() 
{ 
    if(currentCard > 1) 
    { 
    currentCard -= 1; 
    cl.show(cardPanel, "" + (currentCard)); 

    } 
} 

錯誤消息* **

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at AsciiCanvas.Next(AsciiCanvas.java:108) 

回答

2

的問題是,cl爲空,這裏的原因:

private CardLayout cl; 

... 

public AsciiCanvas() 
{ 
    ... 
    CardLayout cl = new CardLayout(); 

在這種構造你已經聲明瞭一個新的局部變量,而不是分配值到實例變量。局部變量陰影隱藏實例變量。你consructor代碼應該是:

public AsciiCanvas() 
{ 
    ... 
    cl = new CardLayout(); 

(順便說一句,我強烈建議您使用的String.valueOf(...)代替"" + ...將值轉換爲字符串在一個空安全的方式你不是真的感興趣。在字符串連接,是嗎?)

+0

thx爲您的答案! – FireStorm 2012-03-30 20:46:02

1

這是在構造的局部變量,隱藏構件cl

CardLayout cl = new CardLayout(); 

意味着構件clnull在方法Next()Previous()

更改爲:

cl = new CardLayout(); 
+0

好了,錯誤消失了,非常感謝! – FireStorm 2012-03-30 20:44:41

相關問題