问题
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.
Here is the piece of code :
IfcModelInterface model = client.getModel(project, project.getLastRevisionId(),false, true,true);
Collection<IfcProduct> products = model.getAllWithSubTypes(IfcProduct.class);
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 is null and hence I get an exception, I don't understand why?
回答1:
Looking at your stacktrace, I assume this line is Connecting.java:48
Collection<IfcProduct> products = model.getAllWithSubTypes(IfcProduct.class);
This calls the following method (IfcModel.java:310)
public <T extends IdEObject> List<T> getAllWithSubTypes(Class<T> interfaceClass) {
return getAllWithSubTypes(packageMetaData.getEClass(interfaceClass));
}
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) {
...
}
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
.
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.
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.
来源:https://stackoverflow.com/questions/52246562/null-pointer-exception-while-retrieving-all-instances-of-a-class-with-the-bimser