Итак, в процессе создания системы ОрВД мне нужно спросить у пользователя, хотят ли они создать новую учетную запись. Это означало, что название банкомата (расположенного вверху) должно измениться с «Логин» на «Создание учетной записи» или что-то подобное. Таким образом, на кнопке нажмите текст названия JLabel, который нужно изменить. Проблема заключается в том, что при нажатии на кнопку новой учетной записи, все, что происходит в том, что терминал всплывает окно, указывающее на NullPointerException в следующей строке:Очень странное 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