[英]java/swing- Why did my return method return to NULL after assigning a value?
我创建了一个用户登录对话框,用于将存储在csv文件中的用户名和密码与他们在相应文本字段中输入的内容进行比较。 在登录出现之前,我使用了JOptionPanes来显示我已经将用户名密码传入的返回语句。 JOptionPane显示名称和密码。 但是在我登录后出现,然后输入要比较的名称和密码,登录不成功。 当我使用JOptionPane再次显示相同的用户名和密码时,它表示两者都为空。 我已经缩小了我设置一个单独的布尔方法的问题,我用它来查看用户输入的内容是否等于返回方法的值,但由于某种原因,我的返回方法在比较之前返回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;
}
}
});
鉴于以下工作正常
public class PasswordData {
private String _password;
private String _username;
public PasswordData(String password, String username)
{
_password = password;
_username = username;
}
public String getUsername()
{
return _username;
}
public void setUsername(String username)
{
_username = username;
}
public String getPassword()
{
return _password;
}
public void setPassword(String password)
{
_password = password;
}
}
public class StackOverflowPasswordExample {
private static PasswordData cn;
public static void main(String[] args) {
cn = new PasswordData("mypassword", "myusername");
boolean auth = authenticate();
System.out.println("authenticated is " + auth);
}
public static boolean authenticate() {
String g2 = cn.getPassword();
String h2 = cn.getUsername();
return g2 != null && h2 != null;
}
}
您没有提供足够的信息,我们需要设置代码,更新代码,因为它几乎肯定存在问题(例如,您认为它是实例或被覆盖的实例)
在你提供一个较短的例子之前,我无法进一步帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.