Android Question Nil Checking

DawningTruth

Active Member
Licensed User
I got the following Java Error...

java.lang.NullPointerException: Attempt to invoke virtual method 'int uk.co.martinpearman.b4a.webkit.WebBackForwardList.GetCurrentIndex()' on a null object reference

I have received this type of Nil exception error a number of times for various random reasons on various methods. I think it best I setup guards to prevent random program crashes.

How do I setup a "Nil Guard" something like:

B4X:
Pseudo Code:

if myObject <> nil Then
   'Do stuff with myObject
End If
 

emexes

Well-Known Member
Licensed User
Try
Catch
End Try

works pretty good. Admittedly, if I get a null object error, then I've found I can always fix it in my code, and I use the Try Catch stuff as a safety net of last resort.

Bit like ABS in a car - yeah, it'll probably save you when your car leaves the road, but much better to try not leave the road in the first place.
 

DawningTruth

Active Member
Licensed User
Try
Catch
End Try

works pretty good. Admittedly, if I get a null object error, then I've found I can always fix it in my code, and I use the Try Catch stuff as a safety net of last resort.

Bit like ABS in a car - yeah, it'll probably save you when your car leaves the road, but much better to try not leave the road in the first place.
Thx Emexes, yes agreed. It a situation where in 99% of use cases it works, but in the 1% random case it crashes. I think it is because the limitations of the Android Platform force so many complex event interactions and this leads to these fringe cases.
 

DawningTruth

Active Member
Licensed User
It worked.
PS: Found that if you put a break point in the catch part of the statement, it is much easier to track down the exact fringe case causing the problem.
 

agraham

Expert
Licensed User
In my major mapping app on Android I have found that I also get occasional Null Object errors when trying to save state in Activity_Pause when the user closes the activity by pressing the Back button. It looks like sometimes Android destroys objects before invoking Pause. I have to trap it in a Try Catch block as there seems to be no other way of dealing with it but it is a most unsatisfactory situation and its existence reinforces everything I dislike about Google and Android.

Also be careful if calling Sleep() or Wait For in Activity_Create, Activity_Pause and Activity_Resume as Android then thinks you have finished and can do stuff before you are re-entered after the wait. For example FirstTime will no longer be True in Activity_Create on re-entry so get all you can done before calling Sleep or Wait For.
 

emexes

Well-Known Member
Licensed User
PS: Found that if you put a break point in the catch part of the statement, it is much easier to track down the exact fringe case causing the problem.
I'm 99% sure you're on to this, but just in case: you can put other statements in the Catch part of the Try ... Catch ... End Try block, that clean up after or recover from whatever event was caught. And it doesn't hurt to Log(LastException.Message), or check that another (different) problem hasn't entered the arena eg

B4X:
If LastException.Message.ToLowerCase.Contains("nullpointerexception") Then
    'pretty sure I know what happened
Else
    'today is not my lucky day
    Log(LastException.Message)
End If
And if the Catch code grows and makes it hard to see your normal code, you can wrap and hide it with #region ... #end region (or you could ship it out to a Sub, but then you lose easy access to all the local variables needed to do the clean/fix-up).
 
Last edited:

DawningTruth

Active Member
Licensed User
In my major mapping app on Android I have found that I also get occasional Null Object errors when trying to save state in Activity_Pause when the user closes the activity by pressing the Back button. It looks like sometimes Android destroys objects before invoking Pause. I have to trap it in a Try Catch block as there seems to be no other way of dealing with it but it is a most unsatisfactory situation and its existence reinforces everything I dislike about Google and Android.

Also be careful if calling Sleep() or Wait For in Activity_Create, Activity_Pause and Activity_Resume as Android then thinks you have finished and can do stuff before you are re-entered after the wait. For example FirstTime will no longer be True in Activity_Create on re-entry so get all you can done before calling Sleep or Wait For.
Yes, I'm also discovering that o_O. I appreciate the tips and will refer back to this post.

Personally, I've always disliked Java. It is just so unpredictable, verbose and slow. I would much rather code in C# or C++. Hence the reason I decided to invest in the B4x Platform. Erel, has managed to create a similar experience to C# for Mobile Development. Enjoying using B4x.
 

DawningTruth

Active Member
Licensed User
I'm 99% sure you're on to this, but just in case: you can put other statements in the Catch part of the Try ... Catch ... End Try block, that clean up after or recover from whatever event was caught. And it doesn't hurt to Log(LastException.Message), or check that another (different) problem hasn't entered the arena eg

B4X:
If LastException.Message.ToLowerCase.Contains("nullpointerexception") Then
    'pretty sure I know what happened
Else
    'today is not my lucky day
    Log(LastException.Message)
End If
And if the Catch code grows and makes it hard to see your normal code, you can wrap and hide it with #region ... #end region (or you could ship it out to a Sub, but then you lose easy access to all the local variables needed to do the clean/fix-up).
Thx Emexes, good tips.

I will definitely use them.
 
Top