我是Java的完全新手,我不太明白为什么我无法从我创建的JTextField获取getText()。
我希望能够使用已输入的电子邮件和密码创建新用户,但我在JTextFields emailText和passwordText上收到nullPointerException,可能是因为即使在字段中键入了某些内容后它们仍然为null运行程序后。
另外,我知道这根本不安全,但现在没关系。
由于
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;
}
}
答案 0 :(得分:2)
您定义了两次文本字段。一次作为实例变量,一次作为局部变量。摆脱局部变量。代码应该是:
//JTextField emailText = new JTextField(30);
emailText = new JTextField(30);
答案 1 :(得分:0)
您的登录类应该是这样的
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);
}
}
}
}
并且您的User类应该像这样实现Serializeable才能正常工作
编辑您的构造函数也搞砸了
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;
}
}