0

我正在通过此代码在 java 中捕获外部 .exe 文件的输出值

Process p = Runtime.getRuntime().exec("filepath\\myexefile.exe 5.53 46.46"); // 5.53 and 46.46 are two input orguments of exe file
BufferedReader stdInput = new BufferedReader(new 
InputStreamReader(p.getInputStream()));

String s;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);                         }
    Double d = Double.valueOf(s);
    System.out.println(d);
}

代码运行良好,它按预期显示输出 53.4429。但是,当我尝试将 53.4429 转换为 double 时,它​​会出现以下错误

Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008)
    at java.lang.Double.valueOf(Double.java:504)

知道为什么字符串不转换成双倍吗?提前致谢

4

1 回答 1

0

因为循环运行 untils为 null,然后您将传递s- 这是 now null,请记住 - 到Double#valueOf(String)

要解决此问题,请在循环内解析。s

String s;
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
    Double d = Double.valueOf(s);
    System.out.println(d);
}
于 2013-01-22T06:31:15.863 回答