简体   繁体   中英

Issue with getting text from a text field with .getText()

I am building a application that verifies a logins from a Amazon Web Service s3 file, It can verify a login fine, but the issue arises when I try to get the text entered by the user in a textField.

This is the section of code that builds the 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);

}

I dont think the problom comes from there, but I could be wrong. The next part of the program is a logic processor that works with the server to verify a login with Amazon s3. It is in the 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();
    } 

I always get a null pointer exception when I call the "ui.usernameLogin.getText()" or "ui.passwordLogin.getText()".

Specifically,

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

You are defining a local variable here in constructor

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

that is different from JTextField usernameLogin; declaration this is never initialized and is null that's why you have a NullPointerException .

Change this

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

}

A few advices :

Don't extends JFrame have a reference instead (composition over inheritance) and don't implement in top classes ActionListener instead use Anonymous class or Inner Classes

Some coding :

(retVal == true && retVal2 == true) change it to (retVal && retVal2)

Manage exceptions , never blank catches

} catch (AmazonServiceException ase) {

    } 

From the code you supplied, you never assigned an object to the ui variable. Additionally, usernameLogin was created locally, and not assigned to the class's member variable

That means that you are trying to pass a null value into somewhere where it doesn't take a null value. You will need to do a bit of error handling as to check for a null value. Also you use the == operator to check for null values. The .equals compares objects.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM