我对servlets和java相当陌生,我正试图使用servlets和jdbc开发一个银行系统。一切都很好,除了当我试图存款时(即使用HTML表格字段从客户端获取一个浮动值),我得到了java.lang.NullPointerException,而且那个特定的页面没有显示出来,而是显示出HTTP 500内部服务器错误。
package co.sayan;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Deposit
*/
@WebServlet("/Dep")
public class Deposit extends HttpServlet {
private static final long serialVersionUID = 1L;
private static PreparedStatement pst =null;
/**
* @see HttpServlet#HttpServlet()
*/
public Deposit() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/account","root","");
pst = con.prepareStatement("update balance set Balance = ?");
}catch(Exception ex) {
System.out.println(ex.toString());
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body style = background:linear-gradient(red,white,red)>");
out.println("<h3>Enter the amount to be deposited</h3>");
out.println("<form action = http://localhost:8082/BankingSystem/Check>"
+ "<label for = amt>Amount:</label><br>"
+ "<input type = number id = amt name = amt><br><br>"
+ "<input type = submit value = Submit></form>");
out.println("</body></html>");
Float amt = Float.parseFloat(request.getParameter("amt"));
try {
pst.setFloat(1, amt);
int result = pst.executeUpdate();
}catch(Exception ex) {
System.out.println(ex.toString());
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
错误
SEVERE: Servlet.service() for servlet [co.sayan.Deposit] in context with path [/BankingSystem] threw
exception
java.lang.NullPointerException
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at java.base/jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.base/java.lang.Float.parseFloat(Float.java:455)
at co.sayan.Deposit.doGet(Deposit.java:60)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:688)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:844)
问题发生在我试图将值转换为float时。如果我不把它改成float,程序运行正常,但是我就不能更新数据库中的字符串值。有谁能帮助我解决这个问题。
看了上面的帖子,我现在得到了正确的答案。应该是在解析之前进行null检查。
所以它应该是
String amtStr = request.getParameter("amt");
if ( amtStr != null )
{
//processing
}
else
{
// Error message probably
}