我是Java的新手,无法解决我的范围问题。我有一个Matlab Connect(MConnect1()/MConnect2()
)方法,需要void
。我试图从另一个类访问该方法中的变量,但获得NullPointerException
。我尝试创建一个方法getResult()
,但这也不起作用。我应该采取哪一步?
我正在尝试从void MConnect2()获取array1。
package OMatlab;
import matlabcontrol.*;
import matlabcontrol.extensions.*;
import java.util.Arrays;
public class MatlabConnectVariable extends MatlabProxyFactory{
private String MConnectStatus;
public double result1;
public String result2;
public double[][] array1;
public double[][] array2;
public int i;
public boolean conStat;
public String getMConnectStatus() {
return MConnectStatus;
}
//Default Constructor
MatlabConnectVariable()
{
if (conStat = true) {
MConnectStatus="The MatlabConnect class instance has been initiated";}
else {MConnectStatus="WARNING: The MatlabConnect class instance could not been initiated";}
}
public void MConnect1() throws MatlabConnectionException, MatlabInvocationException
{
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.setHidden(true)
.setMatlabLocation(null).build();
// MatlabProxyFactoryOptions options =
// new MatlabProxyFactoryOptions.Builder()
// .setUsePreviouslyControlledSession(true)
// .build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
//MatlabProxyFactory factory = new MatlabProxyFactory();
// create proxy
MatlabProxy proxy = factory.getProxy();
conStat = proxy.isConnected();
// ////Set a variable, add to it, retrieve it, and print the result
// proxy.setVariable("a", 5);
// proxy.eval("a = a + 6");
// result = ((double[]) proxy.getVariable("a"))[0];
// System.out.println("Result: " + result);
//call user-defined function (must be on the path)
//proxy.eval("addpath('C:\\Users\\odogu1\\Documents\\MATLAB')");
proxy.feval("data_loading_script");
//proxy.eval("rmpath('C:\\Users\\odogu1\\Documents\\MATLAB')");
proxy.setVariable("a", 1);
result1 = ((double[]) proxy.getVariable("new_data_added"))[0];
// close connection
proxy.disconnect();
}
public void MConnect2() throws MatlabConnectionException, MatlabInvocationException
{
MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()
.setUsePreviouslyControlledSession(true)
.setHidden(true)
.setMatlabLocation(null).build();
MatlabProxyFactory factory = new MatlabProxyFactory(options);
//MatlabProxyFactory factory = new MatlabProxyFactory();
// create proxy
MatlabProxy proxy = factory.getProxy();
conStat = proxy.isConnected();
//call user-defined function (must be on the path)
// proxy.feval("data_loading_script");
//Create a 4x3 array filled with random values
proxy.eval("array1 = randn(4,1)");
//Print a value of the array into the MATLAB Command Window
proxy.eval("disp(['entry: ' num2str(array1(1, 1))])");
//Get the array from MATLAB
MatlabTypeConverter processor = new MatlabTypeConverter(proxy);
double[][] array1 = processor.getNumericArray("array1").getRealArray2D();
//Print out the same entry, using Java's 0-based indexing
// System.out.println("entry: " + array1.getRealValue(2, 1));
//Convert to a Java array and print the same value again
// double[][] result2 = array1.getRealArray2D();
// System.out.println("ENTRY: " + result2[0][0]);
int p=0;
for(int i = 0; i < array1.length; i++)
{
System.out.println(Arrays.toString(array1[i]));
// result2 = Arrays.toString(array1[i]);
}
// System.out.println(Arrays.toString(result2));
// proxy.setVariable("a", 110);
// result2 = ((double[]) proxy.getVariable("a"))[0];
//result2 = ((double[]) proxy.getVariable("new_data"));
// close connection
proxy.disconnect();
}
public double[][] getResult2() {
return array1;
}
}
答案 0 :(得分:1)
看起来您将数组定义为字段,但您从未使用它。您在该方法中使用以下行定义了另一个具有相同名称的名称:
MatlabTypeConverter processor = new MatlabTypeConverter(proxy);
double[][] array1 = processor.getNumericArray("array1").getRealArray2D();
所以你不会让array1充满任何数据。只是做:
array1 = processor.getNumericArray("array1").getRealArray2D();
并尝试调用getResult2()。
答案 1 :(得分:0)
在方法getResult2()中,您将返回定义为类成员的array1,您永远不会初始化它(因此,您将始终将其置为null)。你应该改变一行
double[][] array1 = processor.getNumericArray("array1").getRealArray2D();
进入
array1 = processor.getNumericArray("array1").getRealArray2D();