-2

i got an error Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference everytime i build my app

here's code of my error

07-26 15:04:32.587 18897-18915/com.example.study E/AndroidRuntime: FATAL EXCEPTION: Thread-385
Process: com.example.study, PID: 18897
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v7.app.AlertDialog android.support.v7.app.AlertDialog$Builder.create()' on a null object reference
     at com.example.study.Splash.checking(Splash.java:66)
     at com.example.study.Splash$2.run(Splash.java:51)

it happened on my splash activity, here's my code

package com.example.study;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

import com.example.study.helper.SessionManager;
import com.example.study.util.ConnectionDetector;

public class Splash extends AppCompatActivity {

    private ConnectionDetector cd;
    Boolean isInternetPresent = false;
    protected SessionManager session;

    private AlertDialog.Builder builder;


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


        AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);

        session = new SessionManager(getApplicationContext());
        cd = new ConnectionDetector(getApplicationContext());

        builder.setTitle("No Connection");
        builder.setMessage("Check Your Internet Connection.");
        builder.setIcon(R.drawable.fail);
        builder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int id) {

                    /* TODO Auto-generated method stub */
                dialog.dismiss();
            }
        });

        Thread timer = new Thread(){
            public void run(){
                try {
                    sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    checking();
                }
            }
        };
        timer.start();
    }

    public void checking() {

        isInternetPresent = cd.isConnectingToInternet();

        if(isInternetPresent) {
            session.checkLogin();
            finish();
        } else {
            builder.create().show();
            finish();
        }
    }
}

dunno what to do ... please help to solve this problem,

Shino Rascal
  • 157
  • 1
  • 2
  • 10

2 Answers2

3

I think it fails at your checking() method. You have declared a global variable named builder, then you declared another variable inside your onCreate(). In your checking() method, it refers to the global variable which you didn't initialize, only declare.

Possible solution, edit this :

AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);

to

builder = new AlertDialog.Builder(Splash.this);
Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43
  • 'Can't create handler inside thread that has not called Looper.prepare()' happened at my 'checking();' – Shino Rascal Jul 26 '16 at 09:17
  • Ah I think it's because your builder is started from outside of Main Thread which is the UI thread (I forgot the fancy names), I am currently Googling stuffs, use keyword `Alertdialog in Handler` for reference – Kevin Murvie Jul 26 '16 at 09:29
  • I can't find any good answer and this might be a bit hacky and I don't know it might work buy worth trying, try using `runOnUiThread()` method like described here http://stackoverflow.com/questions/11140285/how-to-use-runonuithread, and may I know what's the purpose of `finish()` after `builder.create().show();`? – Kevin Murvie Jul 26 '16 at 09:54
  • `finish()` this is for the activity.. means close the activity after the alert dialog positive button was pressed ... or let me know if i was wrong .. – Shino Rascal Jul 26 '16 at 21:28
  • If you do `finish()` after `show()`, it'll finish the activity, right after showing the alertdialog.. You should add `finish()` right after `dialog.dismiss();` in your `Dialog` `OK` button method.. I'm sorry but I can only help you that much as I am not so proficient at using handlers – Kevin Murvie Jul 27 '16 at 02:08
0

Please use this below code for reference, it's working fine for me.

Before onCreate()

AlertDialog.Builder alertDialog;

Within OnCreate()

alertDialog = new AlertDialog.Builder(YourActivity.this);

Create AlertDialog

alertDialog.setMessage("Do you want to continue ?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       //write your code here after click yes
    }
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
     }
});
alertDialog.show();
Aftab Alam
  • 1,969
  • 17
  • 17