I have a html form and I need to get the form data in servlet, then i need to do some mathematical operations with that data. But when I am getting null pointer exception while performing the mathematical operation. I am not able to figure this out. Any help would be appreciated. Thanks.. following is my piece of code.
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InterestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String amount=request.getParameter("wpamt");
String time=request.getParameter("prd");
int amt = Integer.parseInt(amount.trim());
int period = Integer.parseInt(time.trim());
int interest=(amt * period)/100;
out.println("for"+period+"years the interest would be"+interest);
}
}
If this is where you are getting a NullPointerException (hard to tell without a stack trace and line numbers), one of the parameters is missing (not posted by the client).
If it makes sense to proceed with default values, you can do
Note that you will also get exceptions if the parameter contains something that is not a number.