Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-546

FactType.get/set throws an NPE for unknown fields

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved (View Workflow)
    • Major
    • Resolution: Done
    • 6.1.0.CR1
    • 6.1.0.Final, 6.2.0.Beta1
    • None
    • None
    • Hide

      String drl = "package org.test; " +

      // existing java class
      "declare Pet" +
      " " +
      "end \n" +

      "";
      KieServices kieServices = KieServices.Factory.get();
      KieFileSystem kfs = kieServices.newKieFileSystem();
      kfs.write( kieServices.getResources().newByteArrayResource( drl.getBytes() )
      .setSourcePath( "test.drl" )
      .setResourceType( ResourceType.DRL ) );
      KieBuilder kieBuilder = kieServices.newKieBuilder( kfs );
      kieBuilder.buildAll();

      assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) );
      KieBase kieBase = kieServices.newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase();

      FactType factType = kieBase.getFactType("org.test", "Pet");
      Object instance = factType.newInstance();

      factType.get(instance, "unknownField"); //throws NullPointerException

      Show
      String drl = "package org.test; " + // existing java class "declare Pet" + " " + "end \n" + ""; KieServices kieServices = KieServices.Factory.get(); KieFileSystem kfs = kieServices.newKieFileSystem(); kfs.write( kieServices.getResources().newByteArrayResource( drl.getBytes() ) .setSourcePath( "test.drl" ) .setResourceType( ResourceType.DRL ) ); KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ); kieBuilder.buildAll(); assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); KieBase kieBase = kieServices.newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase(); FactType factType = kieBase.getFactType("org.test", "Pet"); Object instance = factType.newInstance(); factType.get(instance, "unknownField"); //throws NullPointerException

    Description

      WARNING : the committed fix does not implement the behavior intended by the title or description : the code now simply ignore without any error the unknown field
      [EDIT: I have updated the title to reflect the original problem, not the proposed solution(s).
      After the discussion below, we decided to prevent the NPE but not to throw a different exception. The KieBase.getFactType( String pkg, String name ) will return
      null in case the fact type is not defined. The client can do a null check to determined whether the type is valid or not. --Davide]

      factType.get(instance, "unknownField") is currently throwing an NPE. This gives improper information to the user/developper.
      I propose that it throws a new UnknownFactFieldException giving improved information giving good hints for debugging or enabling specific exception management.

      See pull request for test-case and proposed fix

      Attachments

        Activity

          People

            mfusco@redhat.com Mario Fusco
            bvoisin Benoit Voisin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: