B4J Question Why does this code throw a "NullPointerException" error?

LWGShane

Well-Known Member
Licensed User
Longtime User
I'm on the home stretch of finishing my B4X Launcher app, and some code that should work is throwing me a NullPointerException.

Note that I'm calling the TextFields from another module (It's the way I separate logic from the GUI.)

Note 2: I have "MainForm" declared in my Initialize Sub.

B4X:
Sub Handle_CreateProjectButton_Action
    If Main.ProjectNameField.Text.Length = 0 Then
        fx.Msgbox(MainForm, "I'm not detecting a name for your project. Please supply a name!", "No Name")
    Else If Main.ProjectLocationField.Text.Length = 0 Then
        fx.Msgbox(MainForm, "I'm not detecting a location for your project. Plase select a folder for your project by clicking the ""Open"" button.", "No Location")
    Else If Main.CustomControlNameField.Text.Length = 0 Then
        fx.Msgbox(MainForm, "I'm not detecting a name for the initial custom control module. Please supply a name for it!", "No Name")
    Else
        CreateProject
    End If
End Sub

Any ideas?
 
Last edited:

LWGShane

Well-Known Member
Licensed User
Longtime User
Why are you declaring your mainform in Initialize Sub?
So I can use MainForm without having to type "Main.MainForm". Note that this is in the other module, not in the Main module.
 
Upvote 0

somed3v3loper

Well-Known Member
Licensed User
Longtime User
You are using a code like this
B4X:
Sub Class_Globals
    Private fx As JFX
       dim mainForm as Form
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    mainForm = Main.MainForm
End Sub
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
can you post the full error message? Usually it will tell you where the null pointer comes from. If you run in debug, it will also jump to the line that caused the error.
 
Upvote 0

LWGShane

Well-Known Member
Licensed User
Longtime User
can you post the full error message? Usually it will tell you where the null pointer comes from. If you run in debug, it will also jump to the line that caused the error.
It was coming from the first fx.MsgBox.
I changed "MainForm" to "Main.MainForm", and that resolved it.
 
Upvote 0
Top