简体   繁体   English

从void方法返回数组

[英]Return Array from void Method

I am new to Java and couldn't solve the scope problem I have. 我是Java新手,无法解决我遇到的范围问题。 I have a Matlab Connect ( MConnect1()/MConnect2() ) Method which needs to be void . 我有一个Matlab Connect( MConnect1()/MConnect2() )方法,该方法必须为void I am trying to access a variable in the method from another class but get a NullPointerException . 我试图从另一个类访问方法中的变量,但得到NullPointerException I tried creating a method as getResult() but that also doesn't work. 我尝试将方法创建为getResult()但这也不起作用。 Which step should I take? 我应该走哪一步?

I am trying to get array1 from void MConnect2(). 我正在尝试从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;
    }

 }

It looks like you defined an array as a field but you never used it. 看起来您已将数组定义为字段,但从未使用过。 You defined another one with same name in that method with the following lines: 您在该方法中用以下几行定义了另一个同名:

 MatlabTypeConverter processor = new MatlabTypeConverter(proxy);
 double[][] array1 = processor.getNumericArray("array1").getRealArray2D();

So you won't get array1 filled with any data. 因此,您不会让array1充满任何数据。 Just do: 做就是了:

 array1 = processor.getNumericArray("array1").getRealArray2D();

and try calling getResult2(). 并尝试调用getResult2()。

In the method getResult2() you are returning the array1 defined as a class member, which you never initialize (ergo you'll always get it null). 在方法getResult2()中,您将返回定义为类成员的array1,而您无需对其进行初始化(因此,总是将其设为null)。 You should change the line 你应该换线

double[][] array1 = processor.getNumericArray("array1").getRealArray2D();

into 进入

array1 = processor.getNumericArray("array1").getRealArray2D();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM