使用 BIMserver Java 客户端检索类的所有实例时出现空指针异常 [英] Null pointer exception while retrieving all instances of a class with the BIMserver Java client

查看:31
本文介绍了使用 BIMserver Java 客户端检索类的所有实例时出现空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 BIMserver 非常陌生,我正在尝试使用 Java 客户端库和 IfcModelInterface 获取我签入的 IFC 特定类的实例.

I am very new to BIMserver and I am trying to get instances of a specific class of the IFC I have checked in, using the Java client library and IfcModelInterface.

这是一段代码:

IfcModelInterface model = client.getModel(project, project.getLastRevisionId(),false, true,true);
Collection<IfcProduct> products = model.getAllWithSubTypes(IfcProduct.class);

getAllWithSubtypes 的调用导致空指针异常.当我调试时,它会转到以下课程:

The call to getAllWithSubtypes results in a null pointer exception. When I debug it goes to the class where :

public <T extends IdEObject> List<T> getAllWithSubTypes(EClass eClass) {
    if (!loadedClasses.contains(eClass.getName()) && modelState != ModelState.FULLY_LOADED) {

eClass 为空,因此出现异常,我不明白为什么?

eClass is null and hence I get an exception, I don't understand why?

推荐答案

看看你的堆栈跟踪,我假设这一行是 Connecting.java:48

Looking at your stacktrace, I assume this line is Connecting.java:48

Collection<IfcProduct> products = model.getAllWithSubTypes(IfcProduct.class);

这会调用以下方法(IfcModel.java:310)

public <T extends IdEObject> List<T> getAllWithSubTypes(Class<T> interfaceClass)  {
    return getAllWithSubTypes(packageMetaData.getEClass(interfaceClass));
}

然后当 eClass.getName() 被调用时我们来到 NullPointer (ClientIfcModel.java:582)

And then we come to the NullPointer when eClass.getName() is called in (ClientIfcModel.java:582)

public <T extends IdEObject> List<T> getAllWithSubTypes(EClass eClass) {
    if (!loadedClasses.contains(eClass.getName()) && modelState != ModelState.FULLY_LOADED) {
    ...
}

您传入一个普通的 Java Class interfaceClass,它被映射到一个 EMF EClass 以检索其所有实例.此映射在packageMetaData.getEClass(interfaceClass) 中执行.仅当您传入的 Class interfaceClass 与模型的 packageMetaData 属于相同的 IFC 架构版本时,它才有效.

You pass in an ordinary Java Class interfaceClass which gets mapped into an EMF EClass in order to retrieve all its instances. This mapping is carried out in packageMetaData.getEClass(interfaceClass). It only works if the Class interfaceClass that you pass in pertains to the same IFC schema version as the model's packageMetaData.

例如,假设您请求的 interfaceClass 是 org.bimserver.models.ifc4.IfcProduct 而您的 model.getPackageMetaData().getSchema()Schema.IFC2X3TC1,那么映射将返回一个 EClass null,随后您将看到 NullPointer.

For instance, let's say your requested interfaceClass is org.bimserver.models.ifc4.IfcProduct and your model.getPackageMetaData().getSchema() is Schema.IFC2X3TC1, then the mapping will return an EClass null and you will subsequently see the NullPointer.

为了防止 NullPointer 异常,您必须对模型的架构进行运行时检查,并且仅在架构符合您的预期时才请求实例.

To prevent the NullPointer exception, you would have to do a runtime check of the model's schema and only request the instances if the schema is what you expect.

这篇关于使用 BIMserver Java 客户端检索类的所有实例时出现空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