Android Question java.lang.RuntimeException: Object should first be initialized (B4XView).

Maxjonz

Member
Hi
As a newbie to BxA I entered the example program as instructed and compiled it. However I keep getting the error message "java.lang.RuntimeException: Object should first be initialized (B4XView). " when I complie it . It appears that there is a problem on Line 39 (see code below) , but I think that I have entered it all correctly.
I have put the line numbers in for clarity
Private Sub NewProblem
36 Number1 = Rnd(1,10)
37 Number2 = Rnd(1,10)
38 lblNumber1.Text = Number1
39 lblNumber2.Text = Number2
40 lblComments.text = "Enter the result" & CRLF & "and click on OK"
41 edtResult.Text = ""
42
End Sub

The first part of the red error messages reads as follows.

Logger connected to: motorola moto g15
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Error occurred on line: 39 (B4XMainPage)
java.lang.RuntimeException: Object should first be initialized (B4XView).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
at anywheresoftware.b4a.objects.B4XViewWrapper.asLabelWrapper(B4XViewWrapper.java:213)
at anywheresoftware.b4a.objects.B4XViewWrapper.setText(B4XViewWrapper.java:229)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)

Can anyone please help me with a possible solution to this error ?
 

klaus

Expert
Licensed User
Longtime User
Did you also declare
Private lblNumber2 As B4XView
in the Class_Globals routine ?

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private btnAction As B4XView
    Private edtResult As B4XView
    Private lblComments As B4XView
    Private lblMathSign As B4XView
    Private lblNumber1 As B4XView
    Private lblNumber2 As B4XView
    
    Private Number1, Number2 As Int
End Sub
 
Upvote 0

Maxjonz

Member
This is what the example program generated from the designer stage - see the script from the guide

1775042546988.png


Then the guide says to add the Number1 and Number2 variables to the same routine

1775042630190.png


Are you sayng that this is wrong ?
 
Upvote 0

Maxjonz

Member
Hello !
I have partly moved on from my problem, but not very far ! The program is coded as below - some lines have been given numbers for clarity
Th initialise problem is not listed as an error since I put in the extra coding to assign values to the variables ( I hope this is correct). I have not put the Design here but I have (hopefully) attached a zipped version of the program to this request.

The progam now completes a compilation, and my Android device , whilst apparently allowing it to run, closes immediately and the following message apppear on the log file

Once again thanks to anyone who can help - If I can't get this basic program to work then I cannot continue !

The top LOG files read (after compilation)

Main - 25: Layout file 'program01.bal' is not used. Are you missing a call to Activity.LoadLayout? (warning #16)

The main LOG file read as below, saying that there was a problem with line 28 lblNumber1.Text = Number1

Logger connected to: motorola moto g15
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Error occurred on line: 28 (B4XMainPage)
java.lang.RuntimeException: Type does not match (class java.lang.String)
at anywheresoftware.b4a.objects.B4XViewWrapper.typeDoesNotMatch(B4XViewWrapper.java:405)
at anywheresoftware.b4a.objects.B4XViewWrapper.asLabelWrapper(B4XViewWrapper.java:215)
at anywheresoftware.b4a.objects.B4XViewWrapper.setText(B4XViewWrapper.java:229)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.Program01.b4xmainpage._newproblem(b4xmainpage.java:72)
at b4a.Program01.b4xmainpage._b4xpage_created(b4xmainpage.java:63)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)
at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1140)
at b4a.Program01.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1068)
at b4a.Program01.b4xpagesmanager._showpage(b4xpagesmanager.java:425)
at b4a.Program01.b4xpagesmanager._addpage(b4xpagesmanager.java:245)
at b4a.Program01.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:259)
at b4a.Program01.b4xpagesmanager._initialize(b4xpagesmanager.java:165)
at b4a.Program01.main._activity_create(main.java:415)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at b4a.Program01.main.afterFirstLayout(main.java:105)
at b4a.Program01.main.access$000(main.java:17)
at b4a.Program01.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:959)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loopOnce(Looper.java:234)
at android.os.Looper.loop(Looper.java:319)
at android.app.ActivityThread.main(ActivityThread.java:8754)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962)

There was more if needed !

The Program

#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private btnAction As B4XView = "Enter the result" & CRLF & "and click on OK"
Private edtResult As B4XView = " "
Private lblComments As B4XView = " "
Private lblMathSign As B4XView = "+"
Private lblNumber1 As B4XView = "0"
Private lblNumber2 As B4XView = "0"
Public Number1,Number2 As Int
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
NewProblem
End Sub
Private Sub NewProblem
26 Number1 = Rnd(1,10)
27 Number2 = Rnd(1,10)
28 lblNumber1.Text = Number1
29 lblNumber2.Text = Number2
30 lblComments.Text = "Enter the result" & CRLF & "and click on OK"
edtResult.Text = ""
lblMathSign.Text = "+"
End Sub

Private Sub Button1_Click
If btnAction.Text = "O K" Then
If edtResult.Text = "" Then
MsgboxAsync("No result entered", "E R R O R")
Else
CheckResult
End If
Else
NewProblem
btnAction.Text = "O K"
End If
End Sub
Private Sub CheckResult
If edtResult.Text = Number1 + Number2 Then
lblComments.Text = "G O O D result" & CRLF & "Click on NEW"
btnAction.Text = "NEW"
Else
lblComments.Text = "Wrong result " & CRLF & "Try again " & CRLF & "Click OK"
End If
End Sub
 

Attachments

  • Program01.zip
    11.3 KB · Views: 2
Upvote 0

klaus

Expert
Licensed User
Longtime User
1. The problem is that you load the MainForm layout file.
B4X:
    Root.LoadLayout("MainForm")
But your layout file is ProGram01, so need to load this one.
B4X:
Root.LoadLayout("ProGram01")

2. Then, you have this line, which is wrong:
B4X:
Private Sub Button1_Click
It should be:
B4X:
Private Sub btnAction_Click

3. In the Designer you should set the TextSize parameters for the three upper Labels and the EditText view to 36 instead of 14.

4. The lblComments does not show anything because its background color is white and the text color is also white.
Set to black.
You might also increase its TextSize.
 
Upvote 0
Top