Android Question List of Arrays or Array of Arrays

TheWind777

Active Member
Licensed User
Longtime User
I am trying to create, either, an two-dimensional string array, or a List of arrays.

I would like to load in 4 different sentences for 365 different days from a text file, then access them like:

Day(235,1) would equal the first sentence of the 235th day... Day(365, 3) would equal the 3rd sentence of the 365th day. (Day(365, 0) might equal the total number of sentences for that day... because maybe in the future they might be more than 4 sentences per day).

I have tried both methods.

I have tried declaring a list in the Global area, then populating it in a Sub.

I have tried declaring the list in the Global area and populating it in the global area.

I have tried declaring a two-dimensional array in the Global area and populating it in Sub.

I have even succeeded, if one were to count using Log(Day(365,3))

However, whenever I try an actually use that string or return it as a value, or even print it in a StringFunction message Box... it always gives me an error of java.lang.NullPointerException

I'm figuring that the problem has to do with the original definition being just a reference pointer to a string which isn't really there later.

What is the proper way of declaring (either a list which will be a list of string arrays) or a two-dimensional string array so I can fill them in either at the beginning and then access them later, or fill them in from a Sub later without getting that annoying "NullPointrException" error?

365 x 4 real strings need to be created, globally.

Then, in some fashion, either a List of Array pointers has to point to those strings.

And, after they have been declared... how does one access them?

When I was doing experiments using a list of arrays, I seemed to be setting-up the list of arrays properly... but I could then never access the arrays, and thus those string array items properly.

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim ListOfArrays As List
Dim sf As StringFunctions
Dim TimingArray(5) As String
End Sub

Sub Activity_Create(FirstTime AsBoolean)

'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")


Dim z As Short
Dim sf As StringFunctions

ListOfArrays.Initialize ' (I've also tried putting this in the Globals area)

For z = 0 To 365

Dim TimingArray(5) As String

TimingArray(0) = "4"
TimingArray(1) = "A" & NumToStr(z)
TimingArray(2) = "B" & NumToStr(z)
TimingArray(3) = "This is a test of the emergency broadcast system" & NumToStr(z)
TimingArray(4) = "D" & NumToStr(z)
ListOfArrays.Add(TimingArray)
Next

' Creates a string pointer called GetArray?

Dim GetArray() AsString



' Points the GetArray string pointer to the 25th Array in the ListOfArrays List and accesses the 25th

GetArray = ListOfArrays.Get(25)
Log(GetArray(3)) ' And, it shows "This is a test of the emergency broadcast system25"

TheString = GetArray(3) ' Doesn't cause problem to assign it. Must be a string pointer.
sf.MB(TheString) 'Gives null pointer error any time I try actually using that value

End Sub


The same exact thing happened when I tried to create a two-dimensional array.

What's the right way of doing it?
 

TheWind777

Active Member
Licensed User
Longtime User
Oh, and NumToStr is just a casting sub to convert the Number to a String

Sub NumToStr(TheNumber As Short) As String
Dim TheString As String
TheString = TheNumber
Return TheString
End Sub
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Something like this:

B4X:
Dim MyArray(365,4) As String
MyArray(0,0) = "Line One..."
MyArray(0,1) = "Line Two..."
MyArray(0,2) = "Line Three..."
MyArray(0,3) = "Line Four..."
For i = 0 To 3
     Log(MyArray(0, i))
Next

or to show all:

B4X:
Sub My_Array
     'This loop stores and logs all elements
     Dim MyArray(365,4) As String
     For mi = 0 To 364
          MyArray(mi,0) = mi & "-Line One..."
          MyArray(mi,1) = mi & "-Line Two..."
          MyArray(mi,2) = mi & "-Line Three..."
          MyArray(mi,3) = mi & "-Line Four..."
          For i = 0 To 3
               Log(MyArray(mi, i))
          Next
     Next
End Sub
 
Last edited:
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
Yeah, that's what I was doing when I was trying to do it with Arrays.

Both ways... it would seem to work when I did a 'log()' of the Array item; but then when I tried to actually use the variable it always gave me that java.lang.NullPointerException error.

Look up at my example.

I can't understand why a Log() works, but then when I try and access that Array item, it fails. Didn't my Log() just prove that the Array was there, and that the value of the array was there? And, if so, why the error?

