简体   繁体   中英

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

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 !!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM