0

I am new to Java and couldn't solve the scope problem I have. I have a Matlab Connect (MConnect1()/MConnect2()) Method which needs to be void. I am trying to access a variable in the method from another class but get a NullPointerException. I tried creating a method as getResult() but that also doesn't work. Which step should I take?

I am trying to get array1 from void MConnect2().

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

 }

2 Answers 2

1

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. Just do:

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

and try calling getResult2().

Sign up to request clarification or add additional context in comments.

1 Comment

That was totally my lack of attention. Thanks for pointing out, it works fine now. Cok tesekkurler Sinan bey.
0

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). You should change the line

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

into

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.