B4J Question How to create a File object ? (FileSystemView)

jmon

Well-Known Member
Licensed User
Longtime User
Hello,

I am trying to convert some functions on this page : http://docs.oracle.com/javase/7/docs/api/javax/swing/filechooser/FileSystemView.html

B4X:
Public Sub isDrive(Path As String) As Boolean
    Dim FileJO As JavaObject
    FileJO.InitializeStatic("java.io.File")
   
    Dim FileSystemView As JavaObject
    FileSystemView.InitializeStatic("javax.swing.filechooser.FileSystemView")
    FileJO = FileSystemView.RunMethod("createFileObject", Array As String(Path))
   
    Return FileSystemView.RunMethod("isDrive", Array As Object(FileJO))
End Sub

Sub AppStart (Args() As String)
    Log(isDrive("C:\"))

End Sub

But with this code, I get this error :
main._isdrive (java line: 70)
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:80)
at b4j.example.main._isdrive(main.java:70)
at b4j.example.main._appstart(main.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:26)

I don't get it. I tried so many variations of this code, but no luck so far.

Thanks for your help.
jmon
 

jmon

Well-Known Member
Licensed User
Longtime User
Thanks,

With this new code I am getting a different error :
B4X:
Public Sub isDrive(Path As String) As Boolean
    Dim FileSystemView As JavaObject
    FileSystemView.InitializeStatic("javax.swing.filechooser.FileSystemView")
    FileSystemView = FileSystemView.RunMethod("getFileSystemView", Null)
   
    Dim FileJO As JavaObject
    FileJO = FileSystemView.RunMethod("createFileObject", Array As Object(Path))
   
    Return FileSystemView.RunMethod("isDrive", Array As Object(FileJO))
End Sub

main._isdrive (java line: 70)
java.lang.IllegalAccessException: Class anywheresoftware.b4j.object.JavaObject can not access a member of class javax.swing.filechooser.WindowsFileSystemView with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
at java.lang.reflect.Method.invoke(Method.java:599)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:80)
at b4j.example.main._isdrive(main.java:70)
at b4j.example.main._appstart(main.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:26)

The error happens on the "createFileObject" method. Any idea?

Thanks
 
Upvote 0
Top