因此,在製作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");
}
}
可能重複[有人可以解釋我爲什麼我得到在Java中這個錯誤?(http://stackoverflow.com/questions/8344989/can-someone-explain-me-why-im-讓 - 這個錯誤,在-JAVA) – cHao 2013-05-06 02:24:17