I do a Log(GetArray(3))

and it shows "This is a test of the emergency broadcast system25", which is correct.

Then, when I actually try and use the Array value, it gives me the error.

The same thing happened when I tried doing it with a two-dimensional array. I could Log() it. It was correct. I couldn't use it without a java.lang.NullPointerException error.




Something like this:

B4X:
Dim MyArray(365,4) As String
MyArray(0,0) = "Line One..."
MyArray(0,1) = "Line Two..."
MyArray(0,2) = "Line Three..."
MyArray(0,3) = "Line Four..."
For i = 0 To 3
     Log(MyArray(0, i))
Next

or to show all:

B4X:
Sub My_Array
     'This loop stores and logs all elements
     Dim MyArray(365,4) As String
     For mi = 0 To 364
          MyArray(mi,0) = mi & "-Line One..."
          MyArray(mi,1) = mi & "-Line Two..."
          MyArray(mi,2) = mi & "-Line Three..."
          MyArray(mi,3) = mi & "-Line Four..."
          For i = 0 To 3
               Log(MyArray(mi, i))
          Next
     Next
End Sub
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
You say you tried a two dimensional array and that it doesn't work but you don't tel us what you have tried and how.
Without seeing your complete code it's impossible to give you a concrete advice.

Well, it seems to be a problem with the StringFunction.mb() function.

I can use the returned string, even with other functions from the StringFunction functions (like sf.Right(TheString,3), which grabs the right three letters from the String)... but whenever I try using the sf.mb() function, it dies with that error.

I like the simple return from StringFunction's mb function rather than the truncated MsgBox() function.

You know why it might be encountering a java.lang.NullPointerException when an String Array is passed to it? In theory, I see no reason why it's not working.



Sub Activity_Create(FirstTime AsBoolean)
Activity.LoadLayout("main")

Dim TheString AsString
Dim z AsShort
Dim sf AsStringFunctions

Dim TimingArray(366,5) AsString

For z = 0To365
TimingArray(z, 0) = "4"
TimingArray(z, 1) = "A" & NumToStr(z)
TimingArray(z, 2) = "B" & NumToStr(z)
TimingArray(z, 3) = "This is a test of the emergency broadcast system" & NumToStr(z)
TimingArray(z, 4) = "D" & NumToStr(z)
Next

TheString = TimingArray(200, 3)
TheString = sf.Right(TheString, 3)
Msgbox(TheString, "The Array Item")
sf.MB(TheString)

End Sub
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
I took the code you posted above and it runs fine with MB(). I am not sure why you are seeing this issue. What version of the StringFunctions are you using. It will show in the right hand pane just after the name.

My problem is far more extensive than just that one library.

I get two errors all the time...

java.lang.NullPointerException and particularly one that always affects lines such as:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Boolean

That one keeps showing up, generally, when I'm trying to load-in a filename with:

If File.Exists(File.DirInternal,"Init.txt") = True Then

I've also tried "If File.Exists(File.DirInternal,"Init.txt") Then", "If (File.Exists(File.DirInternal,"Init.txt") = True) Then etc. The problem will just go away for a time and then return.

I have seen that java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Boolean error other times where it seems like there's nothing wrong with the line, also. Seems to be rather random, and goes away by me either adding, or getting rid of, parenthesis that are around a Boolean statement (figured-out, usually). Unfortunately, removing parenthesis oftentimes doesn't fix the problem. Annoying.

It seems like I do proper code, yet still get the errors (like in the case I gave to you).

I am currently having trouble with one annoying section I'm trying to add. Am getting that
LogCat connected to: 0781162c
--------- beginning of /dev/log/system


--------- beginning of /dev/log/main


** Activity (main) Create, isFirst = true **
** Activity (main) Pause, UserClosed = false **
An error occurred:
(Line: 1264) If File.Exists(File.DirInternal,"Init.txt") = True Then
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Boolean
** Activity (main) Create, isFirst = true **
Error occurred on line: 183 (main)
java.lang.NullPointerException
at org.mlsoft.mlstr._ischar(mlstr.java:151)
at org.mlsoft.mlstr._wordn(mlstr.java:506)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at b4a.acim.main.afterFirstLayout(main.java:98)
at b4a.acim.main.access$100(main.java:16)
at b4a.acim.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Create, isFirst = true **


