B4J Question [SOLVED] Maddening list bug

JackKirk

Well-Known Member
Licensed User
Longtime User
I've tried everything I can think of to fix this - last resort: post a question...

Here is the code:
B4X:
Log(xxxx.Size)
Log(xxxx.IsInitialized)
        'For each ticket web request file...
        For wrk_ptr = 0 To xxxx.Size - 1                        '<<<<< this is line 631
wrk_ptr is an Int

Here is the log:
Waiting for debugger to connect...
Program started.
0
true
Error occurred on line: 631 (Main)
java.lang.RuntimeException: Object should first be initialized (List).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
at anywheresoftware.b4a.objects.collections.List.getSize(List.java:134)
at b4j.example.main$ResumableSub_Event_obj_check_web_backlog_timer_Tick.resume(main.java:2217)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:153)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:102)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4a.keywords.Common$3.run(Common.java:1103)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
I have tried everything including cleaning the project, shutting down the PC etc but still it persists.

I know I'm doing something stupid but what...
 
Solution
I found it!!!

The code that was producing the error was a modification of code I have had running for 3 years.

So I went back to the archived earlier copy and went slowly thru the modifications I had made.

In the subroutine that was bombing I had made a change that potentially resulted in another totally unrelated list being referenced before it was initialized.

For some totally bizarre reason, this was causing the reference to the other list (that was initialized) to bomb - before the bad reference was even executed.

So moral is: if you get a list bombing on you as "uninitialised" and you can't work out why - check all other lists in the same routine.

I'm guessing this may apply to maps also?

Does this qualify as a bug? - see...

DonManfred

Expert
Licensed User
Longtime User
and for good measure here is a screenshot:
The error line is the line in the JAVA file. NOT the B4A-Source-file.

What is line 639 in the java-file? in the java-file you see a comment above pointing to the real b4a line. This is the line you should look at.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
The error line is the line in the JAVA file. NOT the B4A-Source-file.
DonManfred - are you sure? - I moved the cursor well away from the poisoned area and reran it - the cursor repositioned to line 639 in the b4j code when it bombed.

