gpt4 book ai didi

REST API 中的 Java 空指针异常

转载 作者:行者123 更新时间:2023-12-02 11:03:36 25 4
gpt4 key购买 nike

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

感谢帮助。

这是我的错误屏幕截图 eclipse error for null pointer 这是我通过 postman 发送的内容 Postman

这是登录 dao 类。

 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");
}

}

最佳答案

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

private String Phone_number;

私有(private)字符串电话号码;

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

public String getPhoneNumber() {
return phoneNumber;
}

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

关于REST API 中的 Java 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51147974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com