big zero
big zero

Reputation: 610

getting Null value + probably query isn't working

Actually I'm following mvc-3 tier architecture to my application.and here is my Search option's implementation. In my servlet(controller) I'm doing this :

        String select = request.getParameter("select");  // getting value
        String search = request.getParameter("search");  // getting value
        request.setAttribute("select", select);
        request.setAttribute("search", search);
        System.out.println("Select : "+select+" Search : "+search);

        int page = 1;
        int recordsPerPage = 20;
        if(request.getParameter("page") != null)
            page = Integer.parseInt(request.getParameter("page"));
        SearchDAO searchDAO=new SearchDAO();
        List<User> list1=searchDAO.searchAllUsers((page-1)*recordsPerPage,recordsPerPage,select,search);  //getting null value,doing SOP of list1
        int noOfRecords = searchDAO.getNoOfRecords();  //getting null value,doing SOP
        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);  //getting null value,doing SOP
        session.setAttribute("noOfRecords", noOfRecords);
        request.setAttribute("searchList", list1);
        request.setAttribute("noOfPages", noOfPages);
        request.setAttribute("currentPage", page);

and in the SearchDAO is:

public class SearchDAO {
private int noOfRecords;
Connection connection;
Statement stmt;

    public List<User> searchAllUsers(int offset ,int noOfRecords,String select,String search){
    private static Connection getConnection() throws SQLException,ClassNotFoundException{
    Connection con = ConnectionFactory.getInstance().getConnection();
    return con;  //ConnectionFactory is class for making the connection to DB.
}
     String query="select SQL_CALC_FOUND_ROWS * from info where '"+select+
"' like '%"+search+"%' order by serialNo asc limit 
" + offset + " , " + noOfRecords;

    List<User> list1 = new ArrayList<User>();
    User user1=null;

    try {
        connection = getConnection();
        stmt = connection.createStatement();
        ResultSet rs=stmt.executeQuery(query);
        while(rs.next()){
        user1=new User();
        user1.setSerial(rs.getInt(1));
            user1.setName(rs.getString(2));
            user1.setEmail(rs.getString(3));
            list1.add(user1);
            }

        rs.close();
        rs = stmt.executeQuery("SELECT FOUND_ROWS()");
        if(rs.next())
            this.noOfRecords = rs.getInt(1); 

    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }finally
    {
        try {
            if(stmt != null)
                stmt.close();
            if(connection != null)
                connection.close();
            } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return list1;

}

public int getNoOfRecords() {
    return noOfRecords;
}
}

Upvotes: 1

Views: 374

Answers (3)

prakashb
prakashb

Reputation: 170

AFAIK, create a bean class and with the help of the class, you can communicate between controller and DAO, you could not do it by using request.getAttribute(which is in your DAO).

Upvotes: 1

PermGenError
PermGenError

Reputation: 46398

Your concern is right,:

'%"+request.getParameter("search)+"%'
  1. Firstly, you never initialized request in SearchDAO, and you are calling getParameter on its instance, Thus NullPointerException
  2. Secondly, how would your seachDAO get the request ??, it isnt a servlet, its just aplain POJO

Upvotes: 1

Murali N
Murali N

Reputation: 3498

  • check if you are able to retrieve parameters in your request.getParameter(); call in your servlet

  • check if you are able to store parameters in request scope like
    request.setAttribute() call

Upvotes: 1

Related Questions