Error occurred on line: 183 (main)


java.lang.NullPointerException


at org.mlsoft.mlstr._ischar(mlstr.java:151)
at org.mlsoft.mlstr._wordn(mlstr.java:506)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at b4a.acim.main.afterFirstLayout(main.java:98)
at b4a.acim.main.access$100(main.java:16)
at b4a.acim.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Create, isFirst = true **


The line in question is... the NumWords = line in the following (copy and pasted so you can see the pertinent parts) code.

Dim Reader AsTextReader
Dim MLStringFunctions AsMLStr
Dim NumWords AsInt

IfFile.Exists(File.DirAssets,"timing.txt") Then
Reader.Initialize(File.OpenInput(File.DirAssets,"timing.txt"))
TimingLine = Reader.ReadLine
NumWords = StrToNum(MLStringFunctions.Words(TimingLine))
Reader.close

It makes no sense.

I tried re-installing Basic4Android (full version, paid-for). Nothing different

Might it be that I need to re-install the SDK?

It can't just be this program I'm working-on... because that test I did was just with a newly created module, and there was hardly anything to it.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
First off you need to format your code so it can be read easier. Before the code in your message put the code indicator like the image below.

code.jpg


Also, you should attach your project and let someone test it. It may be your code or your install. Are you using a real device?
 
Last edited:
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
First off you need to format your code so it can be read easier. Before the code in your message put the code indicator like the image below.

code.jpg


Also, you should attach your project and let someone test it. It may be your code or your install. Are you using a real device?

I just uninstalled, then re-installed the Java SDK. Did nothing.

Seems to just affect the libraries. Most of the functions I'm doing with the libraries can all be done some other way; so that's what I'll do next.

Will try making the simplest file possible, then testing it. If it does it with the simplest file, that shows something. That way it's not some stray strange pointer problem or such. When the huge program starts acting-up I always suspect thing like pointers pointing to places that aren't really what they should be, then acting as if they were proper.

The proof was when you tried exactly what I was trying and it worked on your end.
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
First off you need to format your code so it can be read easier. Before the code in your message put the code indicator like the image below.

code.jpg


Also, you should attach your project and let someone test it. It may be your code or your install. Are you using a real device?

Yes, I am using a Nexus 7 with 32GB. I am only having a problem with libraries. I re-installed Java SE Development Kit 7 Update 51. It is the 32 bit version as they said the 64 didn't work with it. Re-installed "b4a-full_32pxk.exe". adt-bundle-windows-x86_64-20131030 was just an unpack. I have everything you could possibly imagine installed.

One problem I know of is, when I click on SDK Manager.exe, a window appears and then goes away. I don't get the manager by double-clicking on it. When I go to Tools->Run AVD Manager from B4a, it also doesn't come up. Might it be that some global variable is not set properly? I remember them saying to set some global variable - but that was a long time ago.
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
First off you need to format your code so it can be read easier. Before the code in your message put the code indicator like the image below.

code.jpg


Also, you should attach your project and let someone test it. It may be your code or your install. Are you using a real device?

The project is really big. It has over 365*3 text files associated with it, so it is bigger than the attach size of this forum (besides, it is classified). The code is also quite big now.

It is easier to do experiments. That proves it isn't the code.
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
I took the code you posted above and it runs fine with MB(). I am not sure why you are seeing this issue. What version of the StringFunctions are you using. It will show in the right hand pane just after the name.

StringFunctions Version 1.04
MLStr Version 2.10

Since the problem is occurring with both libraries, it is hard to imagine that it Is the version number of the library. Since I am just being lazy, I can do everything I was doing with the libraries by hand; so I'll just concentrate on getting my own routines to do it.

It just is irritating that libraries aren't working properly.
 
Upvote 0

TheWind777

Active Member
Licensed User
Longtime User
Couldn't you, as already suggested, post your project so we could look at it and test it ?

It is in a half-broken state because of the error... and is huge because of the 900 text files.

I will not use the libraries at the moment. That will fix the problem for the time being. Both libraries do it.

Thank you. Your suggestions led me to the knowledge that it is libraries that are causing the problem. I will just not use libraries at the moment.
 
Upvote 0
Top