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:
首先,我在method1中生成了一个本体。它很成功。然后在第二种方法中,我需要将这个生成的本体用于另一个进程。我使用了以下代码。它给出了一个错误:
Exception in thread "main" java.lang.NullPointerException.
线程“main”java.lang.NullPointerException中的异常。
Where is the problem? How I need to take generated ontology in method1
to method2
?
哪里有问题?我如何将方法1中的生成本体带到方法2?
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;
}
4 个解决方案
#1
2
If you use the OntModel
more than once with the OntologyCreation
instance, you could use a field to store the variable:
如果您使用OntologyCreation实例多次使用OntModel,则可以使用字段来存储变量:
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.
另一种方法是返回创建的OntModel,如其他答案中所述。
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"?
注意:Java总是按值传递,这就是为什么method2方法中的值保持为null,请参阅Java是“传递引用”还是“按值传递”?
#2
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
.
您不能通过指定函数内部参数的名称来更改函数外部的变量。这样做的唯一方法是更改有问题的局部变量。因此,在method1中对m的赋值可以正确设置局部变量m,但这不会影响method2中的变量m。
The solution is to make method1
return the object and then assigning it to the local variable: m = method1()
解决方案是让method1返回对象,然后将其分配给局部变量:m = method1()
#3
0
Return m
in method1
:
在method1中返回m:
public OntModel method1(OntModel m) { //for ontologyCreation
m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
/*-----*/
return m;
}
And in method2
, the call should be:
在方法2中,调用应该是:
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
.
Java按值传递引用,这意味着method1接收对象m的地址。但是,m =某事意味着您在method1中更改了该地址值。在外面,它仍然是相同的,仍然指向null。
#4
0
You m
variable isn't being instantiated before method2
is called.
在调用method2之前,没有实例化变量。
Therefore it has the value null
associated.
因此它具有null关联值。
Even though you're calling method1
, that attribution will only be valid on the method's scope.
即使您正在调用method1,该归因也只对方法的范围有效。
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
.
你应该做OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);在ont.method2之前(ontoClass,m);为了能够在方法2上使用m。
#1
2
If you use the OntModel
more than once with the OntologyCreation
instance, you could use a field to store the variable:
如果您使用OntologyCreation实例多次使用OntModel,则可以使用字段来存储变量:
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.
另一种方法是返回创建的OntModel,如其他答案中所述。
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"?
注意:Java总是按值传递,这就是为什么method2方法中的值保持为null,请参阅Java是“传递引用”还是“按值传递”?
#2
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
.
您不能通过指定函数内部参数的名称来更改函数外部的变量。这样做的唯一方法是更改有问题的局部变量。因此,在method1中对m的赋值可以正确设置局部变量m,但这不会影响method2中的变量m。
The solution is to make method1
return the object and then assigning it to the local variable: m = method1()
解决方案是让method1返回对象,然后将其分配给局部变量:m = method1()
#3
0
Return m
in method1
:
在method1中返回m:
public OntModel method1(OntModel m) { //for ontologyCreation
m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
/*-----*/
return m;
}
And in method2
, the call should be:
在方法2中,调用应该是:
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
.
Java按值传递引用,这意味着method1接收对象m的地址。但是,m =某事意味着您在method1中更改了该地址值。在外面,它仍然是相同的,仍然指向null。
#4
0
You m
variable isn't being instantiated before method2
is called.
在调用method2之前,没有实例化变量。
Therefore it has the value null
associated.
因此它具有null关联值。
Even though you're calling method1
, that attribution will only be valid on the method's scope.
即使您正在调用method1,该归因也只对方法的范围有效。
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
.
你应该做OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);在ont.method2之前(ontoClass,m);为了能够在方法2上使用m。