-1

My class extends AppCompatActivity

This is my class

public class Login extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final Button loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Login login = new Login();
                try {
                    login.getAccessCode();
                }
                catch (IOException e){
                    Log.e("LOGIN", "I got an error", e);
                }

            }
        });
    }

    public void getAccessCode() throws IOException {
        RequestParams params = new RequestParams();
        params.put("a", "a");

        QuidoRestClient.post("/login/generateCode", params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                // If the response is JSONObject instead of expected JSONArray
                Log.d("LOGIN",response.toString());
                String code = "";
                try {
                    code = response.getString("code");
                }
                catch (JSONException e){

                }
                Context context = getApplicationContext();
                CharSequence text = "Hello toast!";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray data) {
                // If the response is JSONArray instead of expected JSONObject
            }
        });
    }
}

This is the error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

I've also tried

Toast toast = Toast.makeText(Login.this, text, duration);

I've solved the issue by calling my method directly instead of Login login = new Login();

Exceptions
  • 1,174
  • 2
  • 9
  • 28

1 Answers1

2

Activities are Contexts (or at least they inherit from Context). So instead of getApplicationContext(), simply use this or YourActivity.this in the first argument of Toast.makeText.

Another thing: don't do Login login = new Login();. Activities are instantiated by the Android runtime, not by us ! Instead, simply call getAccessCode() directly.

(Also, note that AppCompatActivity does not have any getApplicationContext method)


If the error persists, it might come from the fact that the onSuccess callback does not run in the UI thread. If this is the case, possible fixes are:

  1. Don't use Toast, but Log to get debug messages:

    Log.d("your tag", "your message");
    
  2. Enclose your toast creation inside a UI thread runnable:

    runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(MyActivity.this, text, duration).show();
        }
    }); 
    
Derlin
  • 9,572
  • 2
  • 32
  • 53
  • I've tried Login.this and I'm getting this error java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference – Exceptions Mar 29 '17 at 06:53
  • updated my answer – Derlin Mar 29 '17 at 07:03
  • 1
    did my best with the info available at the time. Updated the answer again. – Derlin Mar 29 '17 at 07:15