0

i am trying to display my data from mysql into the jsp through servlet problem i am facing are im not sure if the data is passing, i am getting the NullPointerException error. it doesnt even display any data taken from the mysql

what i am trying to do is to display all my data from my mysql id,name,price into the jsp in a format table

result.jsp

<%                           
        ArrayList<ProductBean> list=(ArrayList<ProductBean>)request.getAttribute("list");
        for (int i = 0; i < list.size(); i++) {
                list.get(i).getItemID();
                list.get(i).getName();
                list.get(i).getPrice();
            }
        %>

product.java

  connection = DriverManager.getConnection(connectionUrl + dbName, userId, password);
            statement = connection.createStatement();
            String sql = "SELECT id,name,price FROM item";

            ArrayList < ProductBean > list = new ArrayList < ProductBean > ();

            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                String itemID = resultSet.getString("id"); //fetch the values present in database
                String name = resultSet.getString("name");
                String price = resultSet.getString("price");
                list.add(new ProductBean(itemID, name, price));

            }
            request.setAttribute("list", list); 
            request.getRequestDispatcher("/result.jsp").forward(request, response);

ProductBean.java

public class ProductBean {

    private String itemID;
    private String name;
    private String price;

    public String getItemID() {
        return itemID;
    }
    public void setItemID(String itemID) {
        this.itemID = itemID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price; 
    }

    public ProductBean(){
    }

    public ProductBean(String itemID, String name, String price){
        this.itemID = itemID;
        this.name = name;
        this.price = price;
    }
}
7
  • Are you able to get the data into your arraylist list from database in product.java? Commented Mar 23, 2017 at 5:04
  • before i tried doing it all in one jsp file and the list was indeed contain the item data but as i tried to separate into servlet and jsp. i am not so sure but i didnt really change anything other than seperating into jsp and servlet Commented Mar 23, 2017 at 5:07
  • Try to print the results into console in while loop and confirm if data is being fetched from database. Commented Mar 23, 2017 at 5:14
  • Try to print out the values of item id , name and price as follows System.out.println("Item Id is " + itemID); System.out.println("name is " + name); System.out.println("price is " + price); Add these statements in the while loop and see. And also check whether the table name is item or product .Or you can do one more thing just print the list. You will come to know whether the data is added to the list Commented Mar 23, 2017 at 5:17
  • Or you can do either of these to know the value in the list System.out.println("The list size is " + list.size);System.out.println("The list is " + list); Commented Mar 23, 2017 at 5:20

1 Answer 1

1

To display use out.println() if you are not getting NULL pointer Exception

<%                           
        ArrayList<ProductBean> list=(ArrayList<ProductBean>)request.getAttribute("list");
        for (int i = 0; i < list.size(); i++) {
                 out.println(list.get(i).getItemID());
                 out.println(list.get(i).getName());
                 out.println(list.get(i).getPrice());
            }
        %>

Use can also use JSTL

<c:forEach var="i" items=${list}>
  <c:out value="${i.itemID}"/> 
  <c:out value="${i.name}"/> 
  <c:out value="${i.price}"/> 

</c:forEach>
Sign up to request clarification or add additional context in comments.

5 Comments

i am getting the null pointer error.i have never used jstl, do i need to import the jstl.jar? is there other option than using jstl?
try iterating list or check the size of list in product.java
i tried doing this in the product.java System.out.println("The list size is " + list.size); but i dont know where the result is showing is it in the netbeans output box or is it in the browser?i just added that 1 line, im new to jsp and servlet
Try System.out.println("The list size is " +list.size()); , it will show on console
yes, i tried that but i could find anything. i even tried system.out.println("hi"); on my run() console it display something like this run-display-browser: run:BUILD SUCCESSFUL (total time: 0 seconds) while the IDE log says something about FO [org.netbeans.spi.project.support.ant.GlobFileBuiltQuery]: and many more like that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.