Android Question ArrayList traduction from Java in B4A

Francesco Maresca

Member
Licensed User
Longtime User
Hi,

I have to go through Java Object array list string.
I do this in java:
List <String> stocklist = new ArrayList <String> ();
stockList.add ("stock1");
stockList.add ("stock2");
stockList.add ("stock3");
stockList.add ("stock4");
stockList.add ("stock5");
stockList.add ("stock6");
MA.setFields (stockist);

In b4A I write:
Dim stockList As List
stockList.Initialize()
stockList.add("stock1")
stockList.add("stock2")
stockList.add("stock3")
stockList.add("stock4")
stockList.add("stock5")
stockList.add("stock6")
fb.RunMethod("setFields", Array As Object(stockList) )


But I have this error:
Caused by: java.lang.NullPointerException: Attempt to read from null array

How can I call this function? I need a different object?
Francesco
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4A Lists aren't the same java.util.ArrayLists. Try this:
B4X:
Dim al As JavaObject
al.InitializeNewInstance("java.util.ArrayList", Null)
al.RunMethod("add", Array("stock1"))
al.RunMethod("add", Array("stock2"))
fb.RunMethod("setFields", Array As Object(al) )

This is untested code, beware.
 
Upvote 0

MMORETTI964

Member
Licensed User
Longtime User
Actually the code in the first post should work. B4A List wraps java ArrayList.

The problem is somewhere else.

Erel, it seems when B4A create a List (better, when B4A add an item to list), the roomsize of the list (seen on the debugger) is for 12 item.
Yes, the size returns correctly the number (1), but... could be the library inside check the List by roomsize within a cycle?
AFAYK java takes room for List like B4A does?

As expected, the solution given by Roycefer give us the same problem (Attempt to read from null array).
Now we try to write an inline java function to create List directly from java and will post the result.
 
Upvote 0

MMORETTI964

Member
Licensed User
Longtime User
Sorry was an error because we had to initialize the class with a byte array and we doesn't.

The first write work perfectly (and in Java too the roomsize after inserting an item of a List is the same as B4A).

Thank you all for the replies.
 
Upvote 0
Top