2013-07-15 41 views
0

我正在構建一個應用程序,用於驗證來自Amazon Web Service s3文件的登錄, 它可以驗證登錄是否正常,但是當我嘗試獲取用戶在textField中輸入的文本。使用.getText()從文本字段獲取文本的問題

這是代碼,構建UI的部分:

public class UserInterface extends JFrame implements ActionListener { 

JLayeredPane pane; 
JFrame f; 
JTextField usernameLogin; 
JTextField passwordLogin; 
UserInterface ui; 
JButton loginButton; 
static String loggedInAs = null; 
static AmazonS3  s3; 
static boolean tryToLogin = false; 


public UserInterface() { 

    JLayeredPane pane = new JLayeredPane(); 

    JTextField usernameLogin = new JTextField("Username...",20); 
    usernameLogin.setLocation(650,200); 
    usernameLogin.setSize(500, 30); 
    usernameLogin.setVisible(true); 
    pane.add(usernameLogin, 1, 0); 

    JTextField passwordLogin = new JTextField("Password...",20); 
    passwordLogin.setLocation(650,240); 
    passwordLogin.setSize(500, 30); 
    passwordLogin.setVisible(true); 
    pane.add(passwordLogin, 1, 0); 

    JButton loginButton = new JButton("login"); 
    loginButton.setLocation(650,290); 
    loginButton.setSize(75, 20); 
    loginButton.addActionListener(this); 
    loginButton.setVisible(true); 
    pane.add(loginButton, 1, 0); 


    this.add(pane); 

} 

我不認爲problom來自那裏,但我可能是錯的。 該程序的下一部分是一個邏輯處理器,與服務器一起使用來驗證與Amazon s3的登錄。它在main()中。

 int INFINITE = 1; 
    try { 
     System.out.println("Downloading an object"); 
     S3Object object = s3.getObject(new GetObjectRequest("saucyMMO", "logins.txt")); 
     System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); 

     while (INFINITE == 1) { 
      System.out.println("ran"); 
      if (tryToLogin == true) { 
       System.out.println("ran2"); 
       INFINITE = 0; 
      BufferedReader br = new BufferedReader(new InputStreamReader(object.getObjectContent())); 
      String lineValue = null; 
      while((lineValue = br.readLine()) != null && loggedInAs == null){ 
       String splitResult[] = lineValue.split(","); 
       boolean retVal = splitResult[0].equals(ui.usernameLogin.getText()); 
       boolean retVal2 = splitResult[1].equals(ui.passwordLogin.getText()); 
       if (retVal == true && retVal2 == true) { 
       loggedInAs = splitResult[0]; 
       System.out.println("logged in as : " + loggedInAs); 
       } 
       else { 
        System.out.println("SPLIT 0 : " + splitResult[0]); 
        System.out.println("SPLIT 1 : " + splitResult[1]); 
       } 
      } 
      } 
     } 
    } catch (AmazonServiceException ase) { 

    } catch (AmazonClientException ace) { 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

我總是得到一個空指針異常時,我稱之爲 「ui.usernameLogin.getText()」 或 「ui.passwordLogin.getText()」。

具體來說,

java.lang.NullPointerException 
at UserInterface.main(UserInterface.java:102) 
+0

你用一個使用變量監視的工具調試它嗎?找出哪個調用返回null非常有用。例如,你確定你的lineValue有一個非null值嗎? –

回答

2

您在構造函數這裏定義的局部變量

JTextField usernameLogin = new JTextField("Username...",20);

,其不同於JTextField usernameLogin;聲明這是從來沒有初始化並null這就是爲什麼你有一個NullPointerException

更改此

public class UserInterface extends JFrame implements ActionListener { 
    JLayeredPane pane; 
    JFrame f; 
    JTextField usernameLogin; 
    JTextField passwordLogin; 
    UserInterface ui; 
    JButton loginButton; 


    public UserInterface() { 
    LayeredPane pane = new JLayeredPane(); 

    JTextField usernameLogin = new JTextField("Username...",20); 
    usernameLogin.setLocation(650,200); 
    usernameLogin.setSize(500, 30); 
    usernameLogin.setVisible(true); 
    pane.add(usernameLogin, 1, 0); 

    JTextField passwordLogin = new JTextField("Password...",20); 
    passwordLogin.setLocation(650,240); 
    passwordLogin.setSize(500, 30); 
    passwordLogin.setVisible(true); 
    pane.add(passwordLogin, 1, 0); 

    JButton loginButton = new JButton("login"); 
    loginButton.setLocation(650,290); 
    loginButton.setSize(75, 20); 
    loginButton.addActionListener(this); 
    loginButton.setVisible(true); 
    pane.add(loginButton, 1, 0); 


    this.add(pane); 
} 

TO

public class UserInterface { 
    private JLayeredPane pane; 
    private JTextField usernameLogin; 
    private JTextField passwordLogin; 
    private JButton loginButton; 
    private JTextField usernameLogin; 
    private JFrame frame; 

    public UserInterface() { 
    usernameLogin = new JTextField("Username...",20); 
    usernameLogin.setLocation(650,200); 
    usernameLogin.setSize(500, 30); 
    usernameLogin.setVisible(true); 
    pane.add(usernameLogin, 1, 0); 

    passwordLogin = new JTextField("Password...",20); 
    passwordLogin.setLocation(650,240); 
    passwordLogin.setSize(500, 30); 
    passwordLogin.setVisible(true); 
    pane.add(passwordLogin, 1, 0); 

    loginButton = new JButton("login"); 
    loginButton.setLocation(650,290); 
    loginButton.setSize(75, 20); 
    loginButton.addActionListener(this); 
    loginButton.setVisible(true); 
    pane.add(loginButton, 1, 0); 

    frame= new JFrame(); 
    //configure JFrame 

    frame.add(pane); 
    frame.pack();//size the frame 
    frame.setVisible(Boolean.TRUE); 
    } 

     //you have to added to some component 
     private class MyActionListener implements ActionListener{ 
      @Override 
      public void actionPerformed(ActionEvent evt){ 
        //code here 
      } 
     } 

} 

幾個建議:

不要延伸JFrame有,而不是引用(在繼承組成),並沒有在最佳實施類ActionListener改爲使用Anonymous classInner Classes

某些編碼:

(retVal == true && retVal2 == true)將其更改爲(retVal && retVal2)

管理例外情況,從來沒有空白漁獲

} catch (AmazonServiceException ase) { 

    } 
+0

如何使局部變量爲靜態? – user1952381

+0

靜態?你爲什麼要靜態? – nachokk

+0

據我所知,你可以從我的代碼中的任何地方訪問靜態的東西,我需要能夠從我的main() – user1952381

0

從你提供的代碼,你永遠不分配一個對象到ui變量。此外,usernameLogin是在本地創建的,未分配給該類的成員變量

+0

如何將它分配給類成員變量?謝謝btw。 – user1952381

0

這意味着您正嘗試將空值傳遞到不帶空值的地方。您需要執行一些錯誤處理來檢查空值。您也可以使用==運算符來檢查空值。 .equals比較對象。

if(ui.username.getText() != null){ 
      boolean retVal = splitResult[0].equals(ui.usernameLogin.getText()); 
      boolean retVal2 = splitResult[1].equals(ui.passwordLogin.getText()); 
    }