package javajazzup;
public class LoginBean{
String loginid;
String pwd;
public LoginBean(){}
public String getLoginid(){
return loginid;
}
public void setLoginid(String loginid){
this.loginid = loginid;
}
public String getPwd(){
return pwd;
}
public void setPwd(String pwd){
this.pwd = pwd;
}
public String CheckValidUser(){
if(loginid.equals("JavaJazzUp") && pwd.equals("mypwd")){
return "success";
}
else{
return "fail";
}
}
}
答案 0 :(得分:6)
您应该将方法CheckValidUser()更改为:
public String CheckValidUser(){
if("JavaJazzUp".equals(loginid) && "mypwd".equals(pwd)){
return "success";
}else{
return "fail";
}
}
答案 1 :(得分:3)
<强>测试用例强>
public static void LoginBeanTest() {
LoginBean bean = new LoginBean();
bean.setLoginid("JavaJazzUp");
bean.setPwd("mypwd");
System.out.println(bean.CheckValidUser());
bean.setPwd("wrongpwd");
System.out.println(bean.CheckValidUser());
bean.setLoginid(null);
try {
System.out.println(bean.CheckValidUser());
catch (NullPointerException npe) {
System.out.println("NPE as expected");
}
bean.setLoginid("");
bean.setPwd(null);
try {
System.out.println(bean.CheckValidUser());
catch (NullPointerException npe) {
System.out.println("NPE as expected");
}
}
答案 2 :(得分:1)
要获得null pointer
loginid
,null
或pwd
为null
。似乎他们没有正确设置。但是,如果没有提供更多信息,我们无法帮助您。
答案 3 :(得分:1)
您的私有成员变量loginid
和pwd
都在默认构造函数中初始化为null,因为您没有做任何事情来将它们设置为任何默认值。如果您取消引用任何一个而不将它们设置为非空引用,您将获得NullPointerException
。
试试这样:
package javajazzup;
public class LoginBean{
String loginid;
String pwd;
public LoginBean(String user, String password)
{
this.loginid = user;
this.pwd = password;
}
public LoginBean()
{
this("", "");
}
public String getLoginid(){
return loginid;
}
public void setLoginid(String loginid){
this.loginid = loginid;
}
public String getPwd()
{
return pwd;
}
public void setPwd(String pwd)
{
this.pwd = pwd;
}
public boolean checkValidUser(String username, String password)
{
return (loginid.equals(username) && pwd.equals(password));
}
}