Android Question RandomAccessFile - ReadB4XObject ERROR

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, i'm trying to understand how to use the RandomAccessFile Library, beacuse i need to store custom Types of data.
I'm always getting an error while reading, i'm confused.

The test code:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Type Mex(Text As String, Owner As String)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim m As Mex
    m.Initialize
    m.Owner = "Fred"
    m.Text = "Hi"
    
    Log("Mex1: " & m)
    
    
    Dim rf As RandomAccessFile
    rf.Initialize(File.DirInternal, "mex2.dat", False)
    rf.WriteB4XObject(m, rf.CurrentPosition)
    rf.Close
    
    Dim rf2 As RandomAccessFile
    rf2.Initialize(File.DirInternal, "mex2.dat", False)
    Dim m2 As Mex = rf2.ReadB4XObject(rf.CurrentPosition)
    Log("Mex2: " & m2)
    rf2.Close
End Sub


The error:
Error occurred on line: 45 (Main)
java.nio.BufferUnderflowException
at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:230)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadInt(RandomAccessFile.java:137)
at anywheresoftware.b4a.randomaccessfile.RandomAccessFile.ReadB4XObject(RandomAccessFile.java:319)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:6020)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:936)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:797)
** Activity (main) Resume **
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
> Log("Mex1: " & m)

Not sure this is a problem, but can the log show a Type, without specifying the members?
Shouldn't it be Log("Mex1.Text: " & Mex1.Text) ?

RBS
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
> Log("Mex1: " & m)

Not sure this is a problem, but can the log show a Type, without specifying the members?
Shouldn't it be Log("Mex1.Text: " & Mex1.Text) ?

RBS
Yes, infact Log("Mex1: " & m) prints like a map with the "[]", the problem isn't that thing (you can delete those lines)
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Shouldn't this reference rf2?
Oh yes!
Copy/Paste error.. thanks.

In the end i decided not to use RandomAccessFile for the moment. But thanks.
Maybe when it will become necessary i will have a closer look to this lib
 
Upvote 0
Top