Hi All,

I cobbled together some Jena TDB code based on stuff I found on the Web. It's basic boilerplate.

In the code below, if the dataset directory does not exist, then everything works well. However, if the dataset exists, then I get an NPE that began life at the call to dataset.getDefaultModel().

public static void main(String[] args)
{
 String directory = "c:\\semantic web\\jena\\tdb";
 Dataset dataset = TDBFactory.createDataset(directory);
 Model tdb = dataset.getDefaultModel();

Stack trace of the NPE looks like this:

Exception in thread "main" java.lang.NullPointerException
    at com.hp.hpl.jena.tdb.store.DatasetPrefixesTDB.readPrefixMap(DatasetPrefixesTDB.java:173)
    at com.hp.hpl.jena.sparql.graph.GraphPrefixesProjection.getNsPrefixMap(GraphPrefixesProjection.java:62)
    at com.hp.hpl.jena.tdb.store.DatasetPrefixesTDB.getPrefixMapping(DatasetPrefixesTDB.java:222)
    at com.hp.hpl.jena.tdb.store.DatasetPrefixesTDB.getPrefixMapping(DatasetPrefixesTDB.java:213)
    at com.hp.hpl.jena.tdb.store.GraphTriplesTDB.createPrefixMapping(GraphTriplesTDB.java:92)
    at com.hp.hpl.jena.sparql.graph.GraphBase2.getPrefixMapping(GraphBase2.java:194)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.getPrefixMapping(ModelCom.java:908)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.withDefaultMappings(ModelCom.java:952)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.<init>(ModelCom.java:66)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.<init>(ModelCom.java:62)
    at com.hp.hpl.jena.rdf.model.ModelFactory.createModelForGraph(ModelFactory.java:166)
    at com.hp.hpl.jena.sparql.core.DatasetImpl.graph2model(DatasetImpl.java:266)
    at com.hp.hpl.jena.sparql.core.DatasetImpl.getDefaultModel(DatasetImpl.java:102)
    at query.TestTdb.main(TestTdb.java:26)

Can someone explain what's causing this? I'm stumped. Oh, Jena version is 2.7.4.

Thanks.

-Saul

asked 27 Nov '12, 13:33

SaulTocsin's gravatar image

SaulTocsin
13615
accept rate: 0%


At a guess, it looks like the previous use of the directory was not shutting the store down properly, leaving the on disk files inconsistent.

What is the rest of the code doing, when the store is new?

Call TDB.sync(dataset) before exiting or, ideally, use transactions.

link

answered 28 Nov '12, 04:28

AndyS's gravatar image

AndyS ♦
13.0k37
accept rate: 32%

Thanks, Andy. I will soon check the code and get back to you.

(28 Nov '12, 10:30) SaulTocsin SaulTocsin's gravatar image

Andy,

Sorry, but I am today unable to reproduce this problem. I restored code to its original state and ran it several times both with and without an extant TDB. Works fine.

It is entirely likely that I was closing only the QueryExecutionFactory and not the dataset. I certainly didn't have the TDB.sync(dataset) that you recommended. I've now added these to the code.

Thanks for your help.

-Saul

(28 Nov '12, 12:33) SaulTocsin SaulTocsin's gravatar image

It will be to do with update and adding data - not query. Query does not change the database.

(28 Nov '12, 13:44) AndyS ♦ AndyS's gravatar image

Understood, thanks.

(28 Nov '12, 14:56) SaulTocsin SaulTocsin's gravatar image

Try starting a transaction before calling:

dataset.getDefaultModel();

In other words something like this:

public static void main(String[] args)
{
 String directory = "c:\\semantic web\\jena\\tdb";
 Dataset dataset = TDBFactory.createDataset(directory);
 dataset.begin( ReadWrite.WRITE );
 Model tdb = dataset.getDefaultModel();

 // do stuff with the model here

 dataset.end();
}

Wrap all of this in a try/catch block with a finally block that closes the dataset to prevent an unclosed dataset from corrupting your db.

link

answered 10 May, 19:30

sprhodes's gravatar image

sprhodes
112
accept rate: 0%

TDB will work without transactions (not to be encouraged - you loose the robustness and recovery) and dataset.getDefaultModel() should return a model. No need to .close() when using transactions. You need dataset.commit(). After .commit returns the changes are permanent and not lost.

(11 May, 05:25) AndyS ♦ AndyS's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×578
×60

Asked: 27 Nov '12, 13:33

Seen: 1,569 times

Last updated: 11 May, 05:25