Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: a null pointer exception

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default a null pointer exception

    hi guys, I'm writing some servlets for a website, but I'm new to JSP, therefore I made alot mistakes when I was writing them, I've been fixing them for weeks, but this null pointer exception, I just don't know where the problem is, and what caused it..... so here is the code, thanks alot guys!

    package Servlet;
     
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    import Model.DAO;
     
    import java.io.*;
    import java.sql.*;
     
    public class LoginServlet extends HttpServlet
    {
    	Connection conn=null;
    	Statement stmt=null;
     
    	String username;
    	String password;
     
    	public void init(ServletConfig config)throws ServletException
    	{
    		super.init();
    	}
     
    	public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    	{
    		DAO.getConnection(conn);
    		try {
    			stmt=conn.createStatement();
    		} catch (SQLException e1) {
    		}
    		username=request.getParameter("username");
    		password=request.getParameter("password");
    		if(username=="admin")
    		{
    			try
    			{
    				ResultSet rs=stmt.executeQuery("select '"+username+"' from tb_user");
    				if(rs.next())
    				{
    					ResultSet vpass=stmt.executeQuery("select '"+password+"' from tb_user where id = '"+username+"'");
    					if(vpass.next())
    					{
    						request.getRequestDispatcher("./manage/manage_index.jsp").forward(request,response);
     
    					}
    					else
    					{
    						request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_fail.jsp").forward(request,response);
    					}
    				}
     
    			}catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    		else
    		{
    			try
    			{
    				ResultSet rs=stmt.executeQuery("select '"+username+"' from tb_user");
    				if(rs.next()==true)
    				{
    					ResultSet vpass=stmt.executeQuery("select '"+password+"' from tb_user where id = '"+username+"'");
    					if(vpass.next()==true)
    					{
    						request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_success.jsp").forward(request,response);
    					}
    					else
    					{
    						request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_fail.jsp").forward(request,response);
    					}
    				}
    				else
    				{
    					request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_fail.jsp").forward(request,response);
    				}
    			}catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    }

    I know this servlet sucks, so please just ignore its poor design and hard codind.....

    --- Update ---

    eclipse says that the null pointer exception occured at line 28, which is
    stmt=conn.createStatement();

    and DAO.getConnection is just a normal method used to get connection with MySQL database, here is the code of it:
    [hightlight=Java]
    package Model;

    import java.sql.*;

    public class DAO
    {
    public static void getConnection(Connection conn)
    {
    String driver="com.mysql.jdbc.Driver";
    String url="jdbc:mysql://localhost:3306/web";
    try
    {
    Class.forName(driver);
    conn=DriverManager.getConnection(url,"root","12345 6");
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    [/highlight]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: a null pointer exception

    Do you have the full text of the error message? Copy it and paste it here so we can see what statement the error is on.

    --- Update ---

    null pointer exception occured at line 28, which is
    If looks like conn has a null value.
    Where does the code assign a non-null value to the conn variable?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: a null pointer exception

    java.lang.NullPointerException
    at Servlet.LoginServlet.service(LoginServlet.java:28)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:728)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:123)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(A ccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.p rocess(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnect ionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProce ssor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

    Sure, here it is

    --- Update ---

    you mean the conn in the DAO or in the servlet?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: a null pointer exception

    conn where the NPE happens on line 28
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    alskdwq (September 22nd, 2013)

  6. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: a null pointer exception

    Quote Originally Posted by Norm View Post
    conn where the NPE happens on line 28
    Thanks alot man!!! You said about the non null value, so I re checked my codes trying to find such mistakes, and I found that the getConnection method didn't return an object, so the conn I'm using in the servlet is a null value, thanks!!!

Similar Threads

  1. Help With Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 15th, 2012, 10:41 PM
  2. null pointer exception
    By Ramzi89 in forum Object Oriented Programming
    Replies: 1
    Last Post: August 15th, 2012, 01:57 PM
  3. Need Help with Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 23rd, 2012, 02:20 PM
  4. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  5. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM