0

I'm trying to use the IntentService feature that Android provides to try running some calculations in the background.

I've been working on this since yesterday, but I can't seem to find any valid solution.
I've looked at quite a few SO questions with regards to IntentService and tried applying the suggested fixes, but mine just can't seem to work.

I've tried passing in various different context / Activies / getApplicationContext() / this / getBaseContext() from the Activity where I call the function but I'm still getting a null object reference error

"Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference"

Where the application crashes at the 3rd line in the function where startService() is called (see my code below)

//This function is from MyCurrentClass.class - it is a Singleton and its being called from another activity where it passes in its context
public void notify (Context context, JSONObject responseFromDb){

    Intent inputIntent = new Intent(context, MyCurrentClass.class);
    inputIntent.putExtra("responseFromDB", responseFromDb.toString());
    this.startService(inputIntent); //crashes here!
}

P.S.: Most of the solutions I've looked at seem to have only 1 Activity, but my app is switching between different Activities. (Not sure if that matters) But most importantly the class I'm working on is a normal Java class that extends IntentService and its not an Activity class.

I've also declared the service in my Android manifest to link to MyCurrentClass and tried bindService() from the Activity: it was called, but it didn't work too.

Below are some of the questions/solutions I've looked at and tried but failed:

I've actually looked at many others posts, but there were too many.

Hope the info I've provided is enough. If it isn't, do let me know what might be missing and I'll update the question with the required info.

Community
  • 1
  • 1
Candiie
  • 23
  • 5
  • Please show a [mcve]. What is `this`? Also show the manifest – OneCricketeer Sep 14 '16 at 06:22
  • Call `startService()` on the `Context` you're passing into the method. Also, if you're handing a `Context` to `MyCurrentClass`, if probably shouldn't be a `Context` itself, and your method could be `static`. – Mike M. Sep 14 '16 at 06:23
  • @MikeM. Question... Why would `this.startService` throw a NPE for `Context.startService`? Wouldn't that mean `this` is null, which isn't really possible? – OneCricketeer Sep 14 '16 at 06:25
  • @cricket_007 Whatever type `MyCurrentClass` is, I'm assuming it's ultimately a `ContextWrapper`, which keeps a `Context` field that methods like `startService()` and `startActivity()` actually get called on. When a class like that isn't instantiated correctly by the system, that field is null, thus the NPE. – Mike M. Sep 14 '16 at 06:36
  • @MikeM. Yes! "Call startService() on the Context you're passing into the method." was actually the solution ! However you didn't post it as an answer.... do you want to post it as an answer for me to accept? Though Pavneet Singh had mentioned the same solution below too...but I think you mentioned it first so I think its courteous to ask if you would like to post an answer. Otherwise I'll accept his as the correct solution :) – Candiie Sep 14 '16 at 08:15
  • @cricket_007 Thanks for helping out! & Thanks for the sample link, I'll be sure to show a Minimal, Complete & Verifiable example the next time I ask a question! – Candiie Sep 14 '16 at 08:17
  • Doesn't have to be next time. Please [edit] your current question – OneCricketeer Sep 14 '16 at 08:19
  • That's quite all right. Please go ahead and accept Pavneet's answer. Thank you very much for the offer, though. I really appreciate it. Do keep in mind the rest of my comment, however. I think `MyCurrentClass` might be extending a class that it doesn't need to, and that may cause you similar headaches in the future. – Mike M. Sep 14 '16 at 08:21
  • @Rotwang it's the same NPE but considering the the question context it's not the same.i am saying this cuz i have seen marking duplicate question without understanding the question context.what if the question was, unable to start activity from utility class. the major thing here is understanding the context object not the NPE.i hope you consider my suggestion .thanks – Pavneet_Singh Sep 14 '16 at 09:23
  • @PavneetSingh I'm so tired to say this. `EVERY NPE IS THE SAME: AN OBJECT IS USED BEFORE IT IS INSTANCED`. Hope it's clear, now. – Phantômaxx Sep 14 '16 at 09:41
  • @MikeM. Hi! Thanks & Noted! As for MyCurrentClass it is currently extending a IntentService in order for "onHandleIntent" to be called and run in the bg, I'll update my question again to better clarify my previous query as suggested by cricket_007 :) – Candiie Sep 14 '16 at 17:10
  • @PavneetSingh Thank you so much for helping to justify I do feel the same way too! I sincerely do not think that it is a duplicate considering the context of the question. – Candiie Sep 14 '16 at 17:11
  • @Rotwang Hi, yes indeed "EVERY NPE IS THE SAME: AN OBJECT IS USED BEFORE IT IS INSTANCED" however, depending on the context of the question, I would like to further justify that for my questions, ALL of my objs were instanced before it was used, indeed using "this" was wrong (and u might say that its bcos the class wasn't instanced thts why the NPE was thrown) however the correct fix for the question wasn't exactly caused by "this" directly it was actually because I was using the wrong object as suggested by Mike and Pavneet and "this" wasn't even supposed to be called from the beginning. – Candiie Sep 14 '16 at 17:18
  • Ah, I hadn't noticed that the `Intent` you're starting was targeting `MyCurrentClass` itself, and that it's an `IntentService`. My bad. It was late when I read this. :-) I see what you're trying to do, now. Anyway, if you're going to do it like that, it's still advisable to make your `notify()` method `static`, just so you don't accidentally try to use the current instance. Cheers! – Mike M. Sep 14 '16 at 17:21
  • @Rotwang also, if I were to stumble upon this page - http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it that was the suggested fixed/duplicate given by you, I still wouldn't be able to get my NPE issue above resolved, because, the main issue here isn't about NPE, the main problem here is actually using the "context" properly and I do believe that my previous title had mentioned that. Do reconsider retracting the "duplicate" . Best Regards, Candiie – Candiie Sep 14 '16 at 17:21
  • @Candiie your welcome dude and it was my pleasure that i could help you.@Rotwang was actually trying to tell why NPE occures so that you can solve the problem on your own which may or may not take little extra time though it's always good to have a specific details than general but we also grow when we push our limits so we need both the pusher and helper so try your best be wise happy coding – Pavneet_Singh Sep 14 '16 at 17:22
  • @MikeM. XD no worries! Thank you so much ~ now its late for my side of the earth XD and yupp! I'll take in the suggestion to make it static :) Thank you once again!!! – Candiie Sep 14 '16 at 17:24
  • 1
    @PavneetSingh yupp :) I do understand from his/her POV too, however it was actually because I was looking the issue itself for more den 24hrs straight and keep unable to resolve it thats why I decided to came here to seek help ><" also because android is still a little confusing to me, if it was a normal java app w/o android framework it would had been easier to debug ~ but yupp! I do agree with u that we both need the pusher and the helper XD !!! Thank you for your encouraging words and Yupp! Coding is awesome !!! ~ Thank you so much again !!! – Candiie Sep 14 '16 at 17:30
  • again my pleasure and sleep well – Pavneet_Singh Sep 14 '16 at 17:36
  • @Candiie. AGAIN (moan): `EVERY NPE IS CREATED EQUAL AND HAS THE VERY SAME FIX` - **Find the uninstanced object and instance it BEFORE using it**. The earlier you learn this, the better your programming life will be. Enough said – Phantômaxx Sep 15 '16 at 08:07

1 Answers1

2

Use context the like you mentioned in the comments which you are receiving from activity because in this case your current class has no context.read for further details

context.startService(inputIntent); 

instead of

this.startService(inputIntent); 
Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68