-3

Nullpointerexception in accessing interface method in retrofit calling

Userservice.class

public interface UserService {

    @POST("login")
    Call<ResUser> login(@Body login login);
}

RetrofitClient.class

public class RetrofitClient {

    private static Retrofit retrofit=null;

    public static Retrofit getClient(String url){

        if(retrofit==null){
            retrofit=new Retrofit.Builder()
                    .baseUrl(url)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }
}

login.class

    private void doLogin(String username,String password){
    Call<ResUser> call=userService.login(new login(username,password));
    call.enqueue(new Callback<ResUser>() {
        @Override
        public void onResponse(Call<ResUser> call, Response<ResUser> response) {
            if(response.isSuccessful()){
             Log.e("succes");
            }
        }

        @Override
        public void onFailure(Call<ResUser> call, Throwable t) {

        }
    });

}

ApiUtils.class

public class ApiUtils {

    public static final String BASE_URL="";

    public static UserService getUserService(){
        return RetrofitClient.getClient(BASE_URL).create(UserService.class);
    }
}

i have nullpointerexception in this line

  Call<ResUser> call=userService.login(new login(username,password));
Goku
  • 9,102
  • 8
  • 50
  • 81

3 Answers3

0

i think your problem in login method and you should check null username and password before call:

Call call=userService.login(new login(username,password));

Accept my answer if it helpful for you.

LVS
  • 76
  • 1
  • 3
  • no it is not solve my problem still am getting nullpointer on same line – Mahendran B Nov 15 '17 at 05:07
  • Ok i seen your error. Add line below: UserService userService = ApiUtils.getUserService(); Call call=userService.login(new login(username,password)); ..... – LVS Nov 15 '17 at 05:33
0

add this UserService userService = ApiUtils.getUserService(); line of code before you make call to login service Call<ResUser> call=userService.login(new login(username,password));

UserService userService = ApiUtils.getUserService(); 
Call<ResUser> call=userService.login(new login(username,password));

you should have an instance of UserService interface named userService to call the method login()

Basil jose
  • 774
  • 3
  • 11
0

Buddy issue is that you are passing username and password here, so instead of using constructor try to use getter setters.

Also make object of userService class before calling it because it is not instantiated in your code.

UserService userservice = ApiUtils.getUserService(); 

And then try in a following way:

Call<ResUser> call=userService.login(new login(username,password));

You need to pass Object of class Login as you defined here it will get an object of login :

@POST("login")
Call<ResUser> login(@Body login login);

Now solution is that if your server needs object of login class then:

Before this:

Call<ResUser> call=userService.login(new login(username,password));

Make an obect of login class and set user name and password for that object and then pass this object as :

Login login = new Login();
login.setuserName(username);
login.setpassword(password);
UserService userservice = ApiUtils.getUserService(); 
Call<ResUser> call=userService.login(login);

Now if your server requires user name and password in body separately without login object then you need to change this:

@POST("login")
Call<ResUser> login(@Body login login);

to:

@POST("login")
Call<ResUser> login(@Path("userName") String userName,@Path("password") String password);

But as your web service path here @POST("login") shows that you dont need username and password in there so first solution is your solution.Make an object of class login and pass it on calling as I described above.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21