1

Ok, last time I asked this question, it was downvoted by 3 people before I deleted it. May be it wasn't clear, sorry my fault. So I am using retrofit to make api hits. The api returns a JSON which has a field data which can be null. SO the JSON can be something like this.

{
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object"
    "data":null
}

When it isn't null it will be something like

{
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object"
    "data":{"b":"xyz","a":123}
}

Now I have a model class for this object like this

public class A {

    @Expose
    public String title;

    @Expose
    public String description;

    @Expose
    public String type;

    @Expose
    public Data data =new Data();

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

}

And this is the model of Data

public class Data {

    public Data(){
        this.a=0;
        this.b="";
    }

    @Expose
    public double a;

    @Expose
    public String b="";


    public Double getA() {
        return a;
    }

    public void setA(Double a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

}

Retrofit will convert the JSON into a Java object of type A. Now the next thing that I do with this object of type A is to convert it into another object of Type B.

For doing the conversion I used a utility class. Its description is below

This utility converts one java object to another java object.
     * does not support Inheritance
     * Mainly useful for when you have two Class one for Restful call and another for ORM suite
     * if you get one object from RESTFUL call and another object to be created to save in ORM then generally we
     * create another object and manually put the value by setter and getter
     * just keep the same field name in both class and use this utility  function to assign value from one to another
     * and then use another to save in db. So no more stupid getter setter use, it handles nested class and Collection field .

Now the problem that I am facing is that this class throws an exception

Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

This is the only error that I get, nothing else. The stacktrace is clean, just this error. Basically the object conversion has failed. The reson is most likely because the data field is null because prior to that I was not getting any such error.

This is a portion of code from my utility class (the one responsible for object conversion). I pass the two object types one source and the other destination. The source in this case is object of type A. But the first line cause the exception that I have mentioned in the question.

        // get the class of source object
        Class sourceObjectType = sourceObject.getClass();
        // get the class of destination object
        Class destinationObjectType = destinationObject.getClass();

Now I am not sure how to handle this exception. I tried creating a constructor so that Data is not null but this isn't working. It would be great if someone can suggest something or help me out. Thanks !!

varunkr
  • 5,364
  • 11
  • 50
  • 99
  • Obviously `null.getClass()` will throw a NPE... – Selvin Oct 06 '15 at 18:18
  • But my class is not null, the field Data is and I even tried initializing it. – varunkr Oct 06 '15 at 18:19
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Oct 06 '15 at 18:20
  • Yes I understand what a null pointer exception is and as I said I tried to initialize the Data object via the constructor. My question is why is then the exception thrown ? – varunkr Oct 06 '15 at 18:21
  • Obviously because you *attempt to invoke virtual method... on a null object*, seriously, use debugger... – Selvin Oct 06 '15 at 18:23
  • Obviously after `object.setData(null)` data is null... So constructor, yet again, obviously, has nothing to do with that – Selvin Oct 06 '15 at 18:27
  • Note that null in JSON is `null`, not `"null"`. – CommonsWare Oct 06 '15 at 18:31
  • @CommonsWare sorry I just edited that I am getting null not "null" in the JSON – varunkr Oct 06 '15 at 18:34
  • Use `Gson`, `String typeASring = gson.toString(objectOfA); ObjectOfB b = gson.fromString(typeAString, ObjectOfB.class);` From your code, i beieve you are already using Gson for setting the values from REST api call to java object. – JavaGhost Oct 06 '15 at 18:59
  • @JavaGhost yes I am already using Gson, thnx – varunkr Oct 06 '15 at 19:01

1 Answers1

1

You can check first if your destinationObjectType is null using a simple if:

    if(destinationObjectType){
        // handle your condition here
    }

or you can handle an exception:

try{
        // get the class of source object
        Class sourceObjectType = sourceObject.getClass();
        // get the class of destination object
        Class destinationObjectType = destinationObject.getClass();

}
catch(NullPointerException e){

        // Handle your exception here
}

Regards!

lgallard
  • 462
  • 3
  • 10