0

I keep getting a NullPointerException when I run the program.

java.lang.NullPointerException
        at org.apache.jsp.student_jsp._jspService(student_jsp.java from :65)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:406)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:483)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:373)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:619)

SEVERE: org.hibernate.hql.ast.QuerySyntaxException: student is not mapped [from student]
        at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
        at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
        at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
        at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
        at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
        at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
        at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
        at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
        at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
        at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
        at servlet.Servlet.processRequest(Servlet.java:52)
        at servlet.Servlet.doGet(Servlet.java:128)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:619)

Any ideas why I am getting this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joe Smith
  • 205
  • 4
  • 15
  • 5
    On what line do you get the NPE? – ptomli Jan 20 '11 at 14:40
  • 2
    Add exception stack trace? where do you get NPE? which line? – fmucar Jan 20 '11 at 14:41
  • Actually, Im sorry, when I run the servlet on its own, I get "Not binding factory to JNDI, no JNDI name configured"...when I try to run the individual jsp's on their own, I get the NPE. Let me know if you need me to post one of the jsp's. Thanks. – Joe Smith Jan 20 '11 at 14:48
  • The JSPs are doubtless trying to access some data you're expecting to set in the servlet. Since they're not being set, they're null, and you've got an NPE. – ptomli Jan 20 '11 at 14:50
  • So how would I go about fixing this? – Joe Smith Jan 20 '11 at 14:51
  • By not running the JSPs on their own :) -- they rely on some request attributes being set. Fix the servlet and use the JSPs only as views that rely on the controller (servlet) to provide the model data. – Costi Ciudatu Jan 20 '11 at 14:54
  • When I run the program as a whole, when I try to view the jsp's, I get a blank page. The url in the address bar is correct, but nothing shows on the page. Nothing shows up in the page source code either...any idea why this is? – Joe Smith Jan 20 '11 at 15:00
  • 1
    The first thing I think you should do is to NOT use an emtpy catch block in your servlet method. Declare your method to throw ServletException (as the JavaDoc states) and rethrow whatever you catch wrapped in a ServletException. – Costi Ciudatu Jan 20 '11 at 15:07
  • Apart from the concrete problem, the design is clumsy. Everytime you add a JSP page, the servlet's code grows gradually. 30 JSP pages would likely result in a servlet with over 3000 lines of code. Aargh ;) I'd suggest to get yourself through [this question](http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/) or at least adopt a decent MVC framework. – BalusC Jan 20 '11 at 15:09
  • Actually, the whole question is unclear and not useful. -1 until you clarify this. When you get an exception which you don't understand, it's very important to post the stacktrace and the relevant code snippet which threw this exception (as per 1st line of the stacktrace). Because they contain information about the cause of the problem. – BalusC Jan 20 '11 at 15:17
  • I added the stacktrace from the glassfish server...let me know if that helps any... – Joe Smith Jan 20 '11 at 15:29
  • I suppose that's the stacktrace for direct JSP access. But you should post the stacktrace of the exception thrown by your servlet -- after you will properly handle it: log it or (better) rethrow it, instead of doing nothing in an empty catch block. The error seems to be in your servlet, but you miss it because you simply ignore it. – Costi Ciudatu Jan 20 '11 at 15:33
  • I added some more of the stacktrace for the servlet – Joe Smith Jan 20 '11 at 15:45
  • You've actually two separate problems. A bug in your JSP code and a bug in your HQL code. I've posted an answer for both, but in the future, you should be asking separate questions for each individual problem. – BalusC Jan 20 '11 at 16:09
  • SEVERE: org.hibernate.hql.ast.QuerySyntaxException: student is not mapped [from student] --- maybe try Student (capital first letter) or see why that entity is not mapped. – Costi Ciudatu Jan 20 '11 at 16:09

3 Answers3

2

Here's the relevant bit

java.lang.NullPointerException
    at org.apache.jsp.student_jsp._jspService(student_jsp.java from :65)

(the remnant of your question is actually irrelevant, I was so kind to remove the monster servlet code and all INFO messages from your original question to minimize noise).

This exception and trace indicates that you're using old fashioned scriptlets (those <% %> things) in student.jsp file instead of taglibs and EL (which is null-safe). Your culprit is in line 65 of the compiled class file of the JSP file. You can find this class file in servletcontainer's work folder. You need to trackback this to the code in the JSP file itself. It'll look something like this:

<%= someBean.getSomeProperty(); %>

The NullPointerException indicates that someBean is null. Fix it. The ideal fix would be to avoid scriptlets to all extent. E.g.

${someBean.someProperty}

This will never throw NPE whenever ${someBean} is null.


As per the other stacktrace:

org.hibernate.hql.ast.QuerySyntaxException: student is not mapped [from student]

This means that the entity student is nowhere mapped. This is usually supposed to be the short class name of the entity. Since the normal Java convention dictates that the classnames ought to start with uppercase, I'll bet that it's actually called Student and not student. You need to fix it accordingly in your HQL query.

If that is not the case, then you simply need to add it to your Hibernate mapping.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

If it does turn out to be one of your string comparisons, then you might like to adopt a safer strategy

     if ("student".equalsIgnoreCase(view)) { ... }
else if ("course".equalsIgnoreCase(view)) { ... }
// etc

You get to avoid the null check, and you're protected from NPEs

ptomli
  • 11,730
  • 4
  • 40
  • 68
0

Invert your string comparisons. Call equals() on the constant value. This will help avoid NullPointerExceptions.
Here is an example: if ("student".equalsIgnoreCase(view))

If hibernateSession.createQuery() returns null, you will get a NullPointerException

DwB
  • 37,124
  • 11
  • 56
  • 82