Results 1 to 4 of 4
- 12-13-2010, 10:18 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
[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:
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.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)
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; } }Hope you can help me with this NullPointerException error I am getting.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;} }Last edited by rushhour; 12-17-2010 at 10:55 AM. Reason: [SOLVED]
- 12-14-2010, 03:58 AM #2
Java Code:userBean = dataManager.getUserData(userName, password); // This is the NullPointerException line try { if([B][COLOR="Red"]userBean != null && [/COLOR][/B] userBean.getUid() != null ) {
- 12-14-2010, 09:50 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
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.
- 12-14-2010, 10:13 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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
-
java.lang.NullPointerException
By Travis in forum AWT / SwingReplies: 7Last Post: 09-18-2009, 05:49 PM -
Http Status 404 - LoginServlet.do
By mbalas2 in forum Java ServletReplies: 4Last Post: 03-05-2009, 05:52 PM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 3Last Post: 02-28-2009, 05:41 AM -
java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 2Last Post: 02-27-2009, 10:11 AM -
java.lang.NullPointerException
By stevemcc in forum AWT / SwingReplies: 2Last Post: 02-08-2008, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks