1

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?

hlg
  • 1,321
  • 13
  • 29
Shubham
  • 997
  • 1
  • 9
  • 15
  • What library are you using (IfcOpenShell, xBIM ...)? – Loebl Sep 10 '18 at 07:38
  • bimserverclientlib 1.5.51, pluginbase 1.5.51 – Shubham Sep 10 '18 at 11:49
  • Thanks. From what I can see the code should work. Maybe the root of the problem is somewhere else? Do you have access to any logs, can you look if there are any warnings or errors noted? – Loebl Sep 10 '18 at 12:06
  • i don't know how to access logs, but the exception i get is : Exception in thread "main" java.lang.NullPointerException at org.bimserver.client.ClientIfcModel.getAllWithSubTypes(ClientIfcModel.java:582) at org.bimserver.ifc.IfcModel.getAllWithSubTypes(IfcModel.java:310) at com.bim.poc.demo.Connecting.main(Connecting.java:48) – Shubham Sep 10 '18 at 12:57

1 Answers1

0

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.

hlg
  • 1,321
  • 13
  • 29
  • thanks for the input , what you said could be the cause also but i found the root cause for my problem. as EClass and Class are not the same thing. So the mapping from EClass to Class is not working correctly. some how i discontinued on this topic but this is the reason for the failure of my code. – Shubham Oct 23 '18 at 06:05
  • 1
    Exactly, the mapping is carried out in `packageMetaData.getEClass(interfaceClass)` and 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. – hlg Oct 23 '18 at 08:54
  • your comment is the solution so i have edited the answer and marked this as an answer, thanks for the input. – Shubham Oct 25 '18 at 06:33