0

I have shopping cart with fields Title Price Amount Total. Title and price works fine but Amount is input field where user can enter how many items he wants and i want to update Total based on that. Here is my JSP:

 <body>
    <table border="1px"style="width:50%">
        <tr>
            <th>Title</th>
            <th>Price</th>
            <th>Amount</th>
            <th>Total</th>
        </tr>
        <c:forEach var="article" items="${articleList}">              
            <tr>
                <td>${article.title}</td>
                <td>${article.price}</td>
                <td><input type="text" value="${article.numberOfOrder}"  onchange="myFunction()"></td>                    
                <td>${article.total}</td>
                  <input type="hidden" value="${article.numberOfOrder}" name="article_NumberOfOrder">
                  <input type="hidden" value="${article.total}" name="article_total">
                  <input type="hidden" value="${article.price}" name="article_price">
            </tr>
        </c:forEach> <br>
    </table>
    Total:${total}<br>
</body>

Below <body>i have javacsript where i send to servlet when value(number of orders) is changed:

  <script>
function myFunction() {
  document.location.href="ArticalOnChangeServlet";    
}
 </script>

And this is servlet:

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession();
    int numberOfOrder = Integer.parseInt(request.getParameter("articleNumberOfOrder"));//Here i'm getting NPE
    double price = Double.parseDouble(request.getParameter("article_price"));
    Double totalArtical = (Double)session.getAttribute("totalArticle");
    if(totalArtical == null){
        totalArtical = price;
    }
    totalArtical*=numberOfOrder;
    session.setAttribute("totalArtical", totalArticle);
    ArticleUpdate(numberOfOrder,totalArticle); // Here i update database
    RequestDispatcher rd = request.getRequestDispatcher("cart_overview.jsp");
    rd.forward(request, response);
}

IDE and browser are giving me null pointer exception in servlet on line where i try to get parameter from number of orders. What am i doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
nemke nemke
  • 191
  • 1
  • 1
  • 9
  • In your current code you are just redirecting to servlet without passing any value to your backend that's the reason its returning null. – Swati Jul 26 '20 at 14:55
  • @Swati You mean in JS i need to pass value to servlet? But how? – nemke nemke Jul 26 '20 at 15:15
  • 1
    yes either pass value as parameter in url i.e : `ArticalOnChangeServlet?varibale1=value1..` or use ajax . – Swati Jul 26 '20 at 15:26
  • @Swati Okay, that makes sense, but how to pass value in js without going to servlet and typing `request.getParameter("artical_NumberOfOrder")`?For example if i want to pass number of order in js `ArticalOnChangeServlet?artical_NumberOfOrder=` is equal to what? – nemke nemke Jul 27 '20 at 12:13
  • 1
    put `onchange="myFunction(this.value)"` and change your function i.e : `function myFunction(value)` and pass it in url `"ArticalOnChangeServlet?artical_NumberOfOrder="+value;` – Swati Jul 27 '20 at 12:44
  • Yes, thank you! One more question and i want bother you anymore :D When i want to pass value of a price i give it id="price" and tried this in myFunction: `let x = document.getElementById("price").value` then i just concat x value:`ArticalOnChangeServlet?artical_NumberOfOrder="+value+"&article_price="+x ;` But it always giving me price of first item, wherever I change number of order. So how to get value from a item that i'm changing number of orders? – nemke nemke Jul 27 '20 at 14:40
  • 1
    Yes it's because you cannot give same id to all inputs that's the reason it's returning only first value.Instead you can pass that value as well in function i.e : `onchange="myFunction(this.value , '${article.price}')` and then get that value inside function.So, your function will look like `function myFunction(value,price)` and pass it in url. – Swati Jul 27 '20 at 15:40
  • @Swati Thank you, i really appreciate that you took time and answered my questions! – nemke nemke Jul 27 '20 at 19:28

0 Answers0