Android Question Calling routine from httpjob fails but not from elsewhere

jimseng

Active Member
Licensed User
Longtime User
When I call a routine from an httpd job I get java.lang.RuntimeException: Object should first be initialized (View).
when I call it from, say a button, it works.

so:
createalbum("a string") from
B4X:
 Sub JobDone(Job As HttpJob)
fails but from a button click event it works. What is it about http job to cause this?


The subroutine:
Sub createalbum (str As String)

        Dim p As B4XView =clv1.GetPanel(artindex).GetView(1)  'expanded panel

    p.LoadLayout("lytalb")   

    lblAlb.Text = str   'added a label to the expanded panel

End Sub
 

drgottjr

Expert
Licensed User
Longtime User
i'm pretty sure you know httpjob has nothing to do with views. never did.

you have a view which, at the time you reference it, is unitialized. initialize it.

that it may be or have been initialized at some other point in the app is not necessarily relevant.
it's also unclear that the view in question is the same one.

see the attached. without any reference to httpjob, i can cause an error similar to the one you received in createAlbum().
i have no idea what your app does, but i think what i did reflects what you did somewhat. in the example, i declare a clv, add a panel
and a label to the panel. but i fail to initialize it. when createAlbum() is called and looks for it, an unitialized view error is thrown.
i think you are experiencing a similar issue. if you are calling createAlbum() from within an okhttputils2 download and there is an
unitialized view error, the error has nothing to do with httpjob.
 

Attachments

  • cap.png
    38.5 KB · Views: 100
Upvote 0

jimseng

Active Member
Licensed User
Longtime User
I do something similar in the same http job which works.
B4X:
    Dim p As B4XView = CreateArtists(Colors.Gray,  m.get("Artist"),99dip)
                    clv1.Add(p, expandable.CreateValue(p, m.get("Artist")))
I don't often use B4A and so my knowledge is very scant. And I don't understand why it works from a button click or some other event. Perhaps you could expand a little?
I have tried initialising it as you suggest but initialising it errors: java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String
The process is to populate clv1 with some items, then in another query, fill one of those items with 1 or more items. It is specifically
Dim p As B4XView =clv1.GetPanel(artindex).GetView(1):
which throws the original error I posted, even though the view does exists (and therefore, I presume, is initialised at the time?) No doubt I have mis-understood.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
as i mentioned, the simple act of declaring a view (in my example, a label) does not initialize it. only simple data types (eg, int) are initialized on creation. (views created in the designer are initialized, but dynamically created views need to be initialized in code.)

you need to find out which view the error refers to. and where. suddenly, you refer to another sub in your httpjob block. i really have no idea in which sub the error is occurring.

in the snippet you show above, p, m or m.get() could refer to unintialized variables. you're assuming they're valid, i'm assuming they may not be. the same holds true for createAlbum() where the assumption is that the code refers to initialized elements. apparently not, since you're getting an uninitialized view error. if that, in fact, is where the error is.

wrapping code in try/catch blocks can be useful except in cases where you wrap a number of elements in the same block (if there's an error, you won't know which was the offending element, but you would know for sure where the error is occurring). you can test individual elements before using them "blindly". eg before using m in code you can test it for initialization ("if m.isinitialized = false then ..."). once you deal with the error, you can usually remove the test, but in certain cases, it may have to remain. (eg, like with arrays, where it's easy to go beyond its declared size).

i don't know what the button click does, and even if i did, it doesn't necessarily help regarding the error that occurs elsewhere. we don't know which view is the unitialized one. you need to find it and initialize it.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…