2013-05-22 46 views
0

我似乎在創建按鈕處理程序的操作偵聽器中設置了錯誤。當我試圖獲得文本字段nameTF的值時,我得到一個空指針錯誤。我試圖模擬你的計算器代碼,並且退出按鈕處理器工作正常。我知道按下創建按鈕調用處理程序,但語句Java GUI變量問題

inName = nameTF.getText();

給出錯誤消息。

聽衆的完整文本是

private class CreateButtonHandler 
    implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
     String inName, inType; //local variables 
     int inAge; 
     Dog arf; 
     inName = nameTF.getText(); 
     inType = typeTF.getText(); 
     inAge = Integer.parseInt(ageTF.getText()); 
     //System.out.println("Inside CreateButtonHandler"); 
     System.out.println(inName); 
     arf = new Dog(inName, inType, inAge); 
     System.out.println(arf.toString()); 

    } 

} 

,整個程序如下。任何幫助/解釋/建議將非常受歡迎。

import javax.swing.*; // import statement for the GUI components 
import java.awt.*; //import statement for container class 
import java.awt.event.*; 



public class DogGUI //creation of DogGUI clas 
{ 
private JLabel nameL, typeL, ageL, outtputL; 
private JTextField nameTF, typeTF, ageTF; 
private JButton createB, exitB; 
CreateButtonHandler cbHandler; 
ExitButtonHandler ebHandler; 

    public void driver() //creates everything 
    { 
     //create the window 
     JFrame DogInfo = new JFrame ("Dog GUI"); 
     DogInfo.setSize(400,300); //set the pixels for GUI 
     DogInfo.setVisible(true); // set visibility to true 
     DogInfo.setDefaultCloseOperation(DogInfo.EXIT_ON_CLOSE); // when closed JFrame will disappear 

//layout 
Container DogFields = DogInfo.getContentPane(); 
DogFields.setLayout(new GridLayout(5,2)); 

// setting labels for GUI 
nameL = new JLabel ("Enter name of Dog: ", 
        SwingConstants.RIGHT); 
typeL = new JLabel ("Enter the type of the Dog: ", 
        SwingConstants.RIGHT); 
ageL = new JLabel ("Enter the age of the Dog: ", 
        SwingConstants.RIGHT); 
outtputL = new JLabel ("Dog Information: ", 
        SwingConstants.RIGHT); 

//Buttons 

JButton createB, exitB; //creating button for creation of Dog and button to exit 
createB = new JButton("Create Dog"); 
exitB = new JButton("Exit"); 

//text fields 

JTextField nameTF, typeTF, ageTF, outtputTF; 
nameTF = new JTextField(10); 
typeTF = new JTextField(15); 
ageTF = new JTextField(5); 
outtputTF = new JTextField(25); 

outtputTF.setEditable(false); //this TF is to display this output 

//Lables and Textfields 
DogInfo.add(nameL); 
DogInfo.add(nameTF); 

DogInfo.add(typeL); 
DogInfo.add(typeTF); 

DogInfo.add(ageL); 
DogInfo.add(ageTF); 

DogInfo.add(outtputL); 
DogInfo.add(outtputTF); 

//Buttons 
DogInfo.add(createB); 
DogInfo.add(exitB); 


//Instantiate Listeners 
cbHandler = new CreateButtonHandler(); 
ebHandler = new ExitButtonHandler(); 

//Register Listeners 
createB.addActionListener(cbHandler); 
exitB.addActionListener(ebHandler); 

DogInfo.setVisible(true); 
} 
//run the program from main, instantiate class, invoke driver() method 

public static void main(String[] args) 
{ 
    DogGUI d = new DogGUI(); 
    d.driver(); 
} 

// class to actually handle Dog creation 

private class CreateButtonHandler 
    implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
     String inName, inType; //local variables 
     int inAge; 
     Dog arf; 
     inName = nameTF.getText(); 
     inType = typeTF.getText(); 
     inAge = Integer.parseInt(ageTF.getText()); 
     //System.out.println("Inside CreateButtonHandler"); 
     System.out.println(inName); 
     arf = new Dog(inName, inType, inAge); 
     System.out.println(arf.toString()); 

    } 

} 


private class ExitButtonHandler 
    implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
     { 
      System.out.println("inside exit button"); 
      System.exit(0); 
     } // end method actionPerformed 
} 


} 

回答

3

你必須在driver()方法的局部變量叫做nameTF被隱藏的那場(與其他一些變量)。

JTextField nameTF, typeTF, ageTF, outtputTF; 
nameTF = new JTextField(10); 

刪除這些字段的聲明,因爲它們已經聲明爲實例字段。

private JTextField nameTF, typeTF, ageTF; 

(可能不是outtputTF,這取決於你想要做什麼)

+0

非常感謝您索蒂里奧斯 – user2028341

+0

@ user2028341您可以接受的答案是否能解決你的問題。 –

+0

我的確感謝Sotirios – user2028341