Results 1 to 8 of 8
  1. #1
    stimpy4242 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default Null Pointer Exception and MySql

    I am getting a null pointer exception that I would think relates to the "connection" to my mysql db for the following logoncontrol servlet. However another page I have does add new users to the database and there are no issues there.

    Here is the error:
    java.lang.NullPointerException
    user.db.AccUser.getUserData(AccUser.java:27)
    LogonControl.doPost(LogonControl.java:62)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:729)


    Here is the code: Also line 27 is "stmt = con.createStatement();"
    Java Code:
    package user.db;
    
    import java.sql.*;
    import java.util.*;
    
    import user.util.*;
    import user.object.*;
    
    public class AccUser{
    
    	private Connection con;
    
    	public AccUser(Connection con){
    		this.con = con;
    	}
    
    	public ObjUser getUserData(String inid) {
    
    		Statement stmt = null;
    		String query = null;
    		ResultSet rs = null;
    		ObjUser objuser = new ObjUser();
    		query = "SELECT * FROM userinfo where user_id = '" + inid + "'";
    	
    		try {
    			
    			stmt = con.createStatement();
    			rs = stmt.executeQuery(query);
    		
    			if(rs.next()){
    				objuser.setUserId(rs.getString("user_id"));
    				objuser.setUserEmail(rs.getString("user_email"));				
    				objuser.setUserPassword(rs.getString("user_password"));			
    				objuser.setUserFname(rs.getString("user_fname"));						
    				objuser.setUserLname(rs.getString("user_lname"));						
    				objuser.setUserPhone(rs.getString("user_phone"));						
    				objuser.setUserFax(rs.getString("user_fax"));										
    				objuser.setUserAddrStreet(rs.getString("user_addr_street"));										
    				objuser.setUserAddrCity(rs.getString("user_addr_city"));										
    				objuser.setUserAddrState(rs.getString("user_addr_state"));										
    				objuser.setUserAddrZip(rs.getString("user_addr_zip"));																										
    				objuser.setUserType(rs.getString("user_type"));			
    				objuser.setUserStatus(rs.getString("user_status"));				
    				//objuser.setOrgId(rs.getString("org_id"));																
    				//objuser.setOrgDeptName(rs.getString("org_dept_name"));																				
    			}else{
    				objuser = null;
    			}
    			stmt.close();
    			
    		} catch(SQLException se) {
    			LogBuilder.println("SQLException : " + "AccUser : " + 
    						    "getUserData : " + se);
    		}
    		
    		return objuser;
    	}
    
    	//---- holding user list
    	public ArrayList getUserListHold() {
    		
    		Statement stmt = null;
    		String query = null;
    		ResultSet rs = null;
    		
    		ArrayList userList = new ArrayList();
    		
    		query = "SELECT * FROM userinfo where user_status = 'H'";
    		try {
    			stmt = con.createStatement();
    			rs = stmt.executeQuery(query);
    
    			while (rs.next()){
    				
    				ObjUser objuser = new ObjUser();
    				objuser.setUserId(rs.getString("user_id"));
    				objuser.setUserType(rs.getString("user_type"));			
    				objuser.setUserFname(rs.getString("user_fname"));						
    				objuser.setUserLname(rs.getString("user_lname"));									
    				//objuser.setOrgId(rs.getString("org_id"));
    				userList.add(objuser);
    			}
    			stmt.close();
    		} catch(SQLException se) {
    			LogBuilder.println("SQLException : " + "AccUser : " + 
    						    "getUserListHold : " + se);
    		}
    		return userList;
    	}
    	
    	//------- insert 
        public void insertUser(ObjUser user) {
    		Statement stmt = null;
    		
            String query = "insert into userinfo (user_id,user_email,user_password," +
              										"user_fname,user_lname,user_phone," +
              										"user_fax,user_addr_street," +
              										"user_addr_city,user_addr_state," +
              										
    "user_addr_zip,user_type,user_status )" +
    	             " values('" + user.getUserId() + "','" + 
    	  	                  user.getUserEmail() + "','" +
    	  	                  user.getUserPassword() + "','" +
    	  	                  user.getUserFname() + "','" +
    	  	                  user.getUserLname() + "','" +
    	  	                  user.getUserPhone() + "','" +
    	  	                  user.getUserFax() + "','" +
    	  	                  user.getUserAddrStreet() + "','" +
    	  	                  user.getUserAddrCity() + "','" +
    	  	                  user.getUserAddrState() + "','" +
    	  	                  user.getUserAddrZip() + "','" +
    	  	                  user.getUserType() + "','" +
    	  	                  user.getUserStatus()+ "')";
    
    		try {
    			stmt = con.createStatement();
    			int ret = stmt.executeUpdate(query);
    			if(ret != 0) {}
    		} catch(SQLException se) {
    			System.out.println("SQLException : " + "AccUser : " + 
    						    "insertUser : " + se);
    		}
        }	
    	//------- Update User status (list)
        public void updateUserStatus(String userid,String status) {
    						  
    		Statement stmt = null;
            String query = "update userinfo set user_status = " + 
            			   "'" + status + "'" + 
    					   "where user_id = '" + userid + "'";
    		try {
    			stmt = con.createStatement();
    			int ret = stmt.executeUpdate(query);
    			if(ret != 0) {}
    		} catch(SQLException se) {
    			LogBuilder.println("SQLException : " + "AccUser : " + 
    					    	"updateUserstatus : " + se);
    		}
    			
    		// user_message_board table insert
    		/*if (status.compareTo("A") == 0) {     // approved user
    			ObjUser usr = getUserData(userid);
    			String orgid = usr.getOrgId();
    			AccOrg accorg = new AccOrg(con);
    			int mbid = ((ObjOrg)accorg.getOrgData(orgid)).getMbId();
    			AccMessageBoard accmb = new AccMessageBoard(con);
    			accmb.insertUserMessageBoard(mbid,userid);				
    		}
    
    		// profile_user table insert
    		if (status.compareTo("A") == 0) {     // approved user		
    			for (int i = 0; i < prof.length; i++) {
    								
    	        	String query1 = "insert into profile_user (profile_id,user_id)" + 	
    	            				 " values(" + prof[i] + ",'" + userid + "')";	
    
    				try {
    					stmt = con.createStatement();
    					int ret = stmt.executeUpdate(query1);
    					if(ret != 0) {}
    				} catch(SQLException se) {
    					LogBuilder.println("SQLException : " + "AccUser : " + 
    			    	"updateUserstatus2 : " + se);
    			    }
    			}// for loop
    		}// end if */
    
    	}//
    }
    Code for LogonControl: line 62 is "objuser = accuser.getUserData(inUserid);"
    Java Code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import javax.swing.*;
    import java.io.File;
    import java.io.IOException;
    import java.sql.*;
    
    import user.util.*;
    import user.db.*;
    import user.object.*;
    
    public class LogonControl extends HttpServlet {
        	private static DBConnector dbcon = null;
    	private Connection con = null;
    //	private static final String REDIRECT_URL = "MBListControl";
    	private static final String REDIRECT_URL = "../online/Online.jsp";
    	private HttpServletRequest request;
    	private HttpServletResponse response;
    
    
    	public void init() throws ServletException {
    		try {
    
    			dbcon = DBConnector.getInstance();
    		} catch(SQLException se) {
    
    			LogBuilder.println("Database Connection Error	in LogControl : " + se);
    			System.out.println("Database error: " + se.toString());
    		}
    	}
    
    	public void close(Connection con) throws ServletException {
    		dbcon.releaseConnection(con);
    	}
    
    	public void doPost(HttpServletRequest rq, HttpServletResponse rs)
    		throws ServletException, IOException{
                    try {
                        dbcon = DBConnector.getInstance();
                        con = dbcon.getConnection();
    		} catch(SQLException se) {
                        LogBuilder.println("Database getConnection Error in LogControl : " + se);
                        System.out.println("Database getConnection error: " + se.toString());
    		}
    
    		request = rq;
    		response = rs;
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    
    		HttpSession session = request.getSession(true);
    
    		String inUserid = rq.getParameter("userid");
    
    		String inPassword = rq.getParameter("password");
    
                    String utype="H";
    		AccUser accuser = new AccUser(con);
    		ObjUser objuser;
    
    		objuser = accuser.getUserData(inUserid);
    		//close(con);
    
    		if (objuser == null) {
    			sendMessage("User Id Not-Found");
    			return;
    		}
    
    		//if (inPassword.equals(objuser.getUserpsw()) == false) {
    		if (!(inPassword.equals(objuser.getUserPassword()))) {
    			sendMessage("Check PASSWORD");
    			return;
    		}
    		if (utype.equals(objuser.getUserStatus()))
    		{
    			sendMessage("Not yet approved by admin");
    			return;
    		}
    		session.setAttribute("userinfo",objuser);
    		//session.putValue("userinfo",objuser);
    		ServletContext sc = getServletContext();
    //		RequestDispatcher rd = sc.getRequestDispatcher ("/servlet/MBListControl");
    //		rd.forward(request, response);
    		response.sendRedirect(REDIRECT_URL);
    	}
    
    /************************ Method added on Oct 18,2002*******************************/
    	public void doGet(HttpServletRequest rq, HttpServletResponse rs)
    		throws ServletException, IOException{
    
    
                     doPost(rq,rs);
    
    	}
    /**************************************************************************************/
    
    	private void sendMessage(String msg) throws IOException {
    
    		PrintWriter out = response.getWriter();
    		out.println("<html>");
    		out.println("<title>Error Message</title>");
    		out.println("<head>Logon Failed --</head>");
    		out.println("<body>");
    		out.println("<br>");
    		out.println(msg);
    		out.println("</body>");
    		out.println("</html>");
    		out.close();
    	}
    }
    ObjUser code:
    Java Code:
    package user.object;
    
    import user.util.*;
    import user.db.*;
    
    public class ObjUser {
    
    	private String userId;
    	private String userEmail;			
    	private String userPassword;
    	private String userFname;
    	private String userLname;
    	private String userPhone;
    	private String userFax;
    	private String userAddrStreet;
    	private String userAddrCity;
    	private String userAddrState;
    	private String userAddrZip;
    	private String userType;
    	private String userStatus;
    	private String orgId;
    	private String orgDeptName;
    									
    	public ObjUser() {}
    	
    	public String getUserId(){ return userId; }
    	public void setUserId(String arg){ userId = arg; }
    
    	public String getUserEmail(){ return userEmail; }
    	public void setUserEmail(String arg){ userEmail = arg; }
    
    	public String getUserPassword(){ return userPassword; }
    	public void setUserPassword(String arg){ userPassword = arg; }
    
    	public String getUserFname(){ return userFname; }
    	public void setUserFname(String arg){ userFname = arg; }
    
    	public String getUserLname(){ return userLname; }
    	public void setUserLname(String arg){ userLname = arg; }
    
    	public String getUserPhone(){ return userPhone; }
    	public void setUserPhone(String arg){ userPhone = arg; }
    
    	public String getUserFax(){ return userFax; }
    	public void setUserFax(String arg){ userFax = arg; }
    
    	public String getUserAddrStreet(){ return userAddrStreet; }
    	public void setUserAddrStreet(String arg){ userAddrStreet = arg; }
    
    	public String getUserAddrCity(){ return userAddrCity; }
    	public void setUserAddrCity(String arg){ userAddrCity = arg; }
    
    	public String getUserAddrState(){ return userAddrState; }
    	public void setUserAddrState(String arg){ userAddrState = arg; }
    
    	public String getUserAddrZip(){ return userAddrZip; }
    	public void setUserAddrZip(String arg){ userAddrZip = arg; }
    
    	public String getUserType(){ return userType; }
    	public void setUserType(String arg){ this.userType = arg; }
    
    	public String getUserStatus(){ return userStatus; }
    	public void setUserStatus(String arg){ userStatus = arg; }
    
    	//public String getOrgId(){ return orgId; }
    	//public void setOrgId(String arg){ orgId = arg; }
    
    	//public String getOrgDeptName(){ return orgDeptName; }
    	//public void setOrgDeptName(String arg){ orgDeptName = arg; }
    
    	public String getUserFullName(){ 
    		if (userFname == null)
    			userFname = "";
    		if (userLname == null)
    			userLname = "";			
    		return userFname + " " + userLname; 
    	}
    
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Don't make connections instance variables of servlets. If you need a connection, create it, use it and close it preferably all on one method.

  3. #3
    stimpy4242 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    the bi line of this forum is where learning and development BEGINS...so as a beginner, the answer you provided does that mean don't pass in a variable such as "con" which is a connection variable? Is that a best practice or is that why this isn't working? Is that the reason for the error?

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    800
    Rep Power
    6

    Default

    U can take r035198x as good suggestion.

    But,my doubt is if u got connection error ,then whatever s.o.p or debugginging u statements are u able to see?
    Ramya:cool:

  5. #5
    stimpy4242 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    I am sorry, I don't follow your answer. So his is a suggestion, not the fix? Also I don't have a connection error I have a null pointer exception. I know the connection works fine, because other pages are able to insert into the database. What do you mean when you say, whatever sop or debugginging u statements are u able to see....that doesn't make sense to me.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    800
    Rep Power
    6

    Default

    What my question is are u not able get LoginBuilder.println...statements and system.out.println statements whatever u have given?

    try to print the connection object in AccUser class before stmt = con.createStatement();
    Last edited by RamyaSivakanth; 10-05-2009 at 03:01 PM.
    Ramya:cool:

  7. #7
    stimpy4242 is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    Ok I will try to do that, it is working now that I have moved my connection objects locally....

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    When a servlet is invoked, the container starts a thread and calls the doGet or doPost method code on that new thread. There is however only one servlet and therefore only one set of instance variables. You should be able to work out why it's bad/wrong to make connections instance variables when you think about multiple threads trying to use those instance variables at the same time.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  3. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01:47 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 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
  •