sumesh sumesh - 2 months ago 27
Android Question

Android- Retrofit - java.lang.NullPointerException: Attempt to invoke virtual method

I am trying to parse following JSON using Retrofit in android.

{
"message": false,
"suggestions": false,


"vehicle": {
"parked": true,
"uin": "15",
"vin": "WBAEG1312MCB42267",
"make": "Bmw",
"model": "E8SERIES",
"color": "Blue",
"year": "1991",
"package": "Premium",
"options": "",
"interior": "Color: Cream, Type:Leather",
"exterior": "",
"activity": "Parked",
"username": "Dhruba Sarma",
"timestamp": "04-Sep, 00:35",
"latlng": {
"lat": 12.899270164792,
"lng": 77.646080134509
}
}
}


I have created my model classes as follows - VehicleModel.java

public class VehicleModel {


@SerializedName("message")
@Expose
private boolean message;
@SerializedName("suggestions")
@Expose
private boolean suggestions;
@SerializedName("vehicle")
@Expose
private Vehicle vehicle;

public boolean isMessage() {
return message;
}

public boolean isSuggestions() {
return suggestions;
}

public Vehicle getVehicle() {
return vehicle;
}
}
Vehicle.java
public class Vehicle {

@SerializedName("parked")
@Expose
private boolean parked;
@SerializedName("uin")
@Expose
private String uin;
@SerializedName("vin")
@Expose
private String vin;
@SerializedName("make")
@Expose
private String make;
@SerializedName("model")
@Expose
private String model;
@SerializedName("color")
@Expose
private String color;
@SerializedName("year")
@Expose
private String year;
@SerializedName("package")
@Expose
private String _package;
@SerializedName("options")
@Expose
private String options;
@SerializedName("interior")
@Expose
private String interior;
@SerializedName("exterior")
@Expose
private String exterior;
@SerializedName("activity")
@Expose
private String activity;
@SerializedName("username")
@Expose
private String username;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("latlng")
@Expose
private LatLng latlng;



public boolean isParked() {
return parked;
}

public String getUin() {
return uin;
}

public String getVin() {
return vin;
}

public String getMake() {
return make;
}

public String getModel() {
return model;
}

public String getColor() {
return color;
}

public String getYear() {
return year;
}

public String getPackage() {
return _package;
}

public String getOptions() {
return options;
}

public String getInterior() {
return interior;
}

public String getExterior() {
return exterior;
}

public String getActivity() {
return activity;
}

public String getUsername() {
return username;
}

public String getTimestamp() {
return timestamp;
}

public LatLng getLatlng() {
return latlng;
}
}`

**LatLng.java**

public class LatLng {


@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lng")
@Expose
private String lng;

public String getLat() {
return lat;
}

public String getLng() {
return lng;
}
}

**VehicleJSONResponse.java**

public class VehicleJSONResponse {

private VehicleModel vehicle;

public VehicleModel getVehicleJSONResponse() {
return vehicle;
}
}


And here is how i am parsing
JSON


Retrofit retrofit = new Retrofit.Builder().baseUrl(baseurl)
.addConverterFactory(GsonConverterFactory.create()).build();

VehicleRequestInterface request = retrofit.create(VehicleRequestInterface.class);
Call<VehicleJSONResponse> call = request.getVehicleJSON(url);
call.enqueue(new Callback<VehicleJSONResponse>() {
@Override
public void onResponse(Call<VehicleJSONResponse> call, Response<VehicleJSONResponse> response) {

VehicleJSONResponse jsonResponse = response.body();

vehicleData = jsonResponse.getVehicleJSONResponse();`


Now the problem is when i try to retrieve the data using below code, i get the value of message and suggestions as expected. But i get
vehicle=null

Code to retrieve data
vehicleData.getVehicle()
>>> This is always null
vehicleData.isMessage()
>> This is false as expected.

Can anyone please help me here and suggest what am i doing wrong ?

Answer

You're getting response of VehicleModel json from server. So replace VehicleJsonResponse in VehicleRequestInterface with VehicleModel and it should work as expected.