Android Question NullPointerException

marcick

Well-Known Member
Licensed User
Longtime User
Hy guys,
it happened this error but I can't find the reason. Please have a look at this piece of code ( paste here the essential):

B4X:
Sub Globals
    
    Dim Icon As BitmapDrawable
   
End Sub

Sub GPS_LocationChanged (Location1 As Location)
   
    Dim geopoint1 As GeoPoint
    geopoint1.Initialize(Location1.Latitude,Location1.Longitude)
    Move_Marker("Marker_gps",geopoint1,"iconcar.png")

End Sub

Sub Move_Marker(MarkerId As String, geopoint1 As GeoPoint, icn As String)

   
    Icon.Initialize(LoadBitmap(File.DirAssets, icn))
   
etc
etc
etc
End Sub

So, GPS_LocationChanged call Move_Marker and the line Icon.Initialize has generated the exception.
Of course "iconcar.png" exist, the program runs normally all time long, but once has happened that exception. I'm interested to understand why, if there is an answer ...
Thanks
Marco
 

klaus

Expert
Licensed User
Longtime User
Remove Dim Icon As BitmapDrawable from Globals and try to use :
B4X:
Sub Move_Marker(MarkerId As String, geopoint1 As GeoPoint, icn As String)
    Dim Icon As BitmapDrawable

    Icon.Initialize(LoadBitmap(File.DirAssets, icn))
Best regards.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Thanks Klaus, but the program runs normally, why just once I noted that error ? Is it not safe to have global variables ? If so ....
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Hi Erel,
the error is this:

Caused by: java.lang.NullPointerException
at it.mysite.myapp.main._move_marker(main.java:2774)

and looking at the java source I see at line 2774:

//BA.debugLineNum = 849;BA.debugLine="Icon.Initialize(LoadBitmap(File.DirAssets, icn))";
mostCurrent.Icon.Initialize((android.graphics.Bitmap)(anywheresoftware.b4a.keywords.Common.LoadBitmap(anywheresoftware.b4a.keywords.Common.File.getDirAssets(),_icn).getObject()));
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Understand, thank you, I will do that.
But that is a view related variable ... this code runs fine since several months. I'm curious to understand why I noted that error just once. If it is not safe to use global variables, I will use process_global variables for all.
 
Upvote 0
Top