0

Hi, guys. badly need your help.

still a newbie in programming. seems like i cannot display some data on the browser from the database. i was able to create a login page. but when i tried adding another servlet and tried calling its jsp, error message appears: HTTP STATUS 500 java.lang.NullPointerException.

just dont know what to do next. hope somebody can help me.
thanks a lot.

3
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by nohup
0

You are getting NullPointerException that mean that somewhere you calling a variable that has null value (most likely wasn't initialized). Check the line line number which is showed together with the error. If you not able sort it out on your own post full error as you get it and we can guide you from there

0

Hi peter!
It works fine if I declare one servlet in web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	

	<servlet>
		<servlet-name>Login</servlet-name>
		<servlet-class>LoginAuthentication</servlet-class>
	</servlet>
	
		
	<servlet-mapping>
		<servlet-name>Login</servlet-name>
		<url-pattern>/LoginAuthentication</url-pattern>
	</servlet-mapping>
	
	
</web-app>

but if i add another servlet:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>CallLogger</display-name>

	<servlet>
		<servlet-name>Login</servlet-name>
		<servlet-class>LoginAuthentication</servlet-class>
	</servlet>
	
	<servlet>
		<servlet-name>userList</servlet-name>
		<servlet-class>ListUsers</servlet-class>
	</servlet>
	
	
	
	<servlet-mapping>
		<servlet-name>Login</servlet-name>
		<url-pattern>/LoginAuthentication</url-pattern>
	</servlet-mapping>
	
	
	<servlet-mapping>
		<servlet-name>userList</servlet-name>
		<url-pattern>/ListUsers</url-pattern>
	</servlet-mapping>

	
	
</web-app>

it gave me this error message:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
org.apache.jsp.userList_jsp._jspService(userList_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I'm not sure how to do it if you want to declare 2 servlets.
Thanks much for your reply.

0

Hi guys,

how do you define an object if you have two servlets? im not sure how to do it. thanks much.

0

Hi guys,

how do you define an object if you have two servlets? im not sure how to do it. thanks much.

Can you explain more in details? Not sure I do follow...

0

Hi peter.. i think my problem here is that i don't know how to call a servlet from another servlet...

0

i'll just read some tutorials first on how to call a servlet from another...
then i'll get back to this post if i dont know what to do next.

thanks much. ^_^

0

Here are the classes:


This is for the LoginAuthentication Class

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginAuthentication extends HttpServlet{
	private ServletConfig config;
  
	public void init(ServletConfig config) throws ServletException{
		this.config=config;
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException,IOException{
		  
		PrintWriter out = response.getWriter();


	    String connectionURL = "jdbc:jtds:sqlserver://localhost:1433/sample_db";
	    Connection connection=null;
	    ResultSet rs1;
	    String userName=new String("");
	    String passwrd=new String("");
	    String name=new String("");
	    
	    //String pCode = new String("");
	   
	    
	    String usrName = request.getParameter("user");
	    String usrPassword = request.getParameter("pass");
	    response.setContentType("text/html");
	    try{
	    	// Load the database driver
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			// Get a Connection to the database
			connection = DriverManager.getConnection(connectionURL, "sa", "@dm;n");
			
			System.out.println("found Database....");

			String sql1 = "SELECT username, userPassword, userDescription FROM tblUser WHERE username = '"+usrName+"' AND userPassword = '"+usrPassword+"'";

			
			Statement s1 = connection.createStatement();

			s1.executeQuery (sql1);

	
			rs1 = s1.getResultSet();

			
			while (rs1.next ()){
			    userName=rs1.getString("username");
			    passwrd=rs1.getString("userPassword");
			    name = rs1.getString("userDescription");
			    //System.out.println("User Name = "+userName+"\nPassword = "+passwrd);
			}
			rs1.close ();
			s1.close ();
			
			
	    }catch(Exception e){
	    	System.out.println("Exception is ;"+e);
	    }
	    System.out.println("USER = "+usrName);
	    System.out.println("PASSWORD  = "+usrPassword);
	    
	 
	
	    
	    if(userName.equals(usrName) 
			&& passwrd.equals(usrPassword)){
			out.println("Welcome, ");
			out.println(" " + name + "!");
	    }
	    else{
	    	out.println("Please enter correct username and password");
	    	out.println("<a href='index.jsp'><br>Login again</a>");
	    }
	    
	
	    out.println("<a href='http://localhost:8080/CallLogger/ListUsers'><br>List of Users</a>");
	    


	    
	    
	    
	    
	}
}

For the ListUsers class

import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ListUsers extends HttpServlet{

  private ServletConfig config;
  String page="userList.jsp";
  
  
  public void init(ServletConfig config)
  throws ServletException{
   this.config=config;
   }
public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException,IOException {
  
  PrintWriter out = response.getWriter();
  String connectionURL = "jdbc:jtds:sqlserver://localhost:1433/sample_db";
  Connection connection = null;
  ResultSet rs;
  response.setContentType("text/html");
  List dataList = new ArrayList(); 
    try {
     // Load the database driver
    Class.forName("net.sourceforge.jtds.jdbc.Driver");
    // Get a Connection to the database
    connection = DriverManager.getConnection(connectionURL, "sa", "@dm;n"); 
    //Select the data from the database
    String sql = "select * from tblUser";
    Statement s = connection.createStatement();
    s.executeQuery (sql);
    rs = s.getResultSet();
    while (rs.next ()){
        //Add records into data list
        dataList.add(rs.getInt("userID"));
        dataList.add(rs.getString("username"));
        dataList.add(rs.getString("userPassword"));
        dataList.add(rs.getString("userDescription"));
      }
      rs.close ();
      s.close ();
      }catch(Exception e){
      System.out.println("Exception is ;"+e);
      }
      request.setAttribute("data",dataList);
      //Disptching request
      RequestDispatcher dispatcher = request.getRequestDispatcher(page);
      if (dispatcher != null){
        dispatcher.forward(request, response);
      } 
  }
}

I really appreciate your help..^_^

0

There doesn't seem to be any problem with your classes or the web.xml.

Looking at the stacktrace, notice these lines:

java.lang.NullPointerException
org.apache.jsp.userList_jsp._jspService(userList_jsp.java:63)

This means the problem is in 'userList.jsp'.
(Tip: Look for the compiled 'userList.jsp', the file path should be something like work/Catalina/localhost/CallLogger/org/apache/jsp/userList_jsp.java under Tomcat directory, and check what happens at line 63)

Could you please post that JSP's code?

Until then I assume you try to iterate over the contents of the session variable 'data', (which is set in ListUsers), but you call it the wrong way, so that you get something null, which causes a NullPointerException.

The following is a possible 'userList.jsp' that works for the given ListUsers class:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
 <body>
  <c:forEach var="user" items="${requestScope.data}">${user}</c:forEach>
 </body>
</html>

Try this and if it works, then compare it to your 'userList.jsp' regarding the handling of the users data from ListUsers.

Edited by nohup: n/a

This topic has been dead for over six months. 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.