0

I want to get my mysql data using retrofit in android but I getting a null exception. I have tested my database with postman and everything is working fine. I have seen other related answers but mine is different. My code below.

        Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(LikelyProblemsClient.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
    LikelyProblemsClient likelyProblemsClient = retrofit.create(LikelyProblemsClient.class);


    Call<List<LikelyProblems>> call = likelyProblemsClient.getProblems();

    call.enqueue(new Callback<List<LikelyProblems>>() {
        @Override
        public void onResponse(Call<List<LikelyProblems>> call, Response<List<LikelyProblems>> response) {

            List<LikelyProblems> likelyProblems = response.body();

            for (LikelyProblems l: likelyProblems){
                Log.d("id", l.getId());
                Log.d("category", l.getCategory());
                Log.d("Problems", l.getProblems());
            }


        }

        @Override
        public void onFailure(Call<List<LikelyProblems>> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT);
        }
    });

My Model

public class LikelyProblems {
private String id;
private String category;
private String problems;

public LikelyProblems(String id, String category, String problems) {
    this.id = id;
    this.category = category;
    this.problems = problems;
}

public String getId() {
    return id;
}

public String getCategory() {
    return category;
}

public String getProblems() {
    return problems;
}
}

My Interface

public interface LikelyProblemsClient {
  String BASE_URL =  "http://192.168.1.35:85/tranxavApi/public/api/";

  @GET("vehicle-problems")
  Call<List<LikelyProblems>> getProblems();
}

Please all answers will be appreciated. Thanks

oshabz
  • 129
  • 1
  • 3
  • 11
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – C-Otto Jan 30 '18 at 13:00

2 Answers2

0

I have solved the problem. I was using a post method instead of get method in my laravel backend

oshabz
  • 129
  • 1
  • 3
  • 11
0

I know it's been long time you asked this question but it may help somebody else. I was facing the same issue when I tried sending a request every time a user edited text. If you also send multiple requests then the problem occurs maybe because at some point your call returns a response which is null. Just check if your response.body is null before the for loop.

M_droid
  • 2,447
  • 2
  • 25
  • 35