Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Empty Java Array in Matlab

Subject: Empty Java Array in Matlab

From: Stiphu

Date: 6 Jan, 2010 16:14:04

Message: 1 of 4

Hi there

I have a Java method "saveData":

public boolean saveData(... int[] time, ...){
   ...
   if(time.length() == 0){
      ...
   }
   ...
}

This method works fine, as long I'm calling it from Java. I can either pass an array

time = {1, 2, 3, ...};

or an array

time = new int[0] ;

But there is a problem if I'm calling this method from Matlab. I can call it like this:

time = int32([1 2 3]);

that works fine. But if I'm passing an empty value, like

time = int32([]);

a "java.lang.NullPointerException" error occurs. Looks like it's not possible to do time.length() in Java, if I'm passing the variable like that. The question now is: How can I define an empty Integer Java Array in Matlab, which I can pass as int[]? I tried to define an Integer Array like this (before i changed the definition of time from int[] to Integer[])

intArray = javaArray('java.lang.Integer',1);

But this is not an empty Array. It is an Array of the size 1 with the value time[0] = null. So how can I initialize an empty Array in Matlab like

int[] time = new int[0]; ???

Subject: Empty Java Array in Matlab

From: Ed Yu

Date: 10 Jan, 2010 01:19:03

Message: 2 of 4

Stiphu,

In your java code, check for:

if (time==null || time.length<=0) {
...
}

and pass in from MATLAB myJavaCode([]).

Hope this works for you.

Ed.

Subject: Empty Java Array in Matlab

From: Sebastian

Date: 26 Jan, 2012 18:52:10

Message: 3 of 4

Hello,

sorry for resurrecting this thread...
But is there any alternative for the solution above?

In my case the java-code I'm using is not open for modification from my side, so the possibility to check for the null-pointer is does not exist.

Any idea welcome.

Cheers,
Sebastian

"Ed Yu" wrote in message <hib9u7$go3$1@fred.mathworks.com>...
> Stiphu,
>
> In your java code, check for:
>
> if (time==null || time.length<=0) {
> ...
> }
>
> and pass in from MATLAB myJavaCode([]).
>
> Hope this works for you.
>
> Ed.

Subject: Empty Java Array in Matlab

From: Yair Altman

Date: 27 Jan, 2012 09:01:10

Message: 4 of 4

"Sebastian" wrote in message <jfs7cq$kg8$1@newscl01ah.mathworks.com>...
> Hello,
>
> sorry for resurrecting this thread...
> But is there any alternative for the solution above?
>
> In my case the java-code I'm using is not open for modification from my side, so the possibility to check for the null-pointer is does not exist.
>
> Any idea welcome.
>
> Cheers,
> Sebastian
>
> "Ed Yu" wrote in message <hib9u7$go3$1@fred.mathworks.com>...
> > Stiphu,
> >
> > In your java code, check for:
> >
> > if (time==null || time.length<=0) {
> > ...
> > }
> >
> > and pass in from MATLAB myJavaCode([]).
> >
> > Hope this works for you.
> >
> > Ed.


This creates an empty (0-length) array of java.util.Objects:

>> toArray(java.util.Vector)
ans =
java.lang.Object[]:
    [0 element array]

Perhaps this will work for you; if not, perhaps you can type-cast the result into what your Java method expects.

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us