2

First I was generated an ontology inside a method1. it is successful. Then inside the second method I need to use this generated ontology for another process. I used following code. It gave an error:

Exception in thread "main" java.lang.NullPointerException.

Where is the problem? How I need to take generated ontology in method1 to method2?

     public class OntologyCreation {

        public static void main (String args[]) {

        //main
        OntologyCreation ont = new OntologyCreation(); 
        OntModel m = null;
        String ontoClass = null;
        ont.method2(ontoClass, m);


        public  void method1(OntModel m) {     //for ontologyCreation
        m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
        -----
        -----
        }


        public ArrayList<String> method2(String ontoClass, OntModel m) {   // 2nd method for use generated ontology to another process
        method1(m);
        m.read("http://localhost/myont/ont.owl");      ????????
        ExtendedIterator<OntClass> classes = ((OntModel) m).listClasses(); 
        ----------
        ----------               
         }
        return xx; 
        }
KoliaX
  • 78
  • 1
  • 8
Emalka
  • 381
  • 2
  • 4
  • 16

4 Answers4

2

If you use the OntModel more than once with the OntologyCreation instance, you could use a field to store the variable:

private OntModel m;

public void method1() {
    this.m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
}

public ArrayList<String> method2(String ontoClass) {
    method1();
    this.m.read("http://localhost/myont/ont.owl"); // use field value

The alternative is returning the created OntModel as described in the other answers.

Note: Java is always pass-by-value, which is why the value in the method2 method remains null, see Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114
1

You cannot change variables outside of a function by assigning to the name of the parameter inside the function. The only thing this does is change the local variable in question. So the assignment to m in method1 sets the local variable m correctly, but that does not effect the variable m in method2.

The solution is to make method1 return the object and then assigning it to the local variable: m = method1()

Kiskae
  • 24,655
  • 2
  • 77
  • 74
0

Return m in method1:

public OntModel method1(OntModel m) {     //for ontologyCreation
    m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    /*-----*/
    return m;
}

And in method2, the call should be:

m = method1(m);

Java passes references by value, meaning method1 receives an adress of object m. However, m = something means you change that address value inside method1. Outside, it is still the same and still points towards a null.

BlueMoon93
  • 2,910
  • 22
  • 39
0

You m variable isn't being instantiated before method2is called.

Therefore it has the value null associated.

Even though you're calling method1, that attribution will only be valid on the method's scope.

You ought to do OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); before ont.method2(ontoClass, m); in order to be able to use m on method2.

António Ribeiro
  • 4,129
  • 5
  • 32
  • 49