Im currently new in java ee. I need help on how to pass my data from jsp to my servlet to fil up my query. Whenever i run, the error is being catch and i print out the exception. It says java.null.exception.. I couldn’t find out where my error is..pls help me… thanks..
Here is my code:
UpdateProductServlet.java
try {
String description = request.getParameter("description");
float price = Float.parseFloat(request.getParameter("price"));
String size = request.getParameter("size");
int aisle_id = Integer.parseInt(request.getParameter("aisle"));
int id = Integer.parseInt(request.getParameter("id"));
ProductsDao productsDao = new ProductsDao();
productsDao.updateProduct(description, price, size, aisle_id, id);
} catch (Exception e) {
out.println(e);
}
and my ProductDao.java
public void updateProduct(String description, float price, String size, int aisle_id, int id)
throws ClassNotFoundException, SQLException {
Connection conn = null;
PreparedStatement stmt = null;
String sql = "UPDATE products "
+ "SET description = ?, price = ?, size = ?, aisle_id = ? "
+ "WHERE id = ? ";
try {
conn = ConnectionFactory.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setString(1, description);
stmt.setFloat(2, price);
stmt.setString(3, size);
stmt.setInt(4, aisle_id);
stmt.setInt(5, id);
stmt.executeUpdate();
} finally {
if (conn != null) {
conn.close();
}
if (stmt != null){
stmt.close();
}
}
}
and this is my ProductById.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
function functionEdit()
{
document.getElementById("edit").type="hidden";
document.getElementById("update").type="button";
document.getElementById("delete").type="hidden";
document.getElementById("cancel").type="button";
document.getElementById("description").disabled="";
document.getElementById("size").disabled="";
document.getElementById("price").disabled="";
document.getElementById("select").disabled="";
}
function functionUpdateRecord(){
if(confirm('Update existing record?')){
document.location.href="/MachineProblemWebApp/UpdateProductServlet";
alert('Update success!');
document.location.href="/MachineProblemWebApp/GetAllProductsServlet";
}else{
alert('No changes have been made!');
}
}
</script>
</head>
<body>
<table border="0">
<tr>
<td>id: </td>
<td><input type="text" name="id" id="id" value="${product.id}" disabled="disabled"></td>
</tr>
<tr>
<td>Description: </td>
<td><input type="text" name="description" id="description" value="${product.description}" disabled="disabled"></td>
</tr>
<tr>
<td>Aisle </td>
<td><select name="aisle" disabled="disabled" id="select">
<c:forEach var="aisle" items="${aisles}" >
<option value="${aisle.id}" ${(aisle.id==product.aisle.id)?"selected" : ""}>${aisle.description}</option>
</c:forEach>
</select></td>
</tr>
<tr>
<td>Size: </td>
<td><input type="text" name="size" id="size" value="${product.size}" disabled="disabled"></td>
</tr>
<tr>
<td>Price: </td>
<td><input type="text" name="price" id="price" value="${product.price}" disabled="disabled"></td>
</tr>
<tr>
<td><input type="button" name="edit" id="edit" onClick="functionEdit()" value="Edit" >
<input type="hidden" name="update" id="update" onClick="functionUpdateRecord()"value="Update">
<input type="button" name="delete" id="delete" value="Delete">
<input type="hidden" name="cancel" id="cancel" value="Cancel"></td>
</tr>
</table>
</body>
is this the stack trace? posted in Apache Tomcat Log?
Dec 10, 2012 7:38:13 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [UpdateProductServlet] in context with path [/MachineProblemWebApp] threw exception
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008)
at java.lang.Float.parseFloat(Float.java:452)
at grocerific.ui.UpdateProductServlet.processRequest(UpdateProductServlet.java:45)
at grocerific.ui.UpdateProductServlet.doGet(UpdateProductServlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Dec 10, 2012 9:36:40 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextDestroyed()
Dec 10, 2012 9:36:40 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextDestroyed()
Dec 10, 2012 9:36:46 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Dec 10, 2012 9:36:46 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Dec 10, 2012 9:36:46 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@8a47d9')
It seems you try to pass the data whit:
document.location.href="/MachineProblemWebApp/UpdateProductServlet";and it is not possible, because this line just redirect to “UpdateProductServlet” without sending the data.
you need to use the form tag to submit data.
in the link below you can see how to set the form tag:
http://www.w3schools.com/html/html_forms.asp