B4J Question java.lang.RuntimeException: Object should first be initialized (HttpSession).

hatzisn

Well-Known Member
Licensed User
Longtime User
I tried today for the first time to use a session object in websocket and I keep getting this error:

java.lang.RuntimeException: Object should first be initialized (HttpSession).

I searched the forum and found the solutions in:


and in


I implemented them but I keep getting this error.

I even tried a handler which is called at runtime when the websocket is connected (hidloc is a hidden control which gets the window.location; ) with this code (the handler Handle sub is the same as the filter's sub):

B4X:
    Dim hj As HttpJob
    hj.Initialize("", Me)
    Dim sSite As String = hidloc.GetVal.Value
    Log(sSite)
    hj.Download(IIf(sSite.EndsWith("/"), sSite.SubString2(0, sSite.Length - 1), sSite) & "/sess")
    wait for (hj) JobDone(hj As HttpJob)
    hj.Release

I also have placed a breakpoint in the filter request handle but it does not stop there and the same is valid for the custom handler request.

I am through with ideas on how to deal with it. Can somebody advise on anything that maybe I have missed?
 

hatzisn

Well-Known Member
Licensed User
Longtime User
I don't see how this code has anything to do with the "session filter".

Try to reproduce it in a small project, based on: https://www.b4x.com/android/forum/threads/safari-session-variables-in-b4j-v4.61652/#post-389898

Hi, I used already the code mentioned in the link you posted. I added the filter and your code (and also the srvr.Addfilter.... in main) . I was still getting the "session must be initialized". Your code is executed obviously when the web page tries to get the "b4j_ws.js". Is this right? I added a breakpoint in the filter hande sub and it does not ever get there to stop the execution in debug mode. Then I decided to do this manually I added a handler which is called manually with the httpjob by adding this command in main:

B4X:
srvr.AddHandler("/sess", "ManualSessionCreator", False)

and I used the code I have written in the first post to call this handler. This code mentioned is called in websocket_connected.

The handler's code is the following:

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    req.GetSession
End Sub

I did the same adding a breakpoint in req.GetSession and the same thing happened (no stopping at all).
I cannot figure out what is happening... I will try to minify the code and see what is going wrong and let you know.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I fixed it. I changed the position of the Port setting. I moved the srvr.Port = xxxxx after the srvr.AddFilter("/b4j_ws.js", "SessionCreator") and just before srvr.Start as I have seen this in WebAppHelloWorld example. It worked. Why does this position matter?
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Then I suppose B4J feared your reactions and decided to behave itself.
 
Upvote 0
Top