openGeeksLab / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

Vector.toArray returning null in ios.newVM #1276

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is an issue when running an existing app built with ios.newVM=true. It 
runs fine in the simulator and it runs fine on the device using ios.newVM=false.

The app is running on an iPod 5G running iOS 8.1.2

I have built a Vector of about 20 Strings, i.e. 

    buttons.addElement(string);

Once built I am trying to convert the Vector to an array with:

    String[] buttonArray = (String[]) buttons.toArray(new String[1]);

On the new VM this method always returns null. The array I pass in is not big 
enough to store the results so as per the JavaDocs I am expecting it to create 
a new array.

This code is deep in my app and I have not built a test case, I am hoping this 
will be an easy problem to see. If not I can try to build a test case.

Original issue reported on code.google.com by mwarn...@readerware.com on 12 Jan 2015 at 1:11

GoogleCodeExporter commented 9 years ago
This won't work and is generally a very bad practice. Sun made a big mistake by 
supporting that functionality!
Use buttons.toArray(new String[buttons.size()]);

This is a horrible thing to support because what you are essentially doing is:
Allocating a redundant array which is immediately discarded.
Using reflection to detect the array type.
Using reflection to allocate a new Array.

All of that HUGE overhead can be avoided by one call to the size method...

Original comment by shai.almog on 12 Jan 2015 at 6:49