title: JSP NullPointer StandardWrapperValve[servlets. ]: Servlet.service() for servlet servlets. threw exception java.lang.NullPointerException
body: <p>I'm trying to learn how to work with JSP. And I encountered something, which makes no sense, at least in my opinion:</p>
  
  <p>When I try to run:</p>
  
  <pre><code>response.getWriter().println(m.getDb().printAll());
  </code></pre>
  
  <p>I get this when trying to load the page in question:</p>
  
  <pre><code>2014-08-16T03:19:59.789+0200|Warning: StandardWrapperValve[servlets.GUI]: Servlet.service() for servlet servlets.GUI threw exception java.lang.NullPointerException
  at servlets.GUI.doGet(GUI.java:39)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
  at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
  at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
  at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
  at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
  at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
  at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
  </code></pre>
  
  <p>at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      at java.lang.Thread.run(Thread.java:744)</p>
  
  <p>But when I run:</p>
  
  <pre><code>response.getWriter().println(m.getOut());
  </code></pre>
  
  <p>The output is correct.</p>
  
  <p>Both should return the same result since</p>
  
  <pre><code>class M
  {
   String out = "";
  
   private OjectDAO db = new ObjectDAO()
  
   public M()
   {
     db.addAll(TextFileLoader(getServletContext().getRealPath("textfile.xml"))); 
   }
  
  
   ...
          System.out.println(db.printAll());
          out=db.printAll();
   ...
       public String getOut()
       {
        return out;
       }
   ...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class ObjectDAO
  {
  
   ...
   public String printAll()
   {
    String result = "";
    for (WrappedItem item : locDB )
    {
     result = item.toString() + result + "\n";
    }
    return result;
   }
    ...
  }
  </code></pre>
  
  <p>Why is it, that in one case there is a NullPointer and in the other case there is none?</p>
  
  <p>More detail:
  Application runs on Glassfish 4 deployed by Eclipse Luna. OS : Debian
  The application loads text from a file to a DAO and tries to load them from the DAO into the JSP. Path to the xml seems correct.</p>
  
  <p>P.S.: At first I thought that it was a BuildPath problem, because I encountered something similar before and that was solved once I added JDom2.jar to WEB-INF/lib , but frankly I don't see where I could add a jar this time. Funny thing is: Even while having both problems, the output to STDOUT was working seamlessly. </p>
  
  <h3> Addendum 1 </h3>
  
  <p>As I stated above, my problem is:</p>
  
  <pre><code>response.getWriter().println(m.getOut());
  </code></pre>
  
  <p>works fine.</p>
  
  <pre><code>response.getWriter().println(m.getDb().printAll());
  </code></pre>
  
  <p>doesn't. </p>
  
  <p>Both lines are placed in the servlet "GUI.java" file.
  The only difference, at least in how muc I see is:
  In the first case I print the String after it passes through a String variable in class M, in the second case I try to print it directly.</p>
  
  <p>That is all the difference.</p>
  
  <h3> Addendum 2 </h3>
  
  <p>Added the full stack trace.</p>
  
  <p>Servlet: Generic code, the only difference is</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
  {
      System.out.println("Started ");
      response.getWriter().println("&lt;h1&gt; Header1 &lt;/h1&gt;");
      response.getWriter().println("&lt;body&gt; Funny body text &lt;/body&gt;");
      System.out.println(getServletContext().getRealPath("data.xml"));
      serverJava.Monitor m = new serverJava.Monitor(getServletContext().getRealPath("data.xml"));
      response.getWriter().println("&lt;br&gt; I say:" + m.printHello());
      response.getWriter().println(m.getOut());
      response.getWriter().println(m.getDb().printAll());
  }
  </code></pre>
  
  <p>Now before people start: The following code was just to test, whether everything works ok. It's not really there to stay.</p>
  
  <pre><code>public M(String s) 
  {
      try
      {
          XMLFileWrapper xml = new XMLFileWrapper(s);
          DataDAO db = new DataDAO();
          db.loadFromHDD(xml.retrieveWrappedRootDataFromXML());
          System.out.println(db.printAll()); &lt;&lt;&lt;&lt; This yealds output
          out=db.printAll(); 
      }catch(Exception e){
          e.printStackTrace();
      }
  }
  
  public DataDAO getDb() 
  {
      return db;
  }
  
  public String getOut()
  {
      return out;
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>public class DataDAO 
  {
  
      public void loadFromHDD(LinkedList&lt;Item&gt; list)
      {
          locDB.addAll(list);
      }
  
      public String printAll()
      {
          String result = "";
          for (Item item : locDB )
          {
          result = item.toString() + result + "\n";
          }
      return result;
      }
  
  }
  </code></pre>
  
  <p>DataDAO retrieves content from a JDom2 object... and while I doubt it has got anything to do with it, here is the method:</p>
  
  <pre><code>public LinkedList&lt;Item&gt; retrieveWrappedRootDataFromXML()
  {
      LinkedList&lt;Item&gt; result = new LinkedList&lt;Item&gt;();
  
      for ( Element e : getRoot().getChildren() )
      {
          Item item = new Item();
          item.setInfoId(e.getAttributeValue("id"));
          item.setName(e.getChildText("name"));
          item.setShortName(e.getChildText("short"));
          item.setUser(e.getChildText("user"));
          result.add(item);
      }
      return result;
  }
  </code></pre>