Invalid Value for field?

NFOBoy

Active Member
Licensed User
Longtime User
Am using this to save an array:

B4X:
Dim raf As RandomAccessFile    
   Dim filename As String
      
   filename = "SomeFile" & a_Variable & ".sav"
   raf.Initialize(File.DirInternal, filename , False)
   raf.WriteObject(someArray, True, raf.CurrentPosition)
   raf.Flush
   raf.Close   
   ToastMessageShow("File saved", True)

and then am trying to use this to get the array back in when I restart

B4X:
Dim fd As FileDialog    
   Dim Ret As Int    
   fd.FastScroll = True    
   fd.FilePath = File.DirInternal    
   fd.FileFilter = ".sav"    
   Ret = fd.Show("Please Select File", "Load", "Cancel", "Delete",Null)        
   Log(Ret)
   If Ret = -1  Then
      Dim someArray() As MyType
      Dim raf As RandomAccessFile
      raf.Initialize(File.DirInternal, fd.ChosenName, True)
      somearray = raf.ReadObject(raf.CurrentPosition)
      old_MyTypeArray = somearray
   End If

But I am getting an error of Invalid Value for Field, IllegalArgumentException on the raf.ReadObject?

LogCat connected--------- beginning of /dev/log/main
main_activity_create (B4A line: 86)
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.readArray(RandomAccessFile.java:552)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readObject(RandomAccessFile.java:470)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.readHelper(RandomAccessFile.java:401)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadObject(RandomAccessFile.java:362)
at b4a.pasagosoft.nn.minesweeper.main._activity_create(main.java:377)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
at b4a.pasagosoft.nn.minesweeper.main.afterFirstLayout(main.java:84)
at b4a.pasagosoft.nn.minesweeper.main.access$100(main.java:16)
at b4a.pasagosoft.nn.minesweeper.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalArgumentException: invalid value for field


Ross
 
Last edited:

NFOBoy

Active Member
Licensed User
Longtime User
So, can't save Class mix the way I was hoping...

Ok,

so woke up this morning and looked a little deeper.

My somearray() of myType is actually a class with Class Globals:


private doubleArray() as double
private someDobble as double

(so I guess my sample should have said somearray() as Class)

After searching through and realizing I needed to use a List() to store each array in, (I could have made a lot of things more efficient if I new now to use List instead of Array()... but that's why I'm doing this particular project!)

So now I am saving two things:

someList() as List 'to store each doubleArray()
someList2 as List 'to store each double

now RandomAccessFile works as it should!
 
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
Invalid Value for Field, take 2

Hi all... I am also getting the "Invalid Value for Field" error. I have simplified everything as much as I can, and I still get the error. The odd thing is, I am using RAF in two other places and it works fine. I just cannot see what I am doing wrong here. Including my code below.

B4X:
Sub TestRAF
   Dim RAF As RandomAccessFile
   Dim TestAlarmList As List
   Dim ThisTestAlarm As testgeolarm 'this is a class containing a single string
   
   ThisTestAlarm.Initialize
   TestAlarmList.Initialize

   RAF.Initialize(File.DirDefaultExternal, "TestAlarmList.dat", False)
   RAF.WriteObject(TestAlarmList, False, RAF.CurrentPosition)

   Msgbox("Empty File Saved", "TestRAF")
   TestAlarmList.Initialize

   If (File.Exists(File.DirDefaultExternal, "TestAlarmList.dat")) Then
      RAF.Initialize(File.DirDefaultExternal, "TestAlarmList.dat", False)
     TestAlarmList = RAF.ReadObject(RAF.CurrentPosition)
   End If
   Msgbox("Empty File Read", "TestRAF")

   TestAlarmList.Add(ThisTestAlarm)
   RAF.Initialize(File.DirDefaultExternal, "TestAlarmList.dat", False)
   RAF.WriteObject(TestAlarmList, False, RAF.CurrentPosition)

   Msgbox("Populated File Saved", "TestRAF")
   TestAlarmList.Initialize

   If (File.Exists(File.DirDefaultExternal, "TestAlarmList.dat")) Then
      RAF.Initialize(File.DirDefaultExternal, "TestAlarmList.dat", False)
     TestAlarmList = RAF.ReadObject(RAF.CurrentPosition)  '<<<<< THIS LINE FAILS WITH INVALID ARGUMENT EXCEPTION?
   End If
   Msgbox("Populated File Read", "TestRAF")
End Sub

I took all the relevant code and created a single Test method, and this is in my Main Activity_Create, so I can try to rapidly troubleshoot.

Any suggestion greatly appreciated, this is killin me. Thanx!!

David
 
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
Log Files

Here you go...

LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


main_testraf (B4A line: 150)


