我正在构建一个验证来自 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)