Nukhba Arshad Nukhba Arshad - 4 months ago 26
Android Question

Am getting null point exception at retrofit onResponse method

my Json:

{
"status": "1",
"login": "sucess"
}


this is my model class:

public class LoginModel {

@SerializedName("status")
@Expose
private String status;

@SerializedName("login")
@Expose
private String login;
...}


this is my interface:

public interface RestInterface {

String url = "http://192.168.10.2/tourist/v1";

@FormUrlEncoded
@POST("/login")
Call<LoginModel> Login(@Field("email") String email,
@Field("pass") String passs);
}


this is my call method:

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RestInterface.url)
.addConverterFactory(GsonConverterFactory.create())
.build();
//Creating Rest Services
final RestInterface restInterface = retrofit.create(RestInterface.class);

//Calling method to get check login
Call<LoginModel> response = restInterface.Login(Email.getText().toString(), Password.getText().toString());
response.enqueue(new Callback<LoginModel>() {
@Override
public void onResponse(Response<LoginModel> response, Retrofit retrofit) {
finish();
startActivity(getIntent());

Email.setText("");
Password.setText("");

LoginModel lm = response.body();

if ( lm.getStatus().equals("1")) { //login Success

Toast.makeText(LoginActivity.this, "Login In SuccessFully", Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this,AfterLoginActivity.class);
startActivity(i);

} else if (lm.getStatus().equals("0")) // login failure
{
Toast.makeText(LoginActivity.this, "Invalid UserName/Password ", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Throwable t) {
finish();
startActivity(getIntent());
// String merror = error.getMessage();
Toast.makeText(LoginActivity.this, "try again", Toast.LENGTH_LONG).show(); }
});


Am getting null point exception at line => lm.getstatus().equals(1);
It was working fine before with retrofit 1.9 then I changed it into Retrofit 2 and am getting null point exception now!

Anu Anu
Answer

https://inthecheesefactory.com/blog/retrofit-2.0/en has clarifications on on the new URL declaration pattern in Retrofit 2.0. It suggests

- Base URL: always ends with /
- @Url: DO NOT start with /

In your case the url might be interpreted as "http://192.168.10.2/login". Try using

String url = "http://192.168.10.2/tourist/v1/";

and

@POST("login")