Java null pointer exception in REST API
Java null pointer exception in REST API
I am actually using postman to send login request to my REST API but it's giving me a javaNullpointer
exception. It's because in model class one of my variables is not getting a value. setPhone_Number()
function is not called in my model class. So the phone_number
variable is empty.
javaNullpointer
setPhone_Number()
phone_number
Help is appreciated.
Here is my error screnshot eclipse error for null pointer
here is what i am sending through postman Postman
here is the login dao class.
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")))){
Here is model class login
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;
}
}
Here is 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");
}
}
line number 52 in LoginDao
it seems that you are not taking in
login log
param in loginResource.doLogin
– Fabulous
Jul 3 at 6:51
login log
loginResource.doLogin
As it is, the
login
constructor sets phonenumber
to null. Also, LoginDao
's constructor sets phonenumber
to null. they will remain null as long as you supply your own login
, which i see you are trying to do in loginResource
. Could you post the request headers?– Fabulous
Jul 3 at 6:53
login
phonenumber
LoginDao
phonenumber
login
loginResource
@EmptyBrain this is line 52 if((this.phone_number.equals(rs.getString("phone_number"))) && (this.password.equals(rs.getString("password")))){
– immad Naseem
Jul 3 at 7:10
@Fabulous sir as u can see in first screenshot that setting abc123 is called which means password is set but setPhone_Number() function is not called.
– immad Naseem
Jul 3 at 7:14
2 Answers
2
In your login.java
class, replace: private String phone_number;
with private String phoneNumber;
and update its getters and setters according like so:
login.java
private String phone_number;
private String phoneNumber;
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
Still same problem. Actually my setPoneNumber function is not being called, i check it by printing values. Setpassword function is working correctly :(
– immad Naseem
Jul 3 at 8:06
Have you updated the json body in postman as well? Change phone_number to phoneNumber and let me know if setter is being called or not.
– Kamran Abdul Sattar
Jul 3 at 9:20
In loginResource.java
, the line
loginResource.java
LoginDao loginDao = new LoginDao();
LoginDao loginDao = new LoginDao();
calls this method in LoginDao
:
LoginDao
public LoginDao(){
phone_number = null;
password = null;
}
where you have explicitly set phone_number = null
phone_number = null
now on Line 52 of LoginDao
LoginDao
if((this.phone_number.equals(rs.getString("phone_number"))) && (this.password.equals(rs.getString("password"))))
this.phone_number
corresponds to the LoginDao
object most recently constructed, which has phone_number
set to null. Even before values can be compared with rs
, the equals
function fails and you get a null pointer
exception.
this.phone_number
LoginDao
phone_number
rs
equals
null pointer
You should instead take in the values phone_number
and password
from the request. Try creating a parametrized constructor and assigning them the values you pass, like so:
phone_number
password
public LoginDao(String number, String password){
this.phone_number = number;
this.password = password;
}
and calling it from loginResource
like so:
loginResource
if(user_type.equalsIgnoreCase("Rider"))
return new loginDao(number,password).doLogin(log,"Rider");
else
return new loginDao(number,password).doLogin(log,"Driver");
}
Alternatively, I think you are trying to save parameters into Login
object, but you haven't done that anywhere in loginResource
. You should change your constructor in loginDao
to
Login
loginResource
loginDao
public loginDao() { }
and write the following in loginResource
:
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");
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
In stack trace. it shows the error in
line number 52 in LoginDao
. Can you Please share the 52 number line from LoginDao?– Empty Brain
Jul 3 at 6:40