I'm running B4J 9.1 (latest and greatest).
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
DonManfred - are you sure?
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim i As Int = "aa" ' B4J Line 21
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub
b4xpagesmanager._createpageifneeded (java line: 301)
java.lang.RuntimeException: java.lang.NumberFormatException: For input string: "aa"
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:513)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:459)
at b4j.example.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:301)
at b4j.example.b4xpagesmanager._showpage(b4xpagesmanager.java:716)
at b4j.example.b4xpagesmanager._addpage(b4xpagesmanager.java:113)
at b4j.example.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:120)
at b4j.example.b4xpagesmanager._initialize(b4xpagesmanager.java:485)
at b4j.example.main._appstart(main.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at b4j.example.main.start(main.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NumberFormatException: For input string: "aa"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at b4j.example.b4xmainpage._b4xpage_created(b4xmainpage.java:39)
at b4j.example.b4xmainpage.callSub(b4xmainpage.java:68)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:488)
... 23 more
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8

at b4j.example.main.start(main.java:37) <------
This is the java-part

//BA.debugLineNum = 21;BA.debugLine="Dim i As Int = \"aa\" ' B4J Line 21";
_i = (int)(Double.parseDouble("aa"));
^ line 37
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
I've broken my own rule and looked at it. The Java is a bit hard to read as it is complicated by both the Resumeable Sub code and the debug code additions but I can't see any references to a List in the code so I am left rather baffled without being able to run the code I'm afraid.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
java.lang.RuntimeException: Object should first be initialized (List).

Unfortunately very often the name of the object is not indicated but only the type.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
It's not Java line 639 it's a B4A line number. the Java line number is 49 in AbsObjectWrapper where it attempts to get the size of a List using getSize() on what looks like a null object reference.

The nearest line I can see to this is "633 garbage = xxxx.Size" which ought to be the source of the error so I'm baffled why the error is not shown on this line. Is it possible that the structure of your code allows this line to be executed before you have initialised xxxx?
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
End Sub

Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
Dim i As Int = "aa" ' B4J Line 21
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
xui.MsgboxAsync("Hello world!", "B4X")
End Sub
This is the java-part

//BA.debugLineNum = 21;BA.debugLine="Dim i As Int = \"aa\" ' B4J Line 21";
_i = (int)(Double.parseDouble("aa"));
^ line 37
But agraham says:
It's not Java line 639 it's a B4A line number. the Java line number is 49 in AbsObjectWrapper where it attempts to get the size of a List using getSize() on what looks like a null object reference.
Now I am confused...
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Is it possible that the structure of your code allows this line to be executed before you have initialised xxxx?
I've looked to no avail - also this is in contravention of the log entries saying it is initialized.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
Pressing F7 on xxxx might (should) help.
LucasMs, see partial screenshot:
f7.png

This sort of implies "Public xxxx As List" is done after all the other stuff but if you click on it you get transported to the statement in Process_Globals where it ought to be.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Have you tried
B4X:
Dim garbage As Int
Instead of
B4X:
Private garbage As Int
Note: grasping at straws
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I found it!!!

The code that was producing the error was a modification of code I have had running for 3 years.

So I went back to the archived earlier copy and went slowly thru the modifications I had made.

In the subroutine that was bombing I had made a change that potentially resulted in another totally unrelated list being referenced before it was initialized.

For some totally bizarre reason, this was causing the reference to the other list (that was initialized) to bomb - before the bad reference was even executed.

So moral is: if you get a list bombing on you as "uninitialised" and you can't work out why - check all other lists in the same routine.

I'm guessing this may apply to maps also?

Does this qualify as a bug? - see https://www.b4x.com/android/forum/threads/solved-maddening-bug-with-list.134405/post-850548 for simple example.
 
Last edited:
Upvote 0
Solution

JackKirk

Well-Known Member
Licensed User
Longtime User
I continue to be amazed at the level of support offered by the B4X community - I had 2 esteemed experts on this before I could blink - thanks to all.
 
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I have managed to create a simple example which illustrates the problem:
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Public good_list As List
    Public good_value As String
    Public bad_list As List
    Public bad_value As String
    Public obj_timer As Timer
End Sub

Sub AppStart (Args() As String)
    good_list.Initialize
    good_value = "good"
    good_list.Add(good_value)
 
    obj_timer.Initialize("event_obj_timer", 1)
    obj_timer.Enabled = True
 
    StartMessageLoop

End Sub

Sub event_obj_timer_Tick
    obj_timer.Enabled = False
 
    Log(good_list.IsInitialized)
 
    Sleep(1)
 
    If good_list.Get(0) = "whatever" Then                 '<<<<<<This is line 34 which is reported as bombing
  
    End If
  
    If bad_list.Size > 0 Then                            '<<<<<<This is the real problem
        Log("???")
    End If

End Sub
When I run this I get:
Waiting for debugger to connect...
Program started.
true
Error occurred on line: 34 (Main)
java.lang.RuntimeException: Object should first be initialized (List).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
at anywheresoftware.b4a.objects.collections.List.getSize(List.java:134)
at b4j.example.main$ResumableSub_event_obj_timer_Tick.resume(main.java:146)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:47)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:42)
at anywheresoftware.b4a.keywords.Common$2$1.run(Common.java:1035)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at b4j.example.main.main(main.java:29)
It appears that the subroutine must be resumable - if you take out the sleep statement it works as you would expect - bombs on line 38

Can I claim this as a bug? - most often when I have made such a claim in the past Erel shoots me down :)
 

Attachments

  • listbug.zip
    893 bytes · Views: 100
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It still appears to report the error on the wrong line. If you remove the (unnecessary in the example) Sleep(1) line, then the error is reported on the correct line. It seems that it may be somehow related to the created resumable sub.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
As I pointed out in post #2,7 and stevel05 reiterates above, the initial confusion about this problem is that the incorrect B4J line is reported in the logged exception which obscures the fact that the actual problem was simply getting the size of an unitialised List. As stevel05 also points out this seems related to the Sleep() statement.
 
Last edited:
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
the incorrect B4J line is reported in the logged exception which obscures the fact that the actual problem was simply getting the size of an unitialised List
Exactly - and becomes a real problem when you have a larger subroutine so the distance between the reported error line and the real error line is significant.
 
Upvote 0
Top