TestAlarmList = RAF.ReadObject(RAF.CurrentPosition) '<<<<< THIS LINE FAILS WITH INVALID ARGUMENT EXCEPTION?
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.readHelper(RandomAccessFile.java:401)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadObject(RandomAccessFile.java:362)
at siriussqa.geolarms.main._testraf(main.java:1037)
at siriussqa.geolarms.main._activity_create(main.java:259)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:165)
at siriussqa.geolarms.main.afterFirstLayout(main.java:84)
at siriussqa.geolarms.main.access$100(main.java:16)
at siriussqa.geolarms.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
java.lang.IllegalArgumentException: invalid value for field


** Activity (main) Resume **
** Activity (main) Create, isFirst = true **




Thanx!
David
 
Upvote 0

wy328

Member
Licensed User
Longtime User
try to put the array into a map, and save the map with RAF lib
and i think raf.Flush is called in raf.Close, so no need to call it before close
 
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
Erel -- I inserted the RAF.Close statements, it did not make any difference. I am not really sure what you meant with your second point. I also reviewed the tutorial again, and tried inserting a new Dim statement just prior to each Initialize statement; still no difference at all.

Still searching, but thanx all...

David
 
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
Ahh, yes. The test was built up from copy/paste snippets of actual code, and I ALWAYS verify the existence or availability of any external resource before I try to access it... just habit from too many bad surprises. :D

So, then I have addressed the initial replies and still have no success. This is very perplexing, and frustrating, as I know this is the proper methodology to do what I am trying to do, which is save a list that contains records of text/numeric data. To have to write my own functions to parse that into text strings and use File.WriteList and then File.Readlist and another function to reassemble seems very inefficient, so I would really love to figure this out, but I have lost 3 days on it now. :(

Any other ideas?

David
 
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
I can. It is a bit of a mess, my first Android project, and I am chunking out many basic pieces of functionality as I learn them, but here it is, such that it is, lots of msgboxs as I generally prefer them to log statements. :D

Thanx for all your help.

David

Oh!! Well, apparently, No, I cannot. I get this message...

Your file of 689.4 KB bytes exceeds the forum's limit of 390.6 KB for this filetype.

Wow, that seems really small... hmmm, what now?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
A question, perhaps irrelevant: Apart from the list, are you writing anything else at this file?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
In order to compile I had to remove the 'dim x as id', have no idea what this is.
Anyway, here's one correction
B4X:
TestAlarmList.Add(ThisTestAlarm.name )
instead of
B4X:
TestAlarmList.Add(ThisTestAlarm)
I think this should show no error. At least logs ran ok.
 
Last edited:
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
Hmmmmmm

Oh, sorry, that uses a custom lib I have installed... it is just a demo function to prove I could use the lib, no real relation to this problem.

As for your change... well, that is not my intent. TestAlarmList is meant to be a list of TestAlarms, not just their names. Your change makes it a list of strings, which is a simple thing to manage... I need it to be a list of records. Ultimately, the TestAlarm class will have many strings and numbers in it. This is why I choose RAF over File.WriteList in the first place. So unless I am just badly misunderstanding something, that is not a solution for me.

Anyone?

David
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The answer is simple. You cannot serialize classes. Classes (unlike Types) hold a complete context.

You can add a method to your class that returns a Type with all the required fields and then save this type.

Some tips:
- Your icon file is quite large. It may cause problems with some launchers.
- Use Log instead of Msgbox.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
In fact, my impression from the code I saw and the demands described, is that a type would be sufficient enough instead of a class in this case.
 
Upvote 0

SiriusDG

Member
Licensed User
Longtime User
Type or Class

Well, a little confused. First, at this point, yes a type would suffice... I went with a class because ultimately I plan on having methods to do all the work, so the classes would be more robust than they are now. However, my priority was to make sure I could save and retrieve the data before doing anything with it... perhaps a bad choice made unknowingly. However, I thought the whole point of RAF was that it would save objects? BTW, you say I am trying to save a class... aren't I really trying to save an object? If I am wrong here, then I am missing something fundamental in my grasp of OOP. And since I am calling WriteList/ReadList, that means I would have to convert before putting into the list... but then, the app at large uses the lists, so I would have to change the design of the app to NOT expect to find an object in the list... which seems to more or less obviate the whole OO approach. So I am a little confused.

Image files, yes, I was just getting something in place, making it proper will come a bit later... I need to understand the appropriate approach for being compatible with all the various screen layouts, that is still down on my list a ways.

MsgBox vs Log... I use both, and also Toast, for debugging, but the thing I like about MsgBox is that it pauses the app so I can see the state when the msg fired. Just a personal pref for specific stuff. ;)

Thanx so much for all your help, obviously I need to rethink this a bit.

David

OOPsss --- I should not type before I have had my morning tea. I do not use WriteList/ReadList, I use WriteObject/ReadObject. And doing some deeper digging, I found a similar post which has about the same reply... the "Object" is restricted to certain objects, including Lists OR user defined types, but not Lists OF objects. Hmmm, that was my basic misunderstanding, that housing everything in a List made it legal. Still, if I have to convert the data contents of every object into a type just to save it, and then back, I am not sure I really save much work over the basic File.WriteList... need more caffiene...
 
Last edited:
Upvote 0
Top