Android Question Object should first be initialized (Cursor). with GetPathFromContentResult()

Didier9

Active Member
Licensed User
I can run Erel's example just fine, but when I integrate it in my app, I get a java.lang.RuntimeException: Object should first be initialized (Cursor). error

The code in this post is now fixed: http://www.basic4ppc.com/android/forum/threads/using-photos-from-the-phone.37444/#post-221367
I've also uploaded a simple example.
I have attached a minimized version of my app with just the offending code. It picks an image from the chooser and scales it to best fit the screen.
Here is the entire error message:

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
Error occurred on line: 101 (Main)
java.lang.RuntimeException: Object should first be initialized (Cursor).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4a.sql.SQL$CursorWrapper.setPosition(SQL.java:322)
    at b4a.example.main._getpathfromcontentresult(main.java:649)
    at b4a.example.main._cc_result(main.java:571)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:843)
    at anywheresoftware.b4a.BA$4.run(BA.java:547)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:432)
    at b4a.example.main$ResumeMessage.run(main.java:301)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
** Activity (main) Resume **
and here is the line of code that causes the error:

B4X:
    Cursor1.Position = 0
Note that my app will give you an error first because it cannot find a picture it's supposed to load but that I had to take out because the zip file was too big.
Once the app is running, click on "Pick new map" in the menu and select an image. The app crashes when the image is selected and the chooser closes.
 

Attachments

DonManfred

Expert
Licensed User
The content chooser itself is working fine.

B4X:
Sub cc_Result( Success As Boolean, Dir As String, FileName As String )
    Dim path As String
    Log($"cc_Result( ${Success}, ${Dir}, ${FileName})"$)
    If Success = True Then
        Dim bm As Bitmap
        'Log( "MapDir = " & Starter.MapDir & ", MapFile = " & Starter.MapFile )
        bm.Initialize( Dir, FileName )
        ImageView1.SetBackgroundImage( bm )

        'path = GetPathFromContentResult( MapFile )
        'MapFile = path.SubString( path.LastIndexOf( "/" )+1 )
        'MapDir = path.SubString2( 0, path.LastIndexOf( "/" ))
        'SaveSettings
        'ImageView1.SetBackgroundImage( Null )
        'LoadMap
    End If
End Sub ' cc_Result()
WhatsApp Image 2018-01-25 at 07.05.17.jpeg

The problem is that it is not possible to get the path and the real filename in every case.

The problem is in getPathFromContentResult( UriString As String ) As String

You should use the given values in the content resolver event sub....
 
Last edited:

Didier9

Active Member
Licensed User
The content chooser itself is working fine.

The problem is that it is not possible to get the path and the real filename in every case.

The problem is in getPathFromContentResult( UriString As String ) As String

You should use the given values in the content resolver event sub....
It's not clear to me why. I did the initial development on a Moto-X with Android 5.1 and it worked fine. Then I tried with a Lenovo Tab 3 tablet with Android 6 and it crashed. I messed with it and for a while it would sometimes work in debug but not in release, then it completely stopped working, so I went back to the Moto-X and it is now also broken on it. All the crashes have been at the same line yet Erel's example works fine on both machines all the time and aside from the image manipulation itself, the content chooser code is the same and I have stripped my app of all other functionality, just keeping the picture sizing bit, but it crashes well before I get there...

It is very frustrating. The error message is that Cursor is not initialized, but Erel does not initialize it in his example.

If not the content chooser, I need another way to navigate the filer storage looking for image files. So what do I do?
 

DonManfred

Expert
Licensed User

Didier9

Active Member
Licensed User
In both cases (Erel's demo code and my code), I am pointing to a given picture in the Picture folder. I would prefer to use the actual file name and path because I originally wrote my app that way and it only looked in the download folder, not using the Content Chooser. My customer wants to be able to look in the Picture and Screenshot folders as well and rather than come up with a convoluted solution of my own, I thought about using the Content Chooser, convert the file location to path and filename using Erel's GetPathFromContentResult() so that I don't have to rewrite the rest of my app which still relies on path and filename. I do not actually want to use pictures that are not files, so I am fine if the Content Chooser does not let me use those.

As I said, Erel's demo code works fine, my code which integrates his code does not.

I get it that the Content Chooser does not always return a path. I understand that and I am OK with it, the call will be in a Try block so if the user chooses a picture that is not a file, he will know and the app will tell him gracefully.

The point is that Erel's GetPathFromContentResult() demo works (solidly) with the files I am trying it with but my code which incorporates it does not work with the SAME files (at least it works erratically).

Note: I renamed the thread to more accurately reflect that the problem is not with ContentChooser but with GetPathFromContentResult()
 
Last edited:

DonManfred

Expert
Licensed User
If you want to use the path and filename then you can check the content of the picture-folder directly, do a File.Listfiles on this folder and use the result.
Search the forum on how to get the right path to the Pictures folder.
Or use the Android-Api to get it. Check my Storage library for ex.
 
Last edited:

Didier9

Active Member
Licensed User
If you want to use the path and filename then you can check the content of the picture-folder directly, do a File.Listfiles on this folder and use the result.
Which is what I was doing initially and that works but
My customer wants to be able to look in the Picture and Screenshot folders as well
so I wanted to avoid having to
Search the forum on how to get the right path to the Pictures folder
and the ScreenShot folder therefore
rather than come up with a convoluted solution of my own, I thought about using the Content Chooser, convert the file location to path and filename using Erel's GetPathFromContentResult()
but as I have experienced
Erel's GetPathFromContentResult() demo works (solidly) with the files I am trying it with but my code which incorporates it does not work with the SAME files (at least it works erratically).
Indeed
The problem is in getPathFromContentResult( UriString As String ) As String
so there I am getting nowhere very fast.

I would like to think that even considering that the Content Chooser does not always return something that can be converted into a path and filename, that is just not the point of my post. The issue is not with the Content Chooser per se but with GetPathFromContentResult(). Erel's demo does work in all cases I have tried it.

What puzzles me is that I made a deliberate attempt to incorporate his demo into a slightly more elaborate "demo" app of my own (in the zip file of the first message in this thread), yet my "demo" app crashes on the same line of code each time
Cursor1.Position = 0
and this error message makes no sense because Erel does not initialize the Cursor in his code either.

My code does not work on the same files that work with Erel's demo code. I must have added or subtracted something and I do not know what.

We have been talking about all the other things I could or should or might or may be doing instead of looking for an answer to this question:

Why does the Cursor1.Position = 0 line crash with a "Cursor not initialized" error?
 
Last edited:
Top