HTTPUtils question regarding potential NPE errors

Kevin

Well-Known Member
Licensed User
Longtime User
I still see occasional errors in my dev console for null pointers and after looking around at some stuff I began to wonder about something.

In the tutorial it is recommended to check if a job finished while our activity was paused...

B4X:
Sub Activity_Resume
    'Check whether a job has finished while the activity was paused.
    If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

In JobDone we get all successful urls...

B4X:
For i = 0 To HttpUtils.Tasks.size - 1
   link = HttpUtils.Tasks.Get(i)
   If HttpUtils.IsSuccess(link) Then UrlDone(link)
Next

UrlDone fetches the resulting string...

B4X:
aStr = HttpUtils.GetString (sUrl)

In HttpUtils.GetString.....

B4X:
Sub GetString(URL As String) As String
   If IsSuccess(URL) = False Then
      Log("Task not completed successfully.")
      Return ""
   End If
   Return File.GetText(HttpUtilsService.TempFolder, SuccessfulUrls.Get(URL))
End Sub

.... Which leads me to my question:

If the job finished while the activity was paused and we then check all successful URLs again, what happens if the service was killed in the meantime? Wouldn't HttpUtilsService.TempFolder be null or at the very least empty? I am wondering this because if the service was killed /stopped then would this string even exist anymore?
 

thedesolatesoul

Expert
Licensed User
Longtime User
You are correct. If TempFolder is not initialized somewhere else.

They way to test this, is to start a download, press home straight away. Wait a while for the download to finish. Force Stop, the service. (Settings>Applications>Running service), then run/resume the app again.

The async nature of the service and app probably requires that you write any settings/lists etc out to file at the end of the service execution. On activity resume check if the service is paused, in that case load the settings/lists from file otherwise use the variables in memory.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
My thought was rather than declaring that variable (TempFolder) in the service module itself, instead declare it in the other module that interacts with the service module.

In other words, instead of HttpUtilsService.TempFolder, what if I declare it in HttpUtils instead? (HttpUtils.TempFolder)

Or perhaps make it a global in the main module and store / restore this with the State Manager? (Main.TempFolder, or in a common code module... Common.TempFolder)

My app works perfect for many people but I do see occasional errors in my console. Since nobody bothers to email me with details and I have no way of reproducing them, I am left to try and guess where the problems are occurring.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Kevin,
It doesnt matter where the variable is declared because it is in Process_Globals so it is always accessible from any module.
The problem lies in the initialization.

The case we are talking about is when a service gets destroyed, and an activity resumes and tries to access the variable, it is not initialized (because it gets initialized in the Service_Create sub).

I am not sure how you are using services, but I did carefully have to think about where to initialize variables, when to load them, when to save them.

In this case both the Activity and the Service need the TempFolder variable. It seems that it should be set in both places if possible as long as it is initialized to the same value in both cases.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Wouldn't HttpUtilsService.TempFolder be null or at the very least empty? I am wondering this because if the service was killed /stopped then would this string even exist anymore?

Process global variables from all modules are kept "alive" until the whole process is killed. There life cycle is not related to the containing module.
 
Upvote 0
Top