2012-06-08 85 views
0

我將我的緯度和經度作爲url參數傳遞給我的java servlet。我想用hibernate將這些經度和緯度存儲到mySQL數據庫中。將String轉換爲Double我使用下面的代碼,但它給了我一個nullPointerException。如何將字符串轉換爲Java Servlet中的double?

  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語句。 @所有我很確定par1或par2不是null。我只是檢查了一下,然後我在文件中輸出了以下內容。 28.500788 77.395829。這些是我從我的Android應用程序發送的座標。

+0

你確定'par1'不是null嗎? – nullpotent

+0

我很肯定part1是空的,因爲當你嘗試解析它時你得到了NPE。有了這個說法,你的問題在於你的getParameter行 –

+0

是的。之前,我將這兩個參數寫入文件,並且工作正常。 – rishiag

回答

0

最有可能的一個或兩個兩行不返回任何值:

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

這就是爲什麼您會收到NPE。

嘗試:

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

在你做類型轉換,只是爲了驗證。

0

is req.getParameter(「param1」)returns null?你傳遞這些參數嗎?

檢查分析之前:

if(par1 != null) lat =Double.parseDouble(par1); 
if(par2 != null) lon= Double.parseDouble(par2); 
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; 
} 
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"); 
     } 
0
String par1 = (String) req.getParameter("param1"); 
String par2 = (String) req.getParameter("param2"); 

讓這種改變和嘗試。

相關問題