2013-05-05 137 views
0

因此,在製作ATM系統的過程中,我將不得不詢問用戶是否希望創建新帳戶。這意味着ATM(位於頂部)的標題將不得不從「登錄」改爲「賬戶創建」等。所以在按鈕上按下JLabel標題的文本需要改變。問題是,當我按下按鈕,新的帳戶,所發生的一切是終端窗口彈出顯示在下面的行一個NullPointerException:非常奇怪的空指針異常

title.setText("Create New Account"); 

從我記得,這意味着該對象「稱號」一片空白。問題是它不應該是空的,我絕對相信我確立了它,我想不出它爲什麼突然爲我這樣的錯誤返回一個錯誤。

下面是相關的代碼:

public class AccountSystem extends JFrame implements ActionListener 
{ 
    public static Account currentuser = new Account(); //This is so that the methods know which account is currently logged in so they can perform operations on it. 
    public static int count=0; 
    public static Account acc[] = new Account[1000]; 
    public static String parts[] = new String[3]; 
    private JButton login, logout, createacc, deposit1, deposit2, withdraw1, withdraw2, transfer1, transfer2, nevermind; 
    private JPanel optionson, optionsoff, loginarea, mainarea, titlecard, depositscreen, withdrawscreen, transferscreen, newaccountscreen; 
    private JTextField username, password, transfer, depositarea, withdrawarea, retypearea; 
    private JLabel userprompt, depositprompt, withdrawpromt, balancedisp, passwordprompt, mainmessage, title; 
    private String newuser, newpass, newpassconfirm; 
    BorderLayout borderlayout; 
    GridLayout gridlayout; 
    public AccountSystem() 
    { 
     borderlayout = new BorderLayout(); 
     borderlayout.setHgap(5); 
     borderlayout.setVgap(5); 
     //Establishing our buttons here. 
     JButton login = new JButton("Login"); 
     login.addActionListener(this); 
     JButton createacc = new JButton("New Account"); 
     createacc.addActionListener(this); 
     JButton withdraw2 = new JButton("Withdraw"); 
     JButton transfer2 = new JButton("Transfer"); 
     //Establishing our panels here. 
     JPanel optionson = new JPanel(); 
     JPanel optionsoff = new JPanel(); 
     JPanel loginarea = new JPanel(); 
     JPanel titlecard = new JPanel(); 
     //Establishing our JLabel here. 
     JLabel userprompt = new JLabel("Username: "); 
     JLabel passwordprompt = new JLabel("Password: "); 
     JLabel title = new JLabel("LOGIN"); 
     //Establishing our textfields here. 
     JTextField username = new JTextField(20); 
     JTextField password = new JTextField(20); 
     JTextField transfer = new JTextField(20); 
     JTextField withdrawarea = new JTextField(20); 
     mainscreen(getContentPane()); 
     //Building the GUI here. 
     titlecard.setSize(500,50); 
     titlecard.setLocation (0,0); 
     loginarea.setSize(300,450); 
     loginarea.setLocation(0,50); 
     optionsoff.setSize(150,450); 
     optionsoff.setLocation(300,50); 
     titlecard.add(title); 
     loginarea.add(userprompt); 
     loginarea.add(username); 
     loginarea.add(passwordprompt); 
     loginarea.add(password); 
     loginarea.add(login); 
     loginarea.add(createacc); 
     getContentPane().setLayout(null); 
     getContentPane().add(titlecard); 
     getContentPane().add(loginarea); 
     getContentPane().add(optionsoff); 
    } 


public void actionPerformed (ActionEvent e) 
{ 
    if ((e.getActionCommand()).equals("Login")) 
    { 
     login(); 
    } 
    else if ((e.getActionCommand()).equals("New Account")) 
    { 
     title.setText("Create New Account"); 
    } 
} 
+0

可能重複[有人可以解釋我爲什麼我得到在Java中這個錯誤?(http://stackoverflow.com/questions/8344989/can-someone-explain-me-why-im-讓 - 這個錯誤,在-JAVA) – cHao 2013-05-06 02:24:17

回答

2

您定義標題變量爲類變量:

private JLabel userprompt, ...., title; 

,並作爲一個局部變量:

JLabel title = new JLabel("LOGIN"); 

標題。 setText()方法訪問爲空的類變量。更改:

//JLabel title = new JLabel("LOGIN");  
title = new JLabel("LOGIN"); 

您將需要爲所有想要作爲類變量對待的變量執行此操作。

0

即使你已經宣佈,將JLabel title作爲一個實例變量,你還在創建在構造函數中的本地範圍內的新JLabel的標題(請記住,這是兩個不同的實體):

JLabel title = new JLabel("LOGIN");

而且當事件處理程序(actionPerformed方法)被調用時,你實際上正在訪問伊娃。

哪個(如果您記得)從未分配過。

所以,你可以使用兩個選擇:

無論是實例實際伊娃標題

title = new JLabel("LOGIN"); 

或者設置本地標題變量來伊娃標題參考。

JLabel title = new JLabel("LOGIN"); 
this.title = title;