I am facing trouble to parse json using Retrofit. i called web api, got 200 Status code, but retrofit give me NullObject Exception. Is there any other solution it would be appreciated. 1) Retrofit 1.9.0 and 2) Retrofit 2.0.2
my json
{
"$id": "1",
"objpo": {
"$id": "2",
"PODetail": [{
"$id": "3",
"PurchaseOrderDetailId": 50003,
"PurchaseOrderId": 0
}],
"POAttachFiles": [{
"$id": "4",
"Attachmentid": 33277,
"AttachFile": "createpo.png"
}],
"StatusName": null,
"Country": "UK"
}
}
POJO Class from http://www.jsonschema2pojo.org/
-----------------------------------com.Model.Objpo.java-----------------------------------
package com.Model;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Objpo {
@SerializedName("$id")
@Expose
private String $id;
@SerializedName("PODetail")
@Expose
private List<com.Model.PODetail> PODetail = new ArrayList<com.Model.PODetail>();
@SerializedName("POAttachFiles")
@Expose
private List<POAttachFile> POAttachFiles = new ArrayList<POAttachFile>();
@SerializedName("StatusName")
@Expose
private Object StatusName;
@SerializedName("Country")
@Expose
private String Country;
/**
*
* @return
* The $id
*/
public String get$id() {
return $id;
}
/**
*
* @param $id
* The $id
*/
public void set$id(String $id) {
this.$id = $id;
}
/**
*
* @return
* The PODetail
*/
public List<com.Model.PODetail> getPODetail() {
return PODetail;
}
/**
*
* @param PODetail
* The PODetail
*/
public void setPODetail(List<com.Model.PODetail> PODetail) {
this.PODetail = PODetail;
}
/**
*
* @return
* The POAttachFiles
*/
public List<POAttachFile> getPOAttachFiles() {
return POAttachFiles;
}
/**
*
* @param POAttachFiles
* The POAttachFiles
*/
public void setPOAttachFiles(List<POAttachFile> POAttachFiles) {
this.POAttachFiles = POAttachFiles;
}
/**
*
* @return
* The StatusName
*/
public Object getStatusName() {
return StatusName;
}
/**
*
* @param StatusName
* The StatusName
*/
public void setStatusName(Object StatusName) {
this.StatusName = StatusName;
}
/**
*
* @return
* The Country
*/
public String getCountry() {
return Country;
}
/**
*
* @param Country
* The Country
*/
public void setCountry(String Country) {
this.Country = Country;
}
}
-----------------------------------com.Model.POAttachFile.java-----------------------------------
package com.Model;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class POAttachFile {
@SerializedName("$id")
@Expose
private String $id;
@SerializedName("Attachmentid")
@Expose
private Integer Attachmentid;
@SerializedName("AttachFile")
@Expose
private String AttachFile;
/**
*
* @return
* The $id
*/
public String get$id() {
return $id;
}
/**
*
* @param $id
* The $id
*/
public void set$id(String $id) {
this.$id = $id;
}
/**
*
* @return
* The Attachmentid
*/
public Integer getAttachmentid() {
return Attachmentid;
}
/**
*
* @param Attachmentid
* The Attachmentid
*/
public void setAttachmentid(Integer Attachmentid) {
this.Attachmentid = Attachmentid;
}
/**
*
* @return
* The AttachFile
*/
public String getAttachFile() {
return AttachFile;
}
/**
*
* @param AttachFile
* The AttachFile
*/
public void setAttachFile(String AttachFile) {
this.AttachFile = AttachFile;
}
}
-----------------------------------com.Model.PODetail.java-----------------------------------
package com.Model;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class PODetail {
@SerializedName("$id")
@Expose
private String $id;
@SerializedName("PurchaseOrderDetailId")
@Expose
private Integer PurchaseOrderDetailId;
@SerializedName("PurchaseOrderId")
@Expose
private Integer PurchaseOrderId;
/**
*
* @return
* The $id
*/
public String get$id() {
return $id;
}
/**
*
* @param $id
* The $id
*/
public void set$id(String $id) {
this.$id = $id;
}
/**
*
* @return
* The PurchaseOrderDetailId
*/
public Integer getPurchaseOrderDetailId() {
return PurchaseOrderDetailId;
}
/**
*
* @param PurchaseOrderDetailId
* The PurchaseOrderDetailId
*/
public void setPurchaseOrderDetailId(Integer PurchaseOrderDetailId) {
this.PurchaseOrderDetailId = PurchaseOrderDetailId;
}
/**
*
* @return
* The PurchaseOrderId
*/
public Integer getPurchaseOrderId() {
return PurchaseOrderId;
}
/**
*
* @param PurchaseOrderId
* The PurchaseOrderId
*/
public void setPurchaseOrderId(Integer PurchaseOrderId) {
this.PurchaseOrderId = PurchaseOrderId;
}
}
-----------------------------------com.Model.PODetailMaster.java-----------------------------------
package com.Model;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class PODetailMaster {
@SerializedName("$id")
@Expose
private String $id;
@SerializedName("objpo")
@Expose
private Objpo objpo;
/**
*
* @return
* The $id
*/
public String get$id() {
return $id;
}
/**
*
* @param $id
* The $id
*/
public void set$id(String $id) {
this.$id = $id;
}
/**
*
* @return
* The objpo
*/
public Objpo getObjpo() {
return objpo;
}
/**
*
* @param objpo
* The objpo
*/
public void setObjpo(Objpo objpo) {
this.objpo = objpo;
}
}
POAPI.java
@FormUrlEncoded
@Headers("Accept:application/json")
@POST(ConstantValues.PO_DETAILS_ENDPOINT) // my web api's url
void PoDetailListService(
@Field("PurchaseOrderIdEyrypt") String poIdEncrypt,
Callback<PODetailMaster> response
);
DashBoard.java ( show output )
public void PODeatilCall(final Context mContext, String PoEncrypId) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(ConstantValues.HOST_NAME)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
POAPI objPOAPI = restAdapter.create(POAPI.class);
final StringBuffer sb=new StringBuffer();
objPOAPI.PoDetailListService(PoEncrypId, new Callback<PODetailMaster>() {
@Override
public void success(PODetailMaster objPurchaseOrder, Response
response) {
Log.i("StatusCode",""+response.getStatus());
Objpo objPo=objPurchaseOrder.getObjpo();
//-----------------objPo is null here-------------------//
String country=objPo.getCountry(); //<---getting null object
Log.i("PO",country);
for(PODetail objPoDetail:objPo.getPODetail()){
Log.i("ID:",""+objPoDetail.getPurchaseOrderDetailId());
}
}
@Override
public void failure(RetrofitError error) {
dialog.dismiss();
Toast.makeText(mContext, error.toString(),
Toast.LENGTH_SHORT).show();
sb.append("Network Error");
}
});
}
Exception::
05-17 16:36:14.435 31529-31529/com.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test, PID: 31529
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.test.Model.PoDetails2.Objpo.getCountry()' on a null object reference
at com.test.Utils.Common$2.success(Common.java:169)
at com.test.Utils.Common$2.success(Common.java:158)
at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Retrofit version 2.0.2
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
}
public interface POAPI {
//Testing
@FormUrlEncoded
@Headers("Accept:application/json")
@POST("PODetails")
Call<PODetailMaster> getPoDetailList(@Field("PurchaseOrderIdEyrypt") String poIdEncrypt);
}
public void PODeatilCallTest(final Context mContext, String PoEncrypId) {
final StringBuffer sb=new StringBuffer();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://host_url/api/PurchaseOrder/")
.addConverterFactory(GsonConverterFactory.create())
.build();
POAPI objPOAPI = retrofit.create(POAPI.class);
Call<PODetailMaster> objCall=objPOAPI.getPoDetailList(PoEncrypId);
objCall.enqueue(new Callback<PODetailMaster>() {
@Override
public void onResponse(Call<PODetailMaster> call, Response<PODetailMaster> response) {
Log.i("Code",""+response.code());
PODetailMaster objPoDetailMaster=response.body();
Objpo objPo=objPoDetailMaster.getObjpo();
String country=objPo.getCountry(); //<---getting null object
Log.i("PO",country);
for(PODetail objPoDetail:objPo.getPODetail()){
Log.i("ID:",""+objPoDetail.getPurchaseOrderDetailId());
}
}
@Override
public void onFailure(Call<PODetailMaster> call, Throwable t) {
}
});
}