我将我的纬度和经度作为 url 参数传递给我的 java servlet。我想使用休眠将这些纬度和经度存储到我的 mySQL 数据库中。要将 String 转换为 Double,我使用以下代码,但它给了我一个 nullPointerException。
FileWriter fstream = new FileWriter("out2.txt");
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
String par1 = req.getParameter("param1");
String par2 = req.getParameter("param2");
BufferedWriter out = new BufferedWriter(fstream);
out.append(par1);
out.append(" ");
out.append(par2);
double lat=0.0;
double lon=0.0;
try
{
lat =Double.parseDouble(par1);
}
catch(NumberFormatException e)
{
//not a double
}
try
{
lon= Double.parseDouble(par2);
}
catch(NumberFormatException e)
{
//not a double
}
我在控制台中收到以下错误:
SEVERE: Servlet.service() for servlet [org.spark.servlet.PostDataReceiverServlet] in context with path [/HelloServlet] threw exception
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:991)
at java.lang.Double.parseDouble(Double.java:510)
at org.spark.servlet.PostDataReceiverServlet.doGet(PostDataReceiverServlet.java:48)
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:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
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:405)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1773)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
如果我删除两个 parseDouble 语句,代码工作正常。@All 我很确定 par1 或 par2 都不为空。我刚刚检查了一下,我的文件中出现了以下输出。28.500788 77.395829
. 这些是我从我的 android 应用程序发送的坐标。