简体   繁体   中英

JSP NullPointer StandardWrapperValve[servlets. ]: Servlet.service() for servlet servlets. threw exception java.lang.NullPointerException

I'm trying to learn how to work with JSP. And I encountered something, which makes no sense, at least in my opinion:

When I try to run:

response.getWriter().println(m.getDb().printAll());

I get this when trying to load the page in question:

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)

at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) at java.lang.Thread.run(Thread.java:744)

But when I run:

response.getWriter().println(m.getOut());

The output is correct.

Both should return the same result since

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;
     }
 ...
}

and

class ObjectDAO
{

 ...
 public String printAll()
 {
  String result = "";
  for (WrappedItem item : locDB )
  {
   result = item.toString() + result + "\n";
  }
  return result;
 }
  ...
}

Why is it, that in one case there is a NullPointer and in the other case there is none?

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.

PS: 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.

Addendum 1

As I stated above, my problem is:

response.getWriter().println(m.getOut());

works fine.

response.getWriter().println(m.getDb().printAll());

doesn't.

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.

That is all the difference.

Addendum 2

Added the full stack trace.

Servlet: Generic code, the only difference is

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    System.out.println("Started ");
    response.getWriter().println("<h1> Header1 </h1>");
    response.getWriter().println("<body> Funny body text </body>");
    System.out.println(getServletContext().getRealPath("data.xml"));
    serverJava.Monitor m = new serverJava.Monitor(getServletContext().getRealPath("data.xml"));
    response.getWriter().println("<br> I say:" + m.printHello());
    response.getWriter().println(m.getOut());
    response.getWriter().println(m.getDb().printAll());
}

Now before people start: The following code was just to test, whether everything works ok. It's not really there to stay.

public M(String s) 
{
    try
    {
        XMLFileWrapper xml = new XMLFileWrapper(s);
        DataDAO db = new DataDAO();
        db.loadFromHDD(xml.retrieveWrappedRootDataFromXML());
        System.out.println(db.printAll()); <<<< This yealds output
        out=db.printAll(); 
    }catch(Exception e){
        e.printStackTrace();
    }
}

public DataDAO getDb() 
{
    return db;
}

public String getOut()
{
    return out;
}

.

public class DataDAO 
{

    public void loadFromHDD(LinkedList<Item> list)
    {
        locDB.addAll(list);
    }

    public String printAll()
    {
        String result = "";
        for (Item item : locDB )
        {
        result = item.toString() + result + "\n";
        }
    return result;
    }

}

DataDAO retrieves content from a JDom2 object... and while I doubt it has got anything to do with it, here is the method:

public LinkedList<Item> retrieveWrappedRootDataFromXML()
{
    LinkedList<Item> result = new LinkedList<Item>();

    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;
}

Lets approach this logically.

The following statement can (in theory) throw NullPointerException for the following reasons:

  response.getWriter().println(m.getDb().printAll());
  1. response is null .
  2. response.getWriter() returns null
  3. m is null
  4. m.getDb() is null

The 1 and 2 we can eliminate from what the JSP spec says. (Unless you did something truly bizarre ...)

The 3 we can (tentatively) eliminate on the basis that this statement:

  response.getWriter().println(m.getOut());

works in (I assume) a similar context. That implies m isn't null . (Though given the other inaccuracies in the evidence in your Question, we can't be definite about that ...)

That leaves 4 as the most likely cause. Check that you have implemented getDb() correctly; eg make sure that the ObjectDAO value it returns can't be null .


Note we can't be certain about any of this, because the code snippets in your Question are clearly "fictional". They contain obvious typos that the compiler would reject. Hence, we can't trust that they actually bear much resemblance to the actual code in your JSP.


UPDATE

Now that you have posted the full code of the M class, the cause of the NPE just leaps off the page at me!

The getDb() method returns null because db is not initialized.

"But" I hear you say "it is!!" .

Oh no it isn't! You are assigning new DataDAO() to a local variable db , not to this.db .


Lesson: when indisputable logic tells you that the cause of a bug must be X, it doesn't pay to argue ...

In my case , I mistakenly typed, eg. productDAO proddao; , it should be productDAO proddao= new productDAO(); and the program works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM