0

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.

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"); } } 
8
  • In stack trace. it shows the error in line number 52 in LoginDao. Can you Please share the 52 number line from LoginDao? Commented Jul 3, 2018 at 6:40
  • it seems that you are not taking in login log param in loginResource.doLogin Commented Jul 3, 2018 at 6:51
  • 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? Commented Jul 3, 2018 at 6:53
  • @EmptyBrain this is line 52 if((this.phone_number.equals(rs.getString("phone_number"))) && (this.password.equals(rs.getString("password")))){ Commented Jul 3, 2018 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. Commented Jul 3, 2018 at 7:14

2 Answers 2

1

In your login.java class, replace:

private String phone_number;
with
private String phoneNumber;

and update its getters and setters according like so:

public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Still same problem. Actually my setPoneNumber function is not being called, i check it by printing values. Setpassword function is working correctly :(
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.
0

In loginResource.java, the line

LoginDao loginDao = new LoginDao();

calls this method in LoginDao:

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

where you have explicitly set phone_number = null

now on Line 52 of 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.

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:

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

and calling it from loginResource like so:

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

public loginDao() { } 

and write the following in 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"); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.