因此,在制作ATM系统的过程中,我将不得不询问用户是否希望创建新帐户。这意味着ATM(位于顶部)的标题必须从“登录”更改为“帐户创建”或某些。因此,在按钮上按下JLabel标题的文本需要更改。问题是,当我按下按钮新帐户时,所有发生的事情是弹出终端窗口,指示下一行的NullPointerException:
title.setText("Create New Account");
从我记忆中,这意味着对象“title”为null。问题是它不应该是null,我绝对相信我建立它并且我无法想到它为什么会突然为我发回这样的错误。
以下是相关代码:
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 :(得分:2)
您将title变量定义为类变量:
private JLabel userprompt, ...., title;
并作为局部变量:
JLabel title = new JLabel("LOGIN");
title.setText()方法访问null变量的类变量。变化:
//JLabel title = new JLabel("LOGIN");
title = new JLabel("LOGIN");
您需要对要作为类变量处理的所有变量执行此操作。
答案 1 :(得分:0)
即使您已声明JLabel title
作为实例变量,您仍然在构造函数的本地范围内创建一个新的JLabel标题(请记住这些是两个不同的实体):
JLabel title = new JLabel("LOGIN");
当调用事件处理程序(actionPerformed方法)时,实际上是在访问ivar。
哪一个(如果你还记得)从未被分配过。
所以你可以使用以下两种选择之一:
实例化实际的ivar标题
title = new JLabel("LOGIN");
或者将本地标题变量的引用设置为ivar标题。
JLabel title = new JLabel("LOGIN");
this.title = title;