我正在構建一個應用程序,用於驗證來自Amazon Web Service s3文件的登錄, 它可以驗證登錄是否正常,但是當我嘗試獲取用戶在textField中輸入的文本。使用.getText()從文本字段獲取文本的問題
這是代碼,構建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);
}
我不認爲problom來自那裏,但我可能是錯的。 該程序的下一部分是一個邏輯處理器,與服務器一起使用來驗證與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)
你用一個使用變量監視的工具調試它嗎?找出哪個調用返回null非常有用。例如,你確定你的lineValue有一個非null值嗎? –