Android Question Type variable in list/file crash

techknight

Well-Known Member
Licensed User
Longtime User
So, I have a type and list of this:

B4X:
Type PlayerInfo (Number As String, Name As String, Points As Int, Fouls As Int, OnCourt As Boolean, Slot As Int)
Dim HomeRoster As List

HomeRoster is Initialized during the app launch, So lets say that I do this:
B4X:
Dim PI as PlayerInfo
PI.Initialize

PI.Name = "TestPlayer"
PI.Number = "12" 'I use a string here because you have a player jersey of 00.
HomeRoster.Add(PI)

Notice I didnt set the fouls or points as they arnt necessary at this point.

So then I save the list into a file, Like this:
B4X:
File.WriteList(File.DirInternal, "/rosterv2/" & side & "/" & filename, HomeRoster)

Then later on, I recall the file with this subroutine:
B4X:
Sub LoadRoster(filename As String, side As String)
    Dim PI As PlayerInfo
    PI.Initialize
    Dim I As Int
    If side = "home" Then 
        HomeRoster = File.ReadList(File.DirInternal, "/rosterv2/" & side & "/" & filename)
        For I = 0 To HomeRoster.Size - 1 'Remove Player points, Fouls, and On Court status.
            PI = HomeRoster.Get(I)
            Log(PI)
            PI.Fouls = 0
            PI.Points = 0
            PI.OnCourt = False
            HomeRoster.Set(I, PI)
        Next
    Else
        GuestRoster = File.ReadList(File.DirInternal, "/rosterv2/" & side & "/" & filename)
        For I = 0 To GuestRoster.Size - 1 'Remove Player points, Fouls, and On Court status.
            PI = GuestRoster.Get(I)
            PI.Fouls = 0
            PI.Points = 0
            PI.OnCourt = False
            GuestRoster.Set(I, PI)
        Next
    End If
    LoadPlayers
End Sub

But here is the problem. It crashes on PI.Fouls = 0 point with this:

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (servicemain) Create ***
** Service (servicemain) Start **
releasing KeepAlive
No wakelock.
turning screen on
using partialLock
*** Service (servicetxrx) Create ***
** Service (servicetxrx) Start **
---- AppUpdating.ReadCurVN
UpdateComplete - time: 10:26:57
---- AppUpdating.ReadWebVN
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
---- AppUpdating.JobDone --
UpdateComplete - time: 10:26:57
[Number=31, Name=SIX, IsInitialized=true
Error occurred on line: 1394 (Main)
java.lang.RuntimeException: Field: Fouls not found in: java.lang.String
    at anywheresoftware.b4a.shell.Shell$FieldCache.getField(Shell.java:908)
    at anywheresoftware.b4a.shell.Shell.setField(Shell.java:655)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.agraham.dialogs.InputDialog$FileDialog$2.onClick(InputDialog.java:2079)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5602)
    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:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
Error occurred on line: 1394 (Main)
java.lang.RuntimeException: Unexpected command: 0
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:417)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.agraham.dialogs.InputDialog$FileDialog$2.onClick(InputDialog.java:2079)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5602)
    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:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.OutOfMemoryError: java.lang.Object[] of length 1963329536 exceeds the VM limit
    at anywheresoftware.b4a.shell.ShellConnector.readList(ShellConnector.java:220)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:366)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.agraham.dialogs.InputDialog$FileDialog$2.onClick(InputDialog.java:2079)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5602)
    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:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)

This is where I am stuck.
 

DonManfred

Expert
Licensed User
Longtime User
You really should check the Documentation

Method_636.png
File. WriteList (Dir As String, FileName As String, List As List)

Writes each item in the list as a single line.
Note that a value containing CRLF will be saved as two lines (which will return two item when read with ReadList).
All values will be converted to strings.
Example:
File.WriteList (File.DirInternal, "mylist.txt", List1)

Use Randomaccessfile and use WriteB4XObject/ReadB4XObject
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Ok, well that doenst matter yet because now I have a new and even bigger problem. My player name/number input loop that adds to the list one at a time keeps alerting me "Warning: same object added to list multiple times." and overwrites the previous entry. Technically it is the same object, PI, but it contains new data from the loop.

So now I am totally lost.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I cant play videos here, our firewall has most of that stuff blocked. I need text. It shows the video but its blurry and wont play.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Youtube is blocked at the firewall level. Policies.

Anyways, typing in Collections in search really didnt give me anything useful.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Ugh. Why cant I just get a darn article? I hate videos anyways.

I looked up the randomaccessfile method of saving objects as you specified so I am trying that now to see if it fixes my first issue.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Ill try those and see what I come up with. Thanks.

I tried randomaccessfile WriteB4XObject on my settings map, and its just coming back MyMap{} with nothing in it so I got more research to do.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I took the time out and watched the video on my phone during my lunch break and I see hes calling a subroutine to return the data from type instead of using type directly like I was doing. Is that correct?
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I just did this:

B4X:
HomeRoster.Add(InsertPlayerIntoTable(PI.Name, PI.Number, PI.Points, PI.Fouls, PI.OnCourt)) 'Insert new player information into the roster table.

instead of
B4X:
HomeRoster.Add(PI)

With given sub:
B4X:
Sub InsertPlayerIntoTable(Name As String, Number As String, Points As String, Fouls As String, OnCourt As Boolean) As PlayerInfo
    Dim PI As PlayerInfo
    PI.Initialize
    PI.Name = Name
    PI.Number = Number
    PI.Points = Points
    PI.Fouls = Fouls
    PI.OnCourt = OnCourt
    PI.Slot = 0
    Return PI
End Sub

And this works. Not sure why it makes a difference but I guess things arnt always as simple as they seem.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
According to erels tutorial, this method:

B4X:
Dim PI as PlayerInfo = HomeRoster.Get(0)
PI.Points = 3 <-Directly manipulates the data in the list.

That code above blows my mind logically. its a Get, but yet the variable manipulates the data directly in the list. I am so not used to that. lol.

And here I was doing this: (which works too)
B4X:
PI = HomeRoster.Get(0)
PI.Points = 3
HomeRoster.Set(PI, 0)
 
Upvote 0
Top