-2

I have a line in an Android app which reads as follows:

Intent intent = new Intent(this,MyView.class);

Unfortunately, it appears that on occasions this crashes, however I haven't been able to reproduce the crash, possibly because I don't understand in what circumstances this would fail. The crash is a java.lang.NullPointerException as you may have already guessed.

From reading other solutions, I see that I can replace this with getApplicationContext(), but that still doesn't help as I want to understand:

  • In what circumstances this might happen
  • Why this doesn't work correctly

I am of course assuming this is the problem - I'm assuming that MyView.class would never be null, which I think is correct?

For completeness, I should explain that this is written within a callback. Here is the complete code for the function:

 @Override
    public void didReceiveLoginResponse(boolean status) {
        if(status) {
            Intent intent = new Intent(this,MyView.class);
            startActivity(intent);
        } 
    }

Many thanks for any helpful ideas!

Edit: To be clear, I am not asking what a nullPointerException is. I am asking in what circumstances would this line contain a nullPointerException. If the view no longer exists, how can the function even run (surely the null bit would be the view/function, not a specific line within it?)

Ben
  • 3,981
  • 4
  • 28
  • 49
  • @KlingKlang please see my edit - the suggested duplicate doesn't answer the question I am asking. Thanks :) – Ben Sep 24 '18 at 12:30

3 Answers3

1

My guess is the calling Activity is ending or has ended when the async task finished (perhaps someone launches then quickly hits back), in this case the context is no longer valid.

Try checking isFinishing()

Mark Sheekey
  • 538
  • 5
  • 20
  • This seems like a sensible answer, but I don't fully understand it. If the context is no longer valid, how can the function even run? – Ben Sep 24 '18 at 12:31
0

I think because I have seen it in some asynchronous task like retrofit Callback (where is not possible to use this inside the Callback) that your callback is sometimes falling out of the scope of your main threat Activity, and that's why you are not able to get this instance of context. It is anyways weird that sometimes it happens and sometimes it doesn't.

My suggestion is that you should pass the this instance as a parameter to your Callback if you have that possibility like:

 @Override
    public void didReceiveLoginResponse(Activity activity, boolean status) {
        if(status) {
            Intent intent = new Intent(activity,MyView.class);
            startActivity(intent);
        } 
    }
YeinCM-Qva
  • 121
  • 1
  • 15
0

you should modify this with MainActivity.this or whatever your activity name is. e.g

@Override
    public void didReceiveLoginResponse(boolean status) {
        if(status && MainActiity.this!=null) {
            Intent intent = new Intent(MainActiity.this,MyView.class);
            startActivity(intent);
        } 
    }
asim
  • 300
  • 1
  • 8