2013-05-05 4 views
0

그래서 ATM 시스템을 만드는 과정에서 사용자에게 새 계정을 만들지 묻습니다. 즉, 상단에 위치한 ATM의 제목이 '로그인'에서 '계정 생성'으로 변경되어야합니다. 버튼 누름에서 JLabel 제목의 텍스트를 변경해야합니다. 문제는 내가 버튼 새 계정을 누를 때 발생하는 모든 터미널 창에 다음 줄에 NullPointerException이 나타내는 팝업 있다는 점이다 : 내가 기억하는 바로는매우 이상한 NullPointerException

title.setText("Create New Account"); 

을,이 의미 객체 "제목" 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

가능한 중복 [누군가가 내가 자바에서이 오류를 받고 있어요 왜 나를 설명 할 수 있습니까?] (http://stackoverflow.com/questions/8344989/can-someone-explain-me-why-im- Java에서 오류 발생) – cHao

답변

2

:

다음은 관련 코드입니다

private JLabel userprompt, ...., title; 

로컬 변수로

:

JLabel title = new JLabel("LOGIN"); 

제목입니다. setText() 메소드는 null 인 클래스 변수에 액세스합니다. 변경 :

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

클래스 변수로 처리하려는 모든 변수에 대해이 작업을 수행해야합니다. 당신이 선언에도 불구하고

0

, 인스턴스 변수로 JLabel title, 당신은 아직도 생성자의 로컬 범위의 새로운 JLabel의 제목을 작성하는 (그 두 개의 서로 다른 개체라는 것을 기억) :

JLabel title = new JLabel("LOGIN");

과 이벤트 처리기 (actionPerformed 메서드)가 호출되면 실제로 ivar에 액세스합니다.

(기억 나지 않는 한) 할당되지 않은 항목은 어느 것입니까?

을 하나의 인스턴스를 실제 바르 제목

title = new JLabel("LOGIN"); 

을 또는 바르 제목 지역 제목 변수의 참조를 설정 :

그래서 당신은 두 가지 선택 중 하나를 사용할 수있다.

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