hi friends,
I am a novice java programmer.
I am in a learning process in java servlets.

But i struck into a Class method which should return a result set as an array.

I will show my code as some one can point out where am i going wrong. Currently im dealing with null value exception. Any one help me to get rid of that. Or suggest me a new (standard) method to access it. please treat me novice in java servlet


My servlet file in doGet()

protected void showAllUsers(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {

            LoginEntryDaoImpl users = new LoginEntryDaoImpl();

            List<UserInfo> allUsers = users.getAllUsers();

            
            Iterator itr = allUsers.iterator();
            while(itr.hasNext())
            {
                out.print("coming..................:"+itr.next());
                out.println(itr.next().toString());
            }





        } catch (Exception e) {
            out.print(e.getMessage());
            

        } finally {
            out.close();
        }
    }

My Dao Class

public class LoginEntryDaoImpl  implements LoginEntryDao{

    

public void LoginUser(){
  //constructor
    
}

public List<UserInfo> getAllUsers()   {

    Connection conn = null;
    ResultSet rs = null;
    Statement stmt=null;
    
    
    List<UserInfo> users = new ArrayList<UserInfo>();
     

    
    try {
       LoginEntryConnection config=new LoginEntryConnection();
        conn=config.getDbConnection();
        stmt = conn.createStatement();
        String query="SELECT id,user_name,dob from users";
        rs = stmt.executeQuery(query);
        while (rs.next()) {

            UserInfo user = new UserInfo();
            user.setId(rs.getInt("id"));

            user.setName(rs.getString("user_name"));
            
            user.setDob(rs.getString("dob"));
            
            users.add(user);
            
        }
       
       
    }
     catch(Exception e){

        LoginEntryException eh=null;
        eh.printErrorStatement(e);
    }

    finally {
        if (rs != null) try { rs.close(); }
        catch (SQLException e) {
            LoginEntryException eh=null;
            eh.printErrorStatement(e);
        }
        if (stmt != null) try { stmt.close(); }
        catch (SQLException e) {
            LoginEntryException eh=null;
            eh.printErrorStatement(e);
        }
        if (conn != null) try { conn.close(); }catch (SQLException e)
        {
            LoginEntryException eh=null;
            eh.printErrorStatement(e);
        }
        
    }

    return users;
  }//end of the method
 

}

My UserInFo class

public class UserInfo {
       private int id;
   private String name;
   private String dob;
   private String password;

 public void setId(int id){
       this.id=id;

   }
   public void setName(String name){
        this.name=name;

   }
   public void setPassword(String password){
         this.password=password;


   }
   public void setDob(String dob){
    this.dob=dob;
   }

   public int getId(){

       return this.id;
   }
   public String getName(){

        return this.name;
   }
   public String getPassword(){
        return this.password;
   }
   public String getDob(){
       return this.dob;
   }
}

Recommended Answers

All 6 Replies

You are calling the next method twice, without checking if it has a next element:

out.print("coming..................:"+itr.next());
out.println(itr.next().toString());

The first next call will return since you did that check at the while, but the second will not because you take the next element and you don't know of it has one.
If you are going to call the toString method better override it in your UserInfo class because what you call is the inherited method of the Object class.
Also you need to pass allUsers instance at the request to a jsp page. Then iterate, take each object and use the get methods. What is the point of having get method if you are not going to use them.
Check the method: request.setAttribute("users", allUsers)
At the jsp do:

List<UserInfo> users = request.getAttribute("users");

Check the RequestDispatcher class to send the above request to a jsp.

After you got the users instance use a loop to get each element and then use the get methods to create a table. Check the <table> tag.

And don't use this:

out.print("..");
out.println("...");

Don't use servlets to display data at the browser.

Check the tutorials at the top of the JSP forum

thanks for the reply javaaddict. Thanks for some valid pointers. The point is the function "getAllUsers" doesnot return valid arrayList. It retruns null value. Could you help me a bit detail in that.

In the catch block, you set the LoginEntryException to be null and then you one of its methods. That will give you a NullPointerException. What is the point of setting something to be null and then use it. You need to initialize it. Just do e.printStackTrace and see what is going on. There are exceptions that you don't print

it returns something like

com.login.entry.dao.UserInfo@7f906b

n times

it returns something like

com.login.entry.dao.UserInfo@7f906b

n times

That is not null. And read my previous comments:

If you are going to call the toString method better override it in your UserInfo class because what you call is the inherited method of the Object class.

What you get is what the toString method of the Object class returns, because you haven't overridden it in your class.
And you shouldn't be using that in the first place. Use the get methods to display whatever you want.

Hey, thats a great tip which i missed. Thanks. It returned a value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.