Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java object getClass() method returns null

My understanding is if a java object xx is not null there must be corresponding Class object for that object that can get using getClass() method, but some time this method returns null at run time that is very strange.

Example :

The object (oldFact) is serializble object that is referenced in ExternalFactUpdateDroolsEvent class as blew, this call get serialized and deserilized in it's excution process, means first many objects of type ExternalFactUpdateDroolsEvent get serialized and latter get desterilized before execution, in this process some time oldFact object returns null Class object that is causing problem.

My question is if an object is not null how getClass() method returns null? Is this some thing related to deserilized object that in not properly initializing / instantiating or some thing else, these object might be getting serialized from different JVM and then getting executed on one JVM.

public class ExternalFactUpdateDroolsEvent implements DroolsEvent {

    private static final long serialVersionUID = -8225631607832350264L;

    private Object oldFact;

    public ExternalFactUpdateDroolsEvent(Object oldFact) {
        this.oldFact = oldFact;
        this.updatedFact = updatedFact;
    }

    public void executeAction(StatefulKnowledgeSession ksession) {
        // some time getClass() returns null that is strange  
        Class factClass = oldFact.getClass();

        ........................
    }

    ......................
}

Adding more content:

I am sure oldFact is not null because no nullPointer exception on oldFact.getClass(); call, the Class objecr factClass is being passed to other mentod where null is being asserted that's where we exception is throwing. See the code and exception as below. I appreciate your help in advance.

like image 487
brjsingh Avatar asked Dec 06 '25 10:12

brjsingh


1 Answers

Let me guess that you receive a NullPointerException in this line:

Class factClass = oldFact.getClass();

Due to the fact, that getClass() can't return null (EDIT: Even if getClass() would return null, that wouldn't cause a NullPointerException), oldFact must be the one which is null.

like image 167
Sibbo Avatar answered Dec 08 '25 17:12

Sibbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!