我将纬度和经度作为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应用发送的坐标。

最佳答案

我很确定par1或par2为空。

像下面的代码片段一样修改您的代码。

public static double parseDoubleSafely(String str) {
    double result = 0;
    try {
        result = Double.parseDouble(str);
    } catch (NullPointerException npe) {
        //sysout found null
        return 0;
    } catch (NumberFormatException nfe) {
        //sysout NFE
        return 0;
    }
    return result;
}

08-04 23:29