0

我正在构建一个验证来自 Amazon Web Service s3 文件的登录的应用程序,它可以验证登录是否正常,但是当我尝试获取用户在 textField 中输入的文本时会出现问题。

这是构建 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);

}

我不认为问题来自那里,但我可能是错的。该程序的下一部分是一个逻辑处理器,它与服务器一起使用 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)
4

3 回答 3

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);
}

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) {

    } 
于 2013-07-15T18:00:39.157 回答
0

从您提供的代码中,您从未将对象分配给ui变量。此外,usernameLogin是在本地创建的,而不是分配给类的成员变量

于 2013-07-15T18:01:51.020 回答
0

这意味着您正在尝试将空值传递到它不采用空值的地方。您需要做一些错误处理来检查空值。您还可以使用 == 运算符来检查空值。.equals 比较对象。

    if(ui.username.getText() != null){
          boolean retVal = splitResult[0].equals(ui.usernameLogin.getText());
          boolean retVal2 = splitResult[1].equals(ui.passwordLogin.getText());
    }
于 2013-07-15T18:03:58.313 回答