gpt4 book ai didi

java - 错误 线程中出现异常 "main"java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 06:57:04 29 4
gpt4 key购买 nike

你好,我想从 java 生成 gams 我已经安装了 gams,这是代码gams 是一个建模应用程序,我想从 java 调用它,并且我确信路径是准确的,我应该做什么来纠正它?它给了我异常(exception)异常(exception):

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

最佳答案

看看这段代码:

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

您正在创建一个包含五个元素的数组,但只填充其中四个

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

关于java - 错误 线程中出现异常 "main"java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9160797/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com