错误线程“main”java.lang.NullPointerException中的异常

时间:2012-02-06 13:29:09

标签: java file

你好我想从java中生成gams我已经安装了gams,这里是代码 gams是一个建模应用程序我想从java调用它,我确信路径是准确的,我该怎么做才能纠正它?它给了我例外 例外:

    Exception in thread "main" java.lang.NullPointerException
            at java.lang.ProcessBuilder.start(ProcessBuilder.java:441)
            at java.lang.Runtime.exec(Runtime.java:593)
            at java.lang.Runtime.exec(Runtime.java:466)
            at gams.RunGAMS.main(RunGAMS.java:27)

Java Result: 1

这是我从this link

更改的代码
package gams;

import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;

class RunGAMS {

      public static void main(String[] args) {
              System.out.println("Start");
              String[] cmdArray = new String[5];
              cmdArray[0] ="C:"+File.separator+"Program Files"+File.separator+"GAMS23.7" + File.separator +"gams.exe";
              cmdArray[1] ="C:"+File.separator+"Documents and Settings"+File.separator+"Parsa"+File.separator+"My Documents"+File.separator+"gamsdir"+File.separator+"projdir" + File.separator +"trnsport.gms";
              cmdArray[2] ="C:"+File.separator+"Documents and Settings"+File.separator+"Parsa"+File.separator+".nbi"+ File.separator+"tmp";
              cmdArray[3] ="LO=3";

              try {
                     Process p = Runtime.getRuntime().exec(cmdArray);
                     BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                     String s = null;
                     while((s=stdInput.readLine()) !=null){
                        System.out.println(s);
                     }
                     p.waitFor();

              }
              catch (java.io.IOException e )
              {
                     System.err.println(">>>>" + e.getMessage() );
                     e.printStackTrace();
              }
              catch (InterruptedException e )

              {
                     System.err.println(">>>>" + e.getMessage() );
                     e.printStackTrace();
              }
              System.out.println("Done");
      }
}

3 个答案:

答案 0 :(得分:7)

看看这段代码:

String[] cmdArray = new String[5];
cmdArray[0] = ...;
cmdArray[1] = ...;
cmdArray[2] = ...;
cmdArray[3] = ...;

您正在创建一个五个元素的数组,但只填充四个元素。

如果您只想要四个参数,请尝试将数组大小更改为4.

答案 1 :(得分:2)

避免创建尺寸错误的数组的方法是使用一个衬垫。

String sep = File.separator;
String[] cmdArray = {
          "C:"+sep+"Program Files"+sep+"GAMS23.7" + sep +"gams.exe",
          "C:"+sep+"Documents and Settings"+sep+"Parsa"+sep+"My Documents"+sep+"gamsdir"+sep+"projdir" + sep +"trnsport.gms",
          "C:"+sep+"Documents and Settings"+sep+"Parsa"+sep+".nbi"+ sep+"tmp",
          "LO=3"};

或缩短它。

String[] cmdArray = "C:/Program Files/GAMS23.7/gams.exe,C:/Documents and Settings/Parsa/My Documents/gamsdir/projdir/trnsport.gms,C:/Documents and Settings/Parsa/.nbi/tmp,LO=3".replaceAll("/", File.separator).split(",");

答案 2 :(得分:0)

您的路径中有一个空格,只需看下面的行,

cmdArray[0] ="C:"+File.separator+"Program Files"+File.separator+"GAMS23.7" + File.separator +"gams.exe";  

在这一行中你定义了“程序文件”,它包含一个空格,所以基本上你的变量cmdArray [0]只存储像= C:\ Program这样的保持值,它在空格后终止。与其他数组元素一样,它正在发出Nul Pointer Exception。