Trying to start service at condition

RilleR

Member
Licensed User
Longtime User
Im trying to get my service to start through the condition of a specific sender of an sms, but i get "java.lang.NullPointerException"

The servicestart works fine without the " If Sms1.Address = "123456" Then"
What am i missing here ?

/Richard

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
    Type Message (Address As String, Body As String)
 Dim Sms1 As Sms
   
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then

        Dim messages() As Message
        messages = ParseSmsIntent(StartingIntent)
        For i = 0 To messages.Length - 1
   
            Log(messages(i))
       Next
      End If
      
   If Sms1.Address = "123456" Then 
   
   StartActivity(Main)
   If IsPaused(Main) Then
    StartActivity(Main)
   
End If
Else
 CallSub(Main, "RefreshData")
   End If
   
End Sub
 

mc73

Well-Known Member
Licensed User
Longtime User
I know nothing about the smsLib but from what I see, I suspect that you should replace
B4X:
sms1.address
with
B4X:
messages(i).address
which you should put inside the for loop. There, you could use a boolean flag, turned to true when you have a specific sender, exit the loop and run main activity when of course this flag is true.
 
Upvote 0

PaulR

Active Member
Licensed User
Longtime User
Often Null Pointer Exception problems are caused by not calling .Initialize.

ie Dimming is not enough, you must Dim then call .Initialize to create the object before using it.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If the object has an Initialize method then you need to call it before you call any other method. There are two exceptions:
1. Views loaded from Activity.LoadLayout or Panel.LoadLayout. These methods initialize the views for you.
2. If you assign an already initialized object then you do not need to call Initialize again.

Note that you should have started a new thread for this question.
 
Upvote 0

RilleR

Member
Licensed User
Longtime User
Thankyou Erel,

I guess that is what PaulR said if i understand correctly.
So i just have to figure out how to do it in the correct way.

I did create a new tread for this question, :sign0013: if i placed it wrong.

I will get this eventually, everyone is beginners in the beginnig and sometimes a star can be born out of a rookie.


If the object has an Initialize method then you need to call it before you call any other method. There are two exceptions:
1. Views loaded from Activity.LoadLayout or Panel.LoadLayout. These methods initialize the views for you.
2. If you assign an already initialized object then you do not need to call Initialize again.

Note that you should have started a new thread for this question.
 
Upvote 0
Top