Android Question java.lang.NullPointerException

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I keep seeing this message in a debug session and cannot figure out what is causing it.

Is there a easy way to determine where this happening?

Sometimes the debugger points me to this line:

B4X:
	If FirstTime Then
	   mFirstTime = True
	   
	   mUsePinPosition = 0                       '<---------------- Points to this line
	   
       mGPS.Initialize("GPS")	

       mGPSCourseEntry = -1
	   mGPSCourseHole  = -1
	   
	   mGPSCourseList.Initialize
	   mGPSCourse.Initialize
	   
	   mGPSParser.Initialize
	End If

But that line is defined in my process_globals as
B4X:
	Private mUsePinPosition                      As Int

Any Help

BobVal
 

eps

Expert
Licensed User
Longtime User
Is it definitely that line in the code? As you've got an 'If FirstTime' check which precedes it and this could be what is causing you an issue, if you use the variable later on but haven't assigned a value.. as it isn't the 'FirstTime'..
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Yes, but there is always a FirstTime, I should have included the code above it.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
	
    Activity.LoadLayout("sPlayerScoring")
	Activity.Title = "Player Scoring"

	If FirstTime Then

Also this is a Int so even if I do not assign it a value, it should not give me a Null Pointer.

But in any case just to be sure I did a
B4X:
Private mUsePinPosition   as   Int  =  0

and am still have that Null Pointer. I believe it is somewhere else and just happens to be sometimes pointing there.

BobVal
 
Upvote 0

eps

Expert
Licensed User
Longtime User
If you don't assign it a value it will be null. If you want it to always have a value you will need to assign it a value when it gets defined. :)

You always have a Firsttime, but if it isn't the first time it will be false.. and won't execute that code.
 
Upvote 0

nikolaus

Member
Licensed User
Longtime User
thats legal, as is mine, but I think the OP's version is not and the assignement is simply ignored
 
Upvote 0

Reviewnow

Active Member
Licensed User
Longtime User
Move your assignment to
Sub Process_Globals()

Then it can be accessed anywhere
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Interesting questions about the assignments. If I cannot do my assignments that way then I have a lot of code to change because I do that all the time all over the place and to my knowledge have never had a problem.
Hopefull Erel will answer if
B4X:
Private mSomeVariable as Int = 0
is proper.
I do a lot of
B4X:
private mSomeVariable as Boolean = True
or
B4X:
private mSomeVarialbe as Boolean = False
and always have had them set right
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Well I figured out where this was happening:
B4X:
 DBCursor.GetString(DEFINE_DB_FieldTags_ImportantDates)
In the database I am using someone allowed this field to be Null and I assign it to a string variable that did not test for Null (sure wish allowing a Null field in a DB meant an Empty string "" for string types) but this line was inside a Try / Catch
and the catch part
B4X:
        Catch
                'Log(LastException.Message)
      End Try
was just eating up the Exception. Oh well that's what happens when you use OPC (Other peoples code). I guess I will have to look at all this code instead of just using it.

Thanks for the help guys.
 
Upvote 0
Top