简体   繁体   English

java对象的getClass()方法返回null

[英]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. 我的理解是,如果java对象xx不为null,则必须使用getClass()方法获取该对象的相应Class对象,但是有时此方法在运行时返回null却很奇怪。

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. 对象(oldFact)是可序列化的对象,在ExternalFactUpdateDroolsEvent类中被称为blew,此调用在执行过程中被序列化和反序列化,这意味着首先对类型为ExternalFactUpdateDroolsEvent多个对象进行序列化,然后在执行之前对其进行消毒,在此过程中, object返回null导致问题的Class对象。

My question is if an object is not null how getClass() method returns null ? 我的问题是,如果对象不为nullgetClass()方法如何返回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. 这与未正确初始化/实例化的反序列化对象有关吗,或其他原因,这些对象可能已从不同的JVM进行序列化,然后在一个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(); 我确定oldFact不为null,因为oldFact.getClass()上没有nullPointer异常; call, the Class objecr factClass is being passed to other mentod where null is being asserted that's where we exception is throwing. 调用时,将类对象事实类传递给其他断言,其中断言null就是我们抛出异常的地方。 See the code and exception as below. 请参见下面的代码和异常。 I appreciate your help in advance. 非常感谢您的帮助。

Let me guess that you receive a NullPointerException in this line: 让我猜想您在此行中收到NullPointerException:

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. 由于这个事实,getClass()不能返回null( 编辑:即使getClass()返回null,也不会导致NullPointerException),oldFact必须为null。

the whole point is, oldfact is null because when you send a handle in ExternalFactUpdateDroolsEvent you send a null pointer so oldfact remains a null object, so when you type oldfact.getclass() there is nothing to get 整体而言,oldfact为null,因为当您在ExternalFactUpdateDroolsEvent发送句柄时,您将发送null指针,因此oldfact仍为null对象,因此,当您键入oldfact.getclass() ,没有任何获取

edit-- 编辑 -

or this happens if you call method executeaction before calling ExternalFactUpdateDroolsEvent 否则,如果您在调用ExternalFactUpdateDroolsEvent之前调用方法executeaction ,则会发生这种情况

Altough a bit late to the party, I just saw this searching for the same answers. 晚会晚了一点,我只是看到了这个寻找同样答案的东西。 Shouldn't private Object oldFact; 不应该private Object oldFact; be initialized as a new Object? 初始化为新对象?

private Object oldFact;

to: 至:

private Object oldFact = new Object();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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