如何在Java Servlet中将字符串转换为double?

时间:2012-06-08 08:46:51

标签: java android string double

我将自己的纬度和经度作为url参数传递给我的java servlet。我想使用hibernate将这些纬度和经度存储到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中的任何一个都不是null。我刚刚检查过,我在文件中得到了以下输出。 28.500788 77.395829。这些是我从我的Android应用程序发送的坐标。

5 个答案:

答案 0 :(得分:1)

我很确定par1或par2都是null。

修改您的代码,如下面的代码段。

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;
}

答案 1 :(得分:0)

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

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

这就是您收到NPE的原因。

尝试:

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

在进行类型转换之前,只需验证。

答案 2 :(得分:0)

req.getParameter(“param1”)是否返回null?你传递这些参数吗?

解析前检查:

if(par1 != null) lat =Double.parseDouble(par1);
if(par2 != null) lon= Double.parseDouble(par2);

答案 3 :(得分: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");
        }

答案 4 :(得分:0)

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

进行此更改并尝试。