Android Question RandomAccessFile with (nested) list

jcredk

Member
Licensed User
Longtime User
Hi all,

I have a class the contains a list of classes objects that themself contains list.
I try to save or load it using RandomAccessFile. Saving is ok (and there seems to be a file with things in it), but loading is crashing.

There must be something wrong as I thought that with RandomAccessFile I did not had to care with the object types when saving/loading, may be it's not handling list that contains objects themselves with lists ...

Any idea, advice to find the root cause ?

My class looks like what is below:

B4X:
Sub Class_Globals
    Dim Topics As List
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   Topics.Initialize
End Sub

Public Sub Save()
   Dim raf As RandomAccessFile
   raf.Initialize(Utils.thePath, "Data.txt", False)
   raf.WriteObject(Topics, True, raf.CurrentPosition)
   raf.Close
End Sub

Public Sub Load()
   Dim raf As RandomAccessFile
   raf.Initialize(Utils.thePath, "Data.txt", True)
   Topics=raf.ReadObject(raf.CurrentPosition)
   raf.Close
End Sub

The log is:
B4X:
Topics=raf.ReadObject(raf.CurrentPosition)
java.lang.IllegalArgumentException: invalid value for field
    at java.lang.reflect.Field.setField(Native Method)
    at java.lang.reflect.Field.set(Field.java:588)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readType(RandomAccessFile.java:500)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readObject(RandomAccessFile.java:476)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readList(RandomAccessFile.java:579)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readObject(RandomAccessFile.java:467)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readMap(RandomAccessFile.java:528)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readType(RandomAccessFile.java:495)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readObject(RandomAccessFile.java:476)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readList(RandomAccessFile.java:579)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readObject(RandomAccessFile.java:467)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readHelper(RandomAccessFile.java:401)
    at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadObject(RandomAccessFile.java:362)
    at com.test.testApp.test._load(test.java:71)
 

jcredk

Member
Licensed User
Longtime User
OK, what I was affraid of ... :(
So it means I will have to implement some kind of recursive load/save in my classes that passes the "raf" variable to do the full read/write ?

Anyway thanks for the very quick answer :)
 
Upvote 0

jcredk

Member
Licensed User
Longtime User
I have understood just coming from my "bad" c# habits :)
what I meant upper was add a load/save member to my class having a raf parameter then loop through my list to load/save using this raf and its
raf.CurrentPosition member ... let's try and see ...
 
Upvote 0

jcredk

Member
Licensed User
Longtime User
Another option is to create a new Type that holds the class variables. In your class create a method that converts the object to the custom type. You can then add the custom type instead of the class objects to the lists.
I really like how this one is smart ... :)
I will try this!

In fact at the beginning I have made classes/code modules as my code is supposed to be executed either from an activity or a widget.
Can this new type be defined in the class file itself or in a code module or it needs to be defined in the activity then the class (with the risk for them to be different) ...
 
Last edited:
Upvote 0

jcredk

Member
Licensed User
Longtime User
Yes it what I discovered ...
I finally ended by changing my classes to Type and moving the member functions of my classes to a code module, and it seems that save/load works.
Thanks for that.

Just one more question:
The generated files are not really readable for humans :)
Is there a parameter in RandomAccessFile to have a less performant file format but that still allows a debugging (writing variables as strings ...) ?

Thanks in advance

Jo
 
Upvote 0

jcredk

Member
Licensed User
Longtime User
Yes, stupidly (for my debug), I set it to True ... will try at false and I should be able to guess the textual variables saved!
 
Upvote 0
Top