0
我創建了一個用戶登錄對話框,用於比較它們存儲在csv文件中的用戶名和密碼與他們在相應文本框內鍵入的內容。在登錄出現之前,我使用JOptionPanes來顯示我已經傳遞用戶名稱密碼的返回語句。 JOptionPane顯示名稱和密碼。但是我登錄後出現,然後輸入名稱和密碼進行比較,登錄不成功。當我使用JOptionPane再次顯示相同的用戶名和密碼時,它說兩者都是空的。我已經縮小了我設置了一個單獨的布爾方法的問題,我使用它來查看用戶輸入的內容是否等於返回方法的值,但由於某種原因,我的返回方法在比較之前返回NULL。java/swing-爲什麼我的返回方法在賦值後返回NULL?
有關爲何發生這種情況的任何建議?再次,由於某種原因,我正在失去返回值的「getName()」和「getPass()」。
/**Read csv data. put data into array, then elements into variables */
String lines = "";
String unparsedFile = "";
FileReader fr = new FileReader("c:\\foo\\user.dat");
BufferedReader br = new BufferedReader(fr);
while((lines = br.readLine()) != null){
unparsedFile += lines;
}
br.close();
userData = unparsedFile.split(",");
String g = userData[0];
g1 = userData[0]; // return in String method "getName()"
this.name = g1;
h1 = userData[1]; // returned in String method "getPass()"
this.password = h1;
///////////////////////////////
/* check if what you entered is same as getName() and getPass() */
public static boolean authenticate(String username, String password){
String g2, h2;
g2 = cn.getName();
h2 = cn.getPass();
JOptionPane.showInputDialog(null, cn.getName() + " login " + cn.getPass());
if(username.equals(g2) && password.equals(h2)){
JOptionPane.showInputDialog(null, "True");
return true;
}
JOptionPane.showInputDialog(null, cn.getName() + " login false " + cn.getPass());
return false;
}
////////////////////////
/* Login: enter name and password, the check if it matches getName() and getPass() */
nam = tfUsername.getText();
passA = pfPassword.getText();
if(Login.authenticate(nam, passA)){
JOptionPane.showMessageDialog(UserLogin.this,
"Welcome, " + cn.getChatName() + ". Login successful.",
"Login",
JOptionPane.INFORMATION_MESSAGE);
succeeded = true;
dispose();
}
if(!nam.equals(cn.getName()) && !passA.equals(cn.getPass())){
JOptionPane.showMessageDialog(UserLogin.this, "Invalid username or password",
"Login unsuccessful", JOptionPane.ERROR_MESSAGE);
//reset username and password
tfUsername.setText("");
pfPassword.setText("");
succeeded = false;
}
}
});
如果您提供[SSCCE](http://pscode.org/sscce.html),您可能會得到答案。我們甚至不知道'cn'是什麼。 – Howard 2012-03-25 09:20:14
@Howard cn只是一個類名。這三個部分是3個不同的類別。 – SpicyWeenie 2012-03-25 09:41:42
霍華德是正確的需要更多的代碼cn是一個謎,我應該澄清,如果我們可以看到setName和setPassword被調用,這也會很好。 – 2012-03-25 09:46:29