0

I try to remove an existing individual from my RDF file, but I always get a NullPointerException. The Individual I am getting is there, because I can print out a property. Can someone please tell me what I have done wrong?

final static String SN = "http://vaceta.pavo.uberspace.de/RDF/social-media.owl#";

InputStream in = context.getResourceAsStream("WEB-INF/resources/social-media.rdf");
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
model.read(in, null);
in.close();

Individual user01 = model.getIndividual(SN + "User01");
model.remove(user01, null, (RDFNode) null);
model.remove(null, null, user01);

output = new FileOutputStream(context.getRealPath("/")+"WEB-INF/resources/social-media.rdf");
model.writeAll(output, "RDF/XML", null);
output.close();

stacktrace:

enter image description here

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
klanm
  • 3,168
  • 3
  • 19
  • 22
  • 1
    Please add the stacktrace for the NPE to your question. – forgivenson Apr 25 '14 at 12:48
  • NullPointerException at which line ? – Jay Apr 25 '14 at 12:55
  • in this line: model.remove(user01, null, (RDFNode) null); – klanm Apr 25 '14 at 13:03
  • You give 2 null references into a method and then you're suprised by an NPE? I think you should tell us why this should not lead to a NPE. – Bernd Ebertz Apr 25 '14 at 13:57
  • Sorry I am new in Jena and it is very confusing to me. I learned, that the 3 arguments are like triples, but what should I enter, if I want to delete the entire individual? thank you – klanm Apr 25 '14 at 14:03
  • @SirRotN In many of Jena's API methods, `null` is a used as wildcard in triples. E.g., `listStatements( x, null, null )` provides all triples whose subject is `x`; `remove( null, x, null )` would remove all triples whose predicate is `x`, and so on. – Joshua Taylor Apr 25 '14 at 14:11
  • 1
    @vacetahanna You're on the right track. Sir RotN's comment is not terrible Java advice in general, but Jena's API *does* use `null` as a wildcard in triple patterns, so your use is sensible. – Joshua Taylor Apr 25 '14 at 14:12
  • @JoshuaTaylor Suprising. As far as I can see the only implementation I fond looks like graph.delete( Triple.create( s.asNode(), p.asNode(), o.asNode() ) ); it calls asNode() on all the parameters. Sure that the wildcard thinking applies here. (Code might be old just the first I found) – Bernd Ebertz Apr 25 '14 at 14:18
  • 1
    @SirRotN Yeah, the code here isn't right; I was just pointing out that it's not entirely mistaken. I think what OP wants here is [`removeAll(s,p,o)`](https://jena.apache.org/documentation/javadoc/jena/com/hp/hpl/jena/rdf/model/Model.html). – Joshua Taylor Apr 25 '14 at 14:20

1 Answers1

3

You're using ModelCon.remove(s,p,o) which doesn't allow nulls. You want to use Model.removeAll(s,p,o) instead which does allow nulls and uses them as wildcards. Here are the corresponding Javadocs (with emphasis added):

ModelCon.remove

Model remove(Resource s, Property p, RDFNode o)

remove the statement (s, p, o) from this model and answer this model. None of s, p, o are permitted to be null: for wildcard removal, see removeAll.

Model.removeAll

Model removeAll(Resource s, Property p, RDFNode r)

Remove all the statements matching (s, p, o) from this model.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353