B4J Question [Solved] java.lang.IllegalArgumentException: array element type mismatch when using JavaObject.InitializeArray()

aeric

Expert
Licensed User
Longtime User
Error occured at line #57:

Waiting for debugger to connect...
Program started.
Error occurred on line: 57 (Main)
java.lang.IllegalArgumentException: array element type mismatch
at java.lang.reflect.Array.set(Native Method)
at anywheresoftware.b4j.object.JavaObject.InitializeArray(JavaObject.java:109)
at b4j.example.main._testwrite(main.java:91)
at b4j.example.main._appstart(main.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.main(main.java:29)

Program terminated (StartMessageLoop was not called).

[CODE lang="b4x" highlight="57"]'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#AdditionalJar: jdbf-1.2.jar
#End Region

Sub Process_Globals

End Sub

Sub AppStart (Args() As String)
TestWrite
End Sub

Sub TestRead
' Set charset to GBK
Dim CSet As JavaObject
CSet.InitializeStatic("java.nio.charset.Charset")
Dim cs As Object = CSet.RunMethod("forName", Array As String("GBK"))

' Initialize DBF Reader
Dim dbfReader As JavaObject
dbfReader.InitializeNewInstance("com.hexiong.jdbf.DBFReader", Array As Object ("testwrite.dbf"))

' Get field name
Dim FieldsName As String
Dim FieldsCount As Object = dbfReader.RunMethod("getFieldCount", Null)
For i = 0 To FieldsCount - 1
Dim Fld As JavaObject = dbfReader.RunMethod("getField", Array As Object(i))
Dim Nam As String = Fld.RunMethod("getName", Null)
FieldsName = FieldsName & $"${Nam} | "$
Next
Log(FieldsName)

' Read fields
i = 0
Do While dbfReader.RunMethod("hasNextRecord", Null)
Dim res() As Object = dbfReader.RunMethod("nextRecord", Array As Object(cs))
Dim row As String
For Each Field In res
row = row & $"${Field} | "$
Next
Log(row & CRLF)
i = i + 1
Loop
Log("Total Count: " & i)

' Close the dbf file
dbfReader.RunMethod("close", Null)
End Sub

Sub TestWrite
' Initialize JDB Fields
Dim JDBField As JavaObject

Dim col1 As JavaObject = JDBField.InitializeArray("com.hexiong.jdbf.JDBField", Array As Object("ID", "C".As(Char), 8, 0))
Dim col2 As JavaObject = JDBField.InitializeArray("com.hexiong.jdbf.JDBField", Array As Object("Name", "C".As(Char), 254, 0))
Dim col3 As JavaObject = JDBField.InitializeArray("com.hexiong.jdbf.JDBField", Array As Object("TestN", "N".As(Char), 20, 0))
Dim col4 As JavaObject = JDBField.InitializeArray("com.hexiong.jdbf.JDBField", Array As Object("TestF", "F".As(Char), 20, 6))
Dim col5 As JavaObject = JDBField.InitializeArray("com.hexiong.jdbf.JDBField", Array As Object("TestD", "D".As(Char), 8, 0))

Dim JDBFields As JavaObject
Dim fields() As Object = JDBFields.InitializeArray("com.hexiong.jdbf.JDBField", Array(col1, col2, col3, col4, col5))

' Initialize DBF Writer
Dim dbfWriter As JavaObject
dbfWriter.InitializeNewInstance("com.hexiong.jdbf.DBFWriter", Array As Object ("testwrite.dbf", fields))


Dim row1 As Object = Array("1", "hexiong ", 500, 500.123, DateTime.Date(DateTime.Now))
Dim row2 As Object = Array("2", " hefang ", 600, 600.234, DateTime.Date(DateTime.Now))
Dim row3 As Object = Array("3", "hexi01234567890123456789012345678901234567890123456789" & _
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" & _
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", 600, 600.234, DateTime.Date(DateTime.Now))
Dim row4 As Object = Array("4", "heqiang", 700, 700.456, DateTime.Date(DateTime.Now))

Dim records() As Object
records = Array As Object(row1, row2, row3, row4)
For i = 0 To records.Length - 1
dbfWriter.RunMethod("addRecord", Array As Object(records(i)))
Next

' Close the dbf file
dbfWriter.RunMethod("close", Null)

Log("testwrite.dbf write finished.......")
TestRead
End Sub[/CODE]
 

Attachments

Solution
Still no jdbf-1.2.jar in the archive so I can't run it. Your link only gives the source code. However I guess you want to do this - which is not what your code in the project does.

Java:
JDBField[] fields = { new JDBField("ID", 'C', 8, 0),
                     new JDBField("Name", 'C', 32, 0),
                     new JDBField("TestN", 'N', 20, 0)
                     new JDBField("TestF", 'F', 20, 6) }
You need to do something like this to get a JDBField instance and then put that instance in an array of Objects. I haven't tried this as I don;t have the jdbf jar.
B4X:
Dim JDBFieldAs JavaObject
JDBField.InitializeNewInstance("com.hexiong.jdbf.", Array As Object("ID", "C".As(Char), 8, 0))
I can't download your archive and can't find jdbf-1.2.jar to look at but your code is trying to create an array of com.hexiong.jdbf.JDBField instances and it seems that the values you give for the array contents cannot be cast to a com.hexiong.jdbf.JDBField type.

EDIT: The forum must have had a glitch. I can now download the archive (I kept getting not found errors before) but I still can't find jdbf-1.2.jar.
 
Upvote 1
I can't download your archive and can't find jdbf-1.2.jar to look at but your code is trying to create an array of com.hexiong.jdbf.JDBField instances and it seems that the values you give for the array contents cannot be cast to a com.hexiong.jdbf.JDBField type.
I think because I reupload the attachment just now. Try download again or I will attached the file again.

The jar is based on https://code.google.com/p/jdbf/

Related post:

EDIT: In case you don't want to download the library, I attach the jar here.
 

Attachments

Upvote 0
Still no jdbf-1.2.jar in the archive so I can't run it. Your link only gives the source code. However I guess you want to do this - which is not what your code in the project does.

Java:
JDBField[] fields = { new JDBField("ID", 'C', 8, 0),
                     new JDBField("Name", 'C', 32, 0),
                     new JDBField("TestN", 'N', 20, 0)
                     new JDBField("TestF", 'F', 20, 6) }
You need to do something like this to get a JDBField instance and then put that instance in an array of Objects. I haven't tried this as I don;t have the jdbf jar.
B4X:
Dim JDBFieldAs JavaObject
JDBField.InitializeNewInstance("com.hexiong.jdbf.", Array As Object("ID", "C".As(Char), 8, 0))
 
Upvote 0
Solution
Solved.
I posted an update to the code snippet.

 
Upvote 0
Back
Top