0

Hi, I've just uploaded a very simple web app that uses hibernate via a war file to my webserver:
http://www.colin-java.co.uk/BANK
The first couple of times I loaded the page above, it was fine, and it correctly displayed some of the contents of the User (MySQL) table.

But now when I load it, I get this error...

HTTP Status 500 -

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 25

null

Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	sun.reflect.GeneratedMethodAccessor6469.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)
	org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
	java.security.AccessController.doPrivileged(Native Method)
	javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
	org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
	org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
root cause

java.lang.NullPointerException
	org.apache.jsp.index_jsp._jspService(index_jsp.java:77)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	sun.reflect.GeneratedMethodAccessor6469.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)
	org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
	java.security.AccessController.doPrivileged(Native Method)
	javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
	org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
	org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)

The index.jsp page is shown below:

<%@page import="bank.User"%>
<%@page import="java.util.List"%>
<%@page import="bank.BankHelper"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%
        BankHelper helper = new BankHelper();
        List<User> users = helper.getUsers();

        for(User user : users)            //  **********  LINE 25 ************
        {
            out.println("user is "+user.getSurname()+"<BR>");
        }
        %>
    </body>
</html>

So it appears that 'users' on line 23 is null.

If I display the BankHelper class to look at the getUsers method,
perhaps this is the problem.

package bank;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

public class BankHelper
{

    Session session = null;

    public BankHelper()
    {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public List<User> getUsers()
    {
        List<User> users = null;

        try
        {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from User as user");
            users = q.list();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return users;
    }
}

Can anyone help figure out what I'm doing wrong, I don't like it when things work first time, but not after.
Any help is much appreciated. Thanks

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by javaAddict
0

The NullPointerException could mean that something is null at that line. Can you try this small debug code:

<%@page import="bank.User"%>
<%@page import="java.util.List"%>
<%@page import="bank.BankHelper"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%
        BankHelper helper = new BankHelper();
        List<User> users = helper.getUsers();

        System.out.println(users); // this prints at the server not the web page

        if (users!=null) {
          for(User user : users)            
          {
        %>
             user is <%=user.getSurname()%>
             <BR>
        <%
          }
        } else {
        %>
           users is NULL!
        <%
        }
        %>
    </body>
</html>

If you still get errors post them as well the updated line where they occur

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.