Android Question Getting NullPointerException when referencing label tag

Steve Miller

Active Member
Licensed User
Longtime User
I have layouts with labels on them. I have one label that I use as a caption and the rest are used as information labels. To differentiate them while processing code, I set the Tag value of the caption labels to "CAPTION".

Any idea why I would get a NullPointerException when It goes through this section of code? It happens on the “If lab.Tag=”CAPTION”” statement. I get this quite often when testing.

Here is the code:

B4X:
Sub SetCustomColors
    Dim i As Int
    Dim current As Int = TabHost1.CurrentTab
      For i = 0 To TabHost1.TabCount - 1
        TabHost1.CurrentTab = i
      Next
      TabHost1.CurrentTab = current
      For Each v As View In Activity.GetAllViewsRecursive
        SetViewColors(v)
      Next

End Sub
Sub SetViewColors(v As View)

    Select Case True
        Case v Is CheckBox
            Dim chk As CheckBox
            chk = v
            chk.Color=GetBG
            chk.TextColor=GetFG
        Case v Is EditText
            Dim txt As EditText
            txt = v
            txt.Color=GetBG
            txt.TextColor=GetFG
        Case v Is RadioButton
            Dim rad As RadioButton
            rad = v
            rad.Color=GetBG
            rad.TextColor=GetFG
        Case v Is Button
            Dim but As Button
            but = v
            but.Color=GetBG
            but.TextColor=GetFG
        Case v Is Spinner
            Dim spi As Spinner
            spi = v
            spi.Color=GetBG
            spi.TextColor=GetFG
        Case v Is Panel
            Dim pnl As Panel
            pnl = v
            pnl.Color=GetBG
        Case v Is Label         
            Dim lab As Label
            lab = v
            Log("Text=" & lab.Text)
            If lab.Tag="CAPTION" Then
                lab.Color=GetBG
                lab.TextColor=GetFG
            Else
                lab.Color=GetBG
                lab.TextColor=GetFG
            End If
    End Select
End Sub

This is the exception I get when the " If lab.Tag="CAPTION" Then" code is run:

B4X:
Error occurred on line: 130 (main)
java.lang.NullPointerException
                at b4a.CustomerTablet.main._setviewcolors(main.java:508)
                at b4a.CustomerTablet.main._setcustomcolors(main.java:356)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
                at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
                at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
                at b4a.CustomerTablet.main.afterFirstLayout(main.java:98)
                at b4a.CustomerTablet.main.access$100(main.java:16)
                at b4a.CustomerTablet.main$WaitForLayout.run(main.java:76)
                at android.os.Handler.handleCallback(Handler.java:615)
                at android.os.Handler.dispatchMessage(Handler.java:92)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4921)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                at dalvik.system.NativeStart.main(Native Method)
 

warwound

Expert
Licensed User
Longtime User
A View added in a layout file where you have not set a Tag value will have an empty string value "".
Whereas a View created in code with no Tag value set will have a Null value.

That's my findings...

Martin.
 
Upvote 0

Steve Miller

Active Member
Licensed User
Longtime User
All have been pre-defined with the layout designer. Thanks, Martin.
It's all working now (thanks for the help from everyone!) :)
 
Upvote 0
Top