Sunday, 15 May 2011

java - InputStream.getResourceAsStream() giving null pointer exception -



java - InputStream.getResourceAsStream() giving null pointer exception -

the line persistenceproperties.load(is); throwing nullpointerexception in next method. how can resolve error?

public void setuppersistence(){ final properties persistenceproperties = new properties(); inputstream = null; seek { = getclass().getclassloader().getresourceasstream("src/test/samples/persistence.properties"); persistenceproperties.load(is); }catch (ioexception ignored) {} { if (is != null) {try {is.close();} grab (ioexception ignored) {}} } entitymanagerfactory = persistence.createentitymanagerfactory( "persistence.xml", persistenceproperties); }

i have tried experiment moving class contains method various other locations within application structure, , changing line of code preceding error in next ways:

is = getclass().getclassloader().getresourceasstream("persistence.properties"); = getclass().getclassloader().getresourceasstream("/persistence.properties"); = getclass().getclassloader().getresourceasstream("/src/test/samples/persistence.properties"); = getclass().getclassloader().getresourceasstream("other/paths/after/moving/persistence.properties");

but error still thrown every time method called.

here printscreen of directory construction of eclipse project. class containing method called testfunctions.java, , location of persistence.properties shown:

**edit: **

as per feedback below, changed method to:

public void setuppersistence(){ final properties persistenceproperties = new properties(); inputstream = null; seek { = getclass().getclassloader().getresourceasstream("persistence.properties"); persistenceproperties.load(is); }catch (ioexception i) {i.printstacktrace();} { if (is != null) {try {is.close();} grab (ioexception ignored) {}} } entitymanagerfactory = persistence.createentitymanagerfactory( "persistence.xml", persistenceproperties); }

i moved maintest.testfunctions.java src/test/java. together, these cause next new stack trace:

exception in thread "main" java.lang.noclassdeffounderror: maintest/testfunctions @ maintest.main.main(main.java:7) caused by: java.lang.classnotfoundexception: maintest.testfunctions @ java.net.urlclassloader$1.run(urlclassloader.java:202) @ java.security.accesscontroller.doprivileged(native method) @ java.net.urlclassloader.findclass(urlclassloader.java:190) @ java.lang.classloader.loadclass(classloader.java:306) @ sun.misc.launcher$appclassloader.loadclass(launcher.java:301) @ java.lang.classloader.loadclass(classloader.java:247) ... 1 more

short answer: move persistence.properties src/main/resources, have both main.java , testfunctions.java in src/main/java, , utilize

getclass().getclassloader().getresourceasstream("persistence.properties");

to load properties file.

long reply explanation:

as others have hinted @ - in maven project structure, (typically) have 2 directory trees: /src/main , /src/test. general intent "real" code, resources, etc should go in /src/main, , items test-only should go in /src/test. when compiled , run, items in test tree have access items in main tree, since they're intended test stuff in main; items in main tree, however, not typically have access items in test tree, since it's bad thought have "production" code depending on test stuff. so, since main.java depends on testfunctions.java, , testfunctions.java depends on persistence.properties, if main in src/main both testfunctions , persistence.properties need well.

java inputstream

No comments:

Post a Comment