8

I am getting this error message saying that Variable "snackbar" might not have been initialized.

The following is part of my MainActivity.java code:

public class NetworkChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

    if (info != null && info.isConnected()) {
        final Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "WiFi change detected; updating information...", Snackbar.LENGTH_LONG)
            .setAction("DISMISS", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    snackbar.dismiss();
                }
            });
            snackbar.setActionTextColor(Color.BLUE);
            snackbar.show();

            getWifi();
    }
}
}

I had read the accepted Stack Overflow answer here and it suggests that I initialize snackbar immediately after public class NetworkChangeReceiver extends BroadcastReceiver. So, I changed the code to:

/* no changes here */
@Override
public void onReceive(Context context, Intent intent) {
    Snackbar snackbar = null;

    /* no changes here */
    if (...) {
        snackbar = Snackbar.make(...);
    }

    /* no changes here */
}

But this gives me an error Variable "snackbar" is accessed from within inner class, needs to be declared final and a warning Method invocation "snackbar.dismiss()" may produce "java.lang.NullPointerException".

Any suggestions on how to solve this? Thanks!

Community
  • 1
  • 1

2 Answers2

9

Split up your chained method calls:

final Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "WiFi change detected; updating information...", Snackbar.LENGTH_LONG);
snackbar.setAction("DISMISS", new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      snackbar.dismiss();
    }
  });

This way, snackbar is definitely assigned before you create the OnClickListener.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Just curious, is this a bug in Android studio or am I meant to code it the way you've suggested? –  Feb 04 '16 at 15:04
  • This is not a bug. It is because of the way that anonymous classes are implemented, which is actually quite clear once you poke around a bit. I was actually writing up a detailed explanation... will continue and post a link here. – Andy Turner Feb 04 '16 at 15:06
  • Thanks @Andy Turner, looking forward to reading your article :) –  Feb 04 '16 at 15:07
  • 1
    @YikJin http://stackoverflow.com/questions/35204884/variable-example-might-not-have-been-initialized-in-anonymous-class/35204885#35204885 – Andy Turner Feb 04 '16 at 15:24
  • 1
    Thanks @Andy Turner! That was helpful! Hope people who had this question find your answer useful too. –  Feb 04 '16 at 15:40
-5

As a workaround to

Variable "snackbar" is accessed from within inner class, needs to be declared final

you can make that variable a final one-element array

Meegoo
  • 417
  • 5
  • 13