title: InputStream.getResourceAsStream() giving null pointer exception
body: <p>The line <code>persistenceProperties.load(is);</code> is throwing a <code>nullpointerexception</code> in the following method.  <strong>How can I resolve this error?</strong>  </p>
  
  <pre><code>public void setUpPersistence(){
  
      final Properties persistenceProperties = new Properties();
      InputStream is = null;
      try {
          is = getClass().getClassLoader().getResourceAsStream("src/test/samples/persistence.properties");
          persistenceProperties.load(is);
      }catch (IOException ignored) {}
      finally {
          if (is != null) {try {is.close();} catch (IOException ignored) {}}
      }
  
      entityManagerFactory = Persistence.createEntityManagerFactory(
          "persistence.xml", persistenceProperties);
      }  
  </code></pre>
  
  <p>I have tried to experiment with this by moving the class that contains the method to various other locations within the application structure, and also by changing the line of code preceding the error in the following ways:  </p>
  
  <pre><code>is = getClass().getClassLoader().getResourceAsStream("persistence.properties");
  is = getClass().getClassLoader().getResourceAsStream("/persistence.properties");
  is = getClass().getClassLoader().getResourceAsStream("/src/test/samples/persistence.properties");
  is = getClass().getClassLoader().getResourceAsStream("other/paths/after/moving/persistence.properties");
  </code></pre>
  
  <p>But the error is still thrown every time the method is called.  </p>
  
  <p>Here is a printscreen of the directory structure of the eclipse project.  The class containing the method is called <code>TestFunctions.java</code>, and the location of <code>persistence.properties</code> is shown:  </p>
  
  <p><a href="https://i.stack.imgur.com/VlWrB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VlWrB.jpg" alt=""></a>
    </p>
  
  <hr>
  
  <h2>**EDIT:  **</h2>
  
  <p>As per feedback below, I changed the method to:  </p>
  
  <pre><code>public void setUpPersistence(){
      final Properties persistenceProperties = new Properties();
      InputStream is = null;
      try {
          is = getClass().getClassLoader().getResourceAsStream("persistence.properties");
          persistenceProperties.load(is);
      }catch (IOException i) {i.printStackTrace();}
      finally {
          if (is != null) {try {is.close();} catch (IOException ignored) {}}
      }
      entityManagerFactory = Persistence.createEntityManagerFactory(
          "persistence.xml", persistenceProperties);
  }  
  </code></pre>
  
  <p>I also moved mainTest.TestFunctions.java to <code>src/test/java</code>.  Together, these all cause the following new stack trace:  </p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: maintest/TestFunctions
      at maintest.Main.main(Main.java:7)
  Caused by: java.lang.ClassNotFoundException: maintest.TestFunctions
      at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
      ... 1 more  
  </code></pre>