0

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

4

5 回答 5

1

我很确定 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;
}
于 2012-06-08T08:56:37.397 回答
0

这两行中的一条或两条很可能不会返回任何值:

String par1 = req.getParameter("param1");
String par2 = req.getParameter("param2");

这就是您收到 NPE 的原因。

尝试:

System.out.println("par1 = " + par1 + " and par2 = " + par2);

在进行类型转换之前,只是为了验证。

于 2012-06-08T08:55:07.713 回答
0

req.getParameter("param1") 返回 null 吗?你在传递这些参数吗?

解析前检查:

if(par1 != null) lat =Double.parseDouble(par1);
if(par2 != null) lon= Double.parseDouble(par2);
于 2012-06-08T08:56:05.010 回答
0

这样试试看

        FileWriter fstream = new FileWriter(new File("out2.txt"),true);
        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
        {
///////////////////////////null Condition chk within Single try///////////

         if ((part1.equals(null)) & (part2.equals(null)))
        {
          lat =Double.parseDouble(par1);
          lon= Double.parseDouble(par2);
        }

///////////////////////////null Condition chk within Single try///////////

        }
        catch(NumberFormatException e)
        {
          System.out.println("One or both of the value is null");
        }
于 2012-06-08T09:08:32.390 回答
0
 String par1 =  (String) req.getParameter("param1");
 String par2 =  (String) req.getParameter("param2");

进行此更改并尝试。

于 2012-06-08T09:21:18.650 回答