REST API中的Java空指针异常

时间:2018-07-03 06:32:36

标签: java eclipse rest web-services

我实际上是使用邮递员将登录请求发送到我的REST API,但这给了我javaNullpointer异常。这是因为在模型类中,我的变量之一没有得到值。我的模型类未调用setPhone_Number()函数。因此phone_number变量为空。

感谢您的帮助。

这是我的错误记录eclipse error for null pointer  这是我通过邮递员Postman

发送的邮件

这是登录岛类。

 public class LoginDao {

 private String password;
 private String phone_number;


    public LoginDao(){
        phone_number = null;
        password = null;
    }

    public Response doLogin(login logg, String user_type) throws SQLException{
        ResultSet rs = null;
        boolean userType = true;        
        //if userType is resource then boolean is false, if userType is user then boolean is true.
        //logg.setPhone_Number(phone_number);

        DBConnection dbConnection = new DBConnection();
        //Connection connection = dbConnection.getConnection();
        this.phone_number = logg.getPhone_Number();
        this.password = logg.getPassword();


        try{
            if(user_type.equalsIgnoreCase("Rider")) {
                rs = dbConnection.runSql("select rider_id, phone_number, password from rider");
                userType = true;

            }
            else if(user_type.equalsIgnoreCase("Driver")) {
                rs = dbConnection.runSql("select driver_id, phone_number, password from driver");   
                userType = false;
            }

            while(rs.next()){
                System.out.println(phone_number);
                System.out.println(rs.getString("phone_number"));
                System.out.println("user"+user_type);

                if((this.phone_number.equals(rs.getString("phone_number"))) && (this.password.equals(rs.getString("password")))){

这是模型类登录

 public class login {

private String phone_number;
private String password;

public login(){

}

public String getPhone_Number() {

    System.out.println("getting "+phone_number);
    return phone_number;
}

public void setPhone_Number(String phone_number) {

    System.out.println("seting "+phone_number);
    this.phone_number = phone_number;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {

    System.out.println("seting "+password);
    this.password = password;
}

}

这里是loginResource.java

 import javax.ws.rs.POST;

 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;

 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

 import java.sql.SQLException;

  import javax.ws.rs.Consumes;

  import io.github.yasirfaisal21.schoolvan.schoolvan.dao.LoginDao;
 import io.github.yasirfaisal21.schoolvan.schoolvan.model.login;

  @Path("login")


 public class loginResource {
@Produces(MediaType.APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
@POST
public Response doLogin(login log,@QueryParam("user_type") String user_type ) 
        throws SQLException{


LoginDao loginDao = new LoginDao();
if(user_type.equalsIgnoreCase("Rider"))
        return loginDao.doLogin(log,"Rider");
else
    return loginDao.doLogin(log,"Driver");
 }

}

2 个答案:

答案 0 :(得分:1)

在您的login.java类中,替换:

private String phone_number;

private String phoneNumber;

并按照如下方式更新其getter和setter:

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}   

答案 1 :(得分:0)

loginResource.java中,行

LoginDao loginDao = new LoginDao();

LoginDao中调用此方法:

public LoginDao(){
        phone_number = null;
        password = null;
    }

您已明确设置phone_number = null

的位置

现在位于LoginDao的第52行

if((this.phone_number.equals(rs.getString("phone_number"))) && (this.password.equals(rs.getString("password"))))

this.phone_number对应于最近构造的LoginDao对象,该对象的phone_number设置为null。甚至在将值与rs进行比较之前,equals函数也会失败,并且会出现null pointer异常。

您应该改为从请求中获取值phone_numberpassword。尝试创建一个参数化的构造函数,并为它们分配您传递的值,如下所示:

public LoginDao(String number, String password){
         this.phone_number = number;
         this.password = password;
}

,然后像这样从loginResource调用它:

if(user_type.equalsIgnoreCase("Rider"))
        return new loginDao(number,password).doLogin(log,"Rider");
else
    return new loginDao(number,password).doLogin(log,"Driver");
 }

或者,,我认为您正在尝试将参数保存到Login对象中,但是您还没有在loginResource中的任何地方完成此操作。您应该将loginDao中的构造函数更改为

public loginDao() { }

并在loginResource中编写以下内容:

Login login = new Login();
if(user_type.equalsIgnoreCase("Rider")){
       login.setPhone_number("your phone number from request");
        return loginDao.doLogin(log,"Rider");
 }
else
  {
   login.setPhone_number("your phone number from request");
  return loginDao.doLogin(log,"Driver");
 }