B4J Question Wait For and Ctrl+S causing 'java.lang.NullPointerException'

cyiwin

Active Member
Licensed User
I'm trying to use 'Wait For'. It works but whenever I do a code change in debug mode and press ctrl+s, my program crashes.

here is an example of what I'm doing.
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    Wait For (Job_Current_Time) Complete (Success As Boolean)
    Log("Job_Current_Time = " & Success)
End Sub

Sub Job_Current_Time As ResumableSub
    Log("Http.Get_Current_Time")
    Dim job As HttpJob : job.Initialize("", Me)
    job.Download("http://worldtimeapi.org/api/timezone/America/Los_Angeles")
    
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Dim jobstring As String = job.GetString
        Dim JSON As JSONParser : JSON.Initialize(jobstring)
        Dim m As Map = JSON.NextObject
        Log(m)
    Else
        LogError(job.JobName & "     " & job.ErrorMessage)
    End If
    job.Release
    
    Return job.Success
End Sub
On my computer, placing a break point (red dot) anywhere in Subs AppStart or Job_Current_Time and using ctrl+s causes the error "java.lang.NullPointerException". Where is my mistake?

Thanks
 

amykonio

Member
Licensed User
What version of B4J are you using? In 7.80 (current beta) it works fine.
I do a change in debug mode, and I save it without any error.
 

DonManfred

Expert
Licensed User
job.Release

Return job.Success
you are accessing the job after you released it.

B4X:
Sub Job_Current_Time As ResumableSub
    dim success as boolean
    Log("Http.Get_Current_Time")
    Dim job As HttpJob : job.Initialize("", Me)
    job.Download("http://worldtimeapi.org/api/timezone/America/Los_Angeles")
    
    Wait For (job) JobDone(job As HttpJob)
    success = job.success
    If job.Success Then
        Dim jobstring As String = job.GetString
        Dim JSON As JSONParser : JSON.Initialize(jobstring)
        Dim m As Map = JSON.NextObject
        Log(m)
    Else
        LogError(job.JobName & "     " & job.ErrorMessage)
    End If
    job.Release
    
    Return success
End Sub
 

cyiwin

Active Member
Licensed User
Thanks for the replies.

you are accessing the job after you released it.
I updated the code so it doesn't return Job.Success. I still get the same error.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    Wait For (Job_Current_Time) Complete (Success As Boolean)
    Log("Job_Current_Time = " & Success)
End Sub

Sub Job_Current_Time As ResumableSub
    Log("Http.Get_Current_Time")
    Dim job As HttpJob : job.Initialize("", Me)
    job.Download("http://worldtimeapi.org/api/timezone/America/Los_Angeles")
    
    Wait For (job) JobDone(job As HttpJob)
    Dim success As Boolean
    If job.Success Then
        success = True
        Dim jobstring As String = job.GetString
        Dim JSON As JSONParser : JSON.Initialize(jobstring)
        Dim m As Map = JSON.NextObject
        Log(m)
    Else
        LogError(job.JobName & "     " & job.ErrorMessage)
    End If
    job.Release
    
    Return success
End Sub
What version of B4J are you using?
I was using 7.51, I installed 7.80 beta, but I still get the same error when I press Ctrl+S.


Wait_For.jpg
 

amykonio

Member
Licensed User
This is expected. Compilation of debug and release makes different assumptions therefore yielding different ways to read the code by the compiler. You should always make some tests on release.
Yes... Before testing in debug mode i tried the same code in release. It run without any problem. Also the issue was mentioned to happen while debugging.
Maybe we use different version of java. I use JDK 1.8.0_201.
 
Last edited:

amykonio

Member
Licensed User
From my testing the issue happens when you place a breakpoint after the wait for statement in line 13 in appstart or after the wait for statement in Job_Current_Time sub.
And yes that way I also receive the java.lang.NullPointerException.
Maybe because the code at that point works in a separate thread? So the executed code will be different than the on in the IDE?
 

cyiwin

Active Member
Licensed User
And what version of jdk? We must do something different.
I'm also using jdk1.80_201.

I also tried it on a different computer using jdk-9. I get the exact same error.

Also what happens if you place the breakpoint in line 12 or 13 instead of line 14?
breakpoints at lines 12 and 13 work fine, no crashes.

To recreate the problem:
  • place a breakpoint at line 14
  • run in debug mode
  • when the program halts on line 14, press Ctrl+S
For both my computers this causes a crash and error "java.lang.NullPointerException"
 

cyiwin

Active Member
Licensed User
From my testing the issue happens when you place a breakpoint after the wait for statement in line 13 in appstart or after the wait for statement in Job_Current_Time sub.
And yes that way I also receive the java.lang.NullPointerException.
Maybe because the code at that point works in a separate thread? So the executed code will be different than the on in the IDE?
I see. I rely on hot code swapping a lot, is there a way to kill the 'Wait For' process when it's done?
 

amykonio

Member
Licensed User
I see. I rely on hot code swapping a lot, is there a way to kill the 'Wait For' process when it's done?
I'm not sure. The following:
Maybe because the code at that point works in a separate thread?
was a question. I don't know how this code is transformed to java, and how the ctrl-s may affect it after changes.
Personally I rarely modify the code while debugging.
I think Erel is the most appropriate person to answer your question.
 

cyiwin

Active Member
Licensed User
was a question. I don't know how this code is transformed to java, and how the ctrl-s may affect it after changes.
Personally I rarely modify the code while debugging.
I think Erel is the most appropriate person to answer your question.
Thanks for your time and help!
 
Top