Results 1 to 4 of 4
  1. #1
    rushhour is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default [SOLVED] Java LoginServlet NullPointerException

    Hi all,

    I am having a little problem with my LoginServlet, where it seems to always come back as a NullPointerException. The error I get is:

    Java Code:
    java.lang.NullPointerException
    	LoginServlet.doPost(LoginServlet.java:57)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    What happens is I submit the login form to the server, the LoginServlet extracts the information entered in the login form, checks in another Java class if the information provided was valid, looks up the login information in another Java class, and returns the results back, then the LoginServlet forwards onto student.jsp if successful, or back to login page with the error if no information found.

    Here is my code, with the line in question commented next to it:

    Java Code:
    // LoginServlet
    import java.io.IOException;
    import java.io.*;
    
    import beans.*;
    import model.*;
    
    public class LoginServlet extends HttpServlet
    {
    	private String strError = null;
    	private boolean dbOK = false;
    	
    	//Instance of beans
    	private UserBean userBean;
    	private DataManager dataManager;
    	private Validation val = new Validation();
    	
    	public void init() throws ServletException
    	{
    		try
    		{
    			Class.forName( "com.mysql.jdbc.Driver" ).newInstance( ) ;
    		}
    		catch (Exception ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    	
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    	{		
    		/* 
    		 * Get user data fro submited form
    		 */		
    		String userName = request.getParameter("userName");
    		String password = request.getParameter("password");
    		
    		if(val.validate(userName, password))
    		{
    			userBean = new UserBean();
    			userBean = dataManager.getUserData(userName, password);    [COLOR="Red"]// This is the NullPointerException line[/COLOR]
    			
    			try
    			{
    				if( userBean.getUid() != null )
    				{
    					dbOK = true;
    				}
    			}
    			catch(NullPointerException npe)
    			{
    				System.out.println("Error on DB return");
    				npe.printStackTrace();
    				strError = "No username or password in database match one provided by you.";
    				dbOK = false;				
    			}
    			
    		}
    		else
    		{
    			strError = "Invalid username or password.";
    			dbOK = false;
    		}
    				
    		HttpSession session = request.getSession(true);
    		
    		if(dbOK)
    		{
    				session.setAttribute( "userBean", userBean);
    				RequestDispatcher dispatcher = request.getRequestDispatcher("/student.jsp");
    				dispatcher.forward( request, response);				
    		}
    		else
    		{
    			//Error after DB login checkout, redirect back to index.jsp
    			session.setAttribute( "error", strError);
    			RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
    			dispatcher.forward( request, response);	
    		}		
    	}		
    }
    Java Code:
    // DataManager.java
    package model;
    
    import java.sql.*;
    import beans.*;
    
    public class DataManager 
    {
    	Connection conn ;
    	Statement stmt ;
    	UserBean userBean = new UserBean();
    	
    	public DataManager()
    	{
    		try
    		{
    			Class.forName( "com.mysql.jdbc.Driver" ).newInstance( ) ;
    		}
    		catch ( Exception ex )
    		{
    			System.out.println( "Exception is: " + ex.toString( ) ) ;
    		}
    	}
    	
    	public UserBean getUserData(String userName, String password)
    	{	
    		try
    		{
    			conn = DriverManager.getConnection( "jdbc:mysql://localhost/" + "done?user=myUser&password=myPassword" ) ;
    			
    			stmt = conn.createStatement( ) ;
    		}
    		catch ( SQLException ex )
    		{
    			System.out.println( "SQLException: " + ex.getMessage( ) ) ;
    		}
    		
    		try
    		{
    			int count = 0 ;
    			String strQuery = "select * from member where username = '" + userName
    				               + "' and password = '" + password + "'" ;
    			ResultSet rs = stmt.executeQuery( strQuery ) ;
    				
    			while ( rs.next( ) )
    			{
    				count++ ;
    				userBean.setUid( rs.getString( "username" ) ) ;
    				userBean.setFirstName( rs.getString( "firstname" ) ) ;
    				userBean.setLastName( rs.getString( "surname" ) ) ;
    			}
    			
    			if ( stmt != null )
    			{
    				stmt.close( ) ;
    			}
    			if ( conn != null )
    			{
    				conn.close( ) ;
    			}
    		}
    		catch ( SQLException ex )
    		{
    			System.out.println( "SQLException: " + ex.getMessage( ) ) ;
    		}
    		
    		return userBean ;
    	}
    }
    Java Code:
    // Validation.java
    package model;
    
    public class Validation {
    
        public Validation() {}
        
        public boolean validate(String userName, String password)
    	{
    		if(userName.length() != 10 || (password.length() != 8))
    		{
    			return false;
    		}
    		return true;
    	}    
    }
    Java Code:
    // UseBean.java
    
    package beans;
    
    public class UserBean
    {
        private String uid;         //Username
        private String password;    //Password    
        private String firstName;   //First Name
        private String lastName;    //Last Name
    
        public UserBean() {}
    
        public void setUid(String str) {uid = str;}
        public String getUid() { return uid;}
    
        public void setPassword(String str) {password = str;}
        public String getPassword() {return password;}    
    
        public void setFirstName(String str) {firstName = str;}
        public String getFirstName() { return firstName;}
    
        public void setLastName(String str) {lastName = str;}
        public String getLastName() { return lastName;}
    
    
    }
    Hope you can help me with this NullPointerException error I am getting.
    Last edited by rushhour; 12-17-2010 at 11:55 AM. Reason: [SOLVED]

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Java Code:
    userBean = dataManager.getUserData(userName, password);    // This is the NullPointerException line
    			
    			try
    			{
    				if([B][COLOR="Red"]userBean != null && [/COLOR][/B] userBean.getUid() != null )
    				{

  3. #3
    rushhour is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default

    Hi,

    Thank you for helping me, however, I still get the same NullPointerException on the same line, even with the additional code you added for me.

    This is quite annoying this problem. Hope you can help again.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You never create a dataManager.
    You declare it but never instantiate it.

    However, as a general rule, do not give a Servlet state. You will get into synchronisation issues if you do. It is possible that all connections to your servlet will go through a single instance, so that state can be overwritten.

Similar Threads

  1. java.lang.NullPointerException
    By Travis in forum AWT / Swing
    Replies: 7
    Last Post: 09-18-2009, 06:49 PM
  2. Http Status 404 - LoginServlet.do
    By mbalas2 in forum Java Servlet
    Replies: 4
    Last Post: 03-05-2009, 06:52 PM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 06:41 AM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 11:11 AM
  5. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 10:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •