1

I have got a question. Why I have NullPointerExceptionduring getting data from ResponseModel.

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Integer bjd.adrian.retrofitjavapost.ResponseModel.getStatusOfLogin()' on a null object reference
    at bjd.adrian.retrofitjavapost.MainActivity$1.onResponse(MainActivity.java:51)

MainActivity:

private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onClick(View view)
{
    EditText usernameET = findViewById(R.id.usernameET);
    EditText passwordET = findViewById(R.id.passwordET);

    String username = usernameET.getText().toString();
    String password = passwordET.getText().toString();

    final RequestModel requestModel = new RequestModel();

    requestModel.setUsername(username);
    requestModel.setPassword(password);

    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://192.168.100.9/androidAppScripts/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    WebAPIInterface webAPIInterface = retrofit.create(WebAPIInterface.class);

    webAPIInterface.login(requestModel).enqueue(new Callback<ResponseModel>() {
        @Override
        public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
            ResponseModel responseModel = response.body();



            Log.v(TAG, "StatusOfLogin = "+responseModel.getStatusOfLogin()+"    ID = "+responseModel.getId());
        }

        @Override
        public void onFailure(Call<ResponseModel> call, Throwable t) {
            Log.e(TAG, t.toString());
        }
    });
}

RequestModel:

public class RequestModel
{
    @SerializedName("username")
    private String username;

    @SerializedName("password")
    private String password;

    //getters and setters
}

ResponseModel:

@SerializedName("statusOfLogin")
@Expose
private Integer statusOfLogin;
@SerializedName("id")
@Expose
private Integer id;

I am learning how to use retrofit. Please help me !

tharkay
  • 5,913
  • 2
  • 26
  • 33
ofevy
  • 35
  • 6

1 Answers1

0

I think the problem is that your link looks like this:

http://192.168.100.9/androidAppScripts//retrofitLogin.php.

This results as the concatenation of .baseUrl("http://192.168.100.9/androidAppScripts/") and @POST("/retrofitLogin.php").

You should remove the / in .baseUrl("http://192.168.100.9/androidAppScripts/") or in @POST("/retrofitLogin.php").

I suggest you create a constant BASE_URL and store http://192.168.100.9/androidAppScripts there and after this use .baseUrl(BASE_URL).

grrigore
  • 1,050
  • 1
  • 21
  • 39
  • I delete `/` from WebAPIInterface. It's working ! But I have next error `E/MainActivity: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $` – ofevy Nov 17 '18 at 07:49
  • How to repair it ? I thought to add code from the php script. Maybe in php script is wrong – ofevy Nov 17 '18 at 07:50
  • That is a different error.. so a different question. If my answer solved your problem you should accept it so other users will know it's solved. – grrigore Nov 17 '18 at 07:50
  • Do you know how to repair this error ? Do I make new question ? – ofevy Nov 17 '18 at 07:56
  • What does the PHP script contain? You can make a new question if you Googled the error and did not find a solution. I suggest you to include details about the PHP script. – grrigore Nov 17 '18 at 07:57
  • I added php script into question – ofevy Nov 17 '18 at 07:59
  • @Adrian27045 you should not edit a question that was answered. I think the error you got it's more retaled to php. You're php script should return a valid JSON object. Try to figure it out and if you don't succeed post another question. [Here](https://json.org/example.html) are some JSON examples. – grrigore Nov 17 '18 at 08:08