ComlyW
ComlyW

Reputation: 77

getText for JTextField input after action event

I am a complete newb to Java and I don't quite understand why I am not able to getText() from the JTextFields I have created.

I want to be able to create a new User with the email and password that has been input, but I am getting a nullPointerException on the JTextFields emailText and passwordText, presumably because they are remaining null even after something is typed into the fields after running the program.

Also, I know this isn't secure at all, but that's okay for now.

Thanks

import javax.swing.*;

import java.util.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class Login {
  JFrame frame;
  JPanel panel;
  JTextField emailText;
  JPasswordField passwordText;
  ObjectOutputStream out;
  ObjectInputStream in ;

  public static void main(String[] args) {
    new Login().go();
  }

  public void go() {
    frame = new JFrame("Npact Login");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    BorderLayout layout = new BorderLayout();
    JPanel background = new JPanel(layout);
    background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    Font bigFont = new Font("sanserif", Font.BOLD, 24);

    JLabel instructions = new JLabel("Welcome.");
    instructions.setFont(bigFont);
    JLabel instructions2 = new JLabel("To begin, enter the email and password");
    instructions2.setFont(bigFont);
    buttonBox.add(instructions);
    buttonBox.add(instructions2);

    JLabel emailLabel = new JLabel("Email:");
    JTextField emailText = new JTextField(30);
    emailText.requestFocus();
    buttonBox.add(emailLabel);
    buttonBox.add(emailText);

    JLabel passwordLabel = new JLabel("Password:");
    JPasswordField passwordText = new JPasswordField(30);
    passwordText.addActionListener(new MyStartActionListener());
    buttonBox.add(passwordLabel);
    buttonBox.add(passwordText);

    JButton start = new JButton("Get Started!");
    start.addActionListener(new MyStartActionListener());
    buttonBox.add(start);

    frame.getContentPane().add(buttonBox);
    frame.setSize(1000, 250);
    frame.setVisible(true);
  }

  public class MyStartActionListener implements ActionListener {
    public void actionPerformed(ActionEvent a) {
      String name = emailText.getText();
      char[] pass = passwordText.getPassword();

      User newUser = new User(name, pass);
      try {
        out.writeObject(newUser);
      } catch (Exception ex) {
        System.out.println(ex);
      }
    }
  }
}
package co.npact;

public class User {
  private String email;
  private char[] password;

  public User(String e, char[] p) {
    e = email;
    p = password;
  }

  public String getEmail() {
    return email;
  }

  public char[] getPassword() {
    return password;
  }
}

Upvotes: 0

Views: 595

Answers (2)

JRowan
JRowan

Reputation: 7104

your login class should be like this

import javax.swing.*;

import java.util.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

public class Login {
  JFrame frame;
  JPanel panel;
  JTextField emailText;
  JPasswordField passwordText;
  ObjectOutputStream out;
  ObjectInputStream in ;

  public static void main(String[] args) {
    new Login().go();
  }

  public void go() {
    frame = new JFrame("Npact Login");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    BorderLayout layout = new BorderLayout();
    JPanel background = new JPanel(layout);
    background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    Box buttonBox = new Box(BoxLayout.Y_AXIS);
    Font bigFont = new Font("sanserif", Font.BOLD, 24);

    JLabel instructions = new JLabel("Welcome.");
    instructions.setFont(bigFont);
    JLabel instructions2 = new JLabel("To begin, enter the email and password");
    instructions2.setFont(bigFont);
    buttonBox.add(instructions);
    buttonBox.add(instructions2);

    JLabel emailLabel = new JLabel("Email:");
    emailText = new JTextField(30);
    emailText.requestFocus();
    buttonBox.add(emailLabel);
    buttonBox.add(emailText);

    JLabel passwordLabel = new JLabel("Password:");
    passwordText = new JPasswordField(30);
    passwordText.addActionListener(new MyStartActionListener());
    buttonBox.add(passwordLabel);
    buttonBox.add(passwordText);

    JButton start = new JButton("Get Started!");
    start.addActionListener(new MyStartActionListener());
    buttonBox.add(start);

    frame.getContentPane().add(buttonBox);
    frame.setSize(1000, 250);
    frame.setVisible(true);
  }

  public class MyStartActionListener implements ActionListener {
    public void actionPerformed(ActionEvent a) {
      String name = emailText.getText();
      char[] pass = passwordText.getPassword();

      User newUser = new User(name, pass);

      try {
          FileOutputStream fos = new FileOutputStream("t.tmp");
          ObjectOutputStream oos = new ObjectOutputStream(fos);

          //oos.writeInt(12345);
          //oos.writeObject("Today");
          oos.writeObject(newUser);

          oos.close();
      } catch (Exception ex) {
        System.out.println(ex);
      }
    }
  }
}

and your User class should implement Serializeable like this for it to work correctly

edit your constructor was messed up too

import java.io.Serializable;

public class User implements Serializable {
  private String email;
  private char[] password;

  public User(){

  }
  public User(String e, char[] p) {
    email = e;
    password = p;
  }

  public String getEmail() {
    return email;
  }

  public char[] getPassword() {
    return password;
  }
}

Upvotes: 0

camickr
camickr

Reputation: 324098

You defined your text fields twice. Once as an instance variable and once as a local variable. Get rid of the local variable. The code should be:

//JTextField emailText = new JTextField(30);
emailText = new JTextField(30);

Upvotes: 2

Related Questions