Android Question MAP "Null" Value Assignment Generates Java NullPointerException

DaveB4A

Member
Licensed User
Longtime User
I have a MAP defined which contains multiple STRING entries. Most of them contain text - but some are "blank" - meaning they are "NULL".

When I attempt to load those entries into an EditText view, any MAP entry that is "NULL" generates a Java NullPointerException and the application terminates. However, if the MAP entry contains actual text, the assignment works fine.

For example, the following code will fail if "firstname" is NULL.

B4X:
EditText1.Text = Contacts.Chosen_Contact_Field_Map.Get("firstname")

Error occurred on line: 70 (modifycontactinfo)
modifycontactinfoafterFirstLayout (java line: 98)
java.lang.NullPointerException
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:38)
at anywheresoftware.b4a.objects.EditTextWrapper.setText(EditTextWrapper.java:213)
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:485)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:232)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at b4a.example.modifycontactinfo.afterFirstLayout(modifycontactinfo.java:98)
at b4a.example.modifycontactinfo.access$100(modifycontactinfo.java:16)
at b4a.example.modifycontactinfo$WaitForLayout.run(modifycontactinfo.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:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

In order to circumvent the issue, I found that if I assign each MAP field to a STRING variable and then assign the EditText view to the STRING variable, all entries work fine - even the "NULL" entries.

For example, the following code works as desired, even when "firstname" is NULL.

B4X:
Dim Chosen_Contact_FirstName As String
Chosen_Contact_FirstName = Contacts.Chosen_Contact_Field_Map.Get("firstname")
EditText1.Text = Chosen_Contact_FirstName

Could someone please point me in the direction of where in the documentation this behavior is explained. I'm sure it's due to some kind of "conversion", perhaps... But I really don't understand why my "workaround" actually works. :)
 
Top