0

So I am trying to access a method in another activity, basically to check if there's new data in the chat and if there is, it shows a toast. But I'm getting an error whenever I try to use this, or Activity.this in any function. If I try to use context, it works but rest all functions start giving null.

Here's what I did

DoTheTask.class

Chat c = new Chat();
c.Mymethod("true",id);

Chat.class

public void Mymethod(String fd, String id)
    {
        if(fd.equals("true")){
            Toast.makeText(this,"Chat is open", Toast.LENGTH_LONG).show();
        }
    }

Here the "this" inside toast is giving NPE, I passed context with the method but I want to use this because I have many other functions required which work only with this or activity.this and not context (they give errors)

  • what does makeText method return? – Curious Dec 04 '16 at 22:23
  • this can never be null. You're wrong on what's giving the npe. – Gabe Sechan Dec 04 '16 at 22:25
  • @GabeSechan java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference – Subhranshu Kumar Dec 04 '16 at 22:28
  • @SubhranshuKumar this can never be null. Its the current instance. For this to be null it would need to be called on null- which would have caused an NPE 1 level higher in the callstack. – Gabe Sechan Dec 04 '16 at 22:34

0 Answers0