use*_*210 0 java android nullpointerexception
我写了一个方法来检查输入的用户名和密码,文本文件包含所有用户名和密码.有趣的问题是,如果细节是正确的,那么应用程序会进行,但如果细节不正确,则会返回NullPointerException.我的代码如下:
// Checks whether the inputed details are correct
public boolean isCorrect(String u, String p) {
boolean check = false;
String line = null;
try {
do {
line = br.readLine();
// System.out.println("Checking profile : " + line);
String[] info = line.split("\t");
// nested if-statement to improve efficiency over &&
if (info[0].equals(u)) {
System.out.println("username found!");
if (info[1].equals(p)) {
System.out.println("password correct!");
check = true;
} else System.out.println("password incorrect!");
} else System.out.println("username not found!");
} while (line != null && check == false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return check;
}
Run Code Online (Sandbox Code Playgroud)
返回的布尔值输入到主活动中的以下代码中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpanel);
Button button = (Button) findViewById(R.id.btnLogin);
Correct = false;
lm = new LoginModel(this);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText textUsername = (EditText) findViewById(R.id.textUsername);
EditText textPassword = (EditText) findViewById(R.id.textPassword);
// Collect the login details entered by the user
String username = textUsername.getText().toString();
String password = textPassword.getText().toString();
// Check the application is registering the correct details
System.out.println(username + "\n" + password);
Correct = lm.isCorrect(username, password);
if (Correct) { // if details are correct then start main program
Intent intent = new Intent (LoginView.this, MenuListView.class);
startActivity(intent);
}
else System.out.println("Login is incorrect...");
}
});
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么如果Correct = true然后它运行没有问题,但如果Correct = false它崩溃程序产生一个FATAL EXCEPTION:main,然后一个NullPointerException - 它不应该只是输出System.out .println.