Android The local variable tst may not have been initialized

My work code

@Override public boolean onMenuItemSelected(int featureId, MenuItem item) { Toast tst; if (item.getItemId() == R.id.menuVada) { tst = Toast.makeText(Main.this, " Vada ", 2000); } else { tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle() + " Menu ID: " + item.getItemId(), 2000); } tst.setGravity(Gravity.CENTER, 0, 0); tst.show(); return true; } 

but this code does not work

 @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { Toast tst; if (item.getItemId() == R.id.menuVada) { //tst = Toast.makeText(Main.this, " Vada ", 2000); } else { tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle() + " Menu ID: " + item.getItemId(), 2000); } tst.setGravity(Gravity.CENTER, 0, 0); tst.show(); return true; } 

why doesn't my code work?

Please, help.

Best wishes

+3
android
source share
7 answers

What if (item.getItemId() == R.id.menuVada) ?

just initialize tst to null . And then check if tst!=null shows a toast

 @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { Toast tst = null; if (item.getItemId() == R.id.menuVada) { //tst = Toast.makeText(Main.this, " Vada ", 2000); } else { tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle() + " Menu ID: " + item.getItemId(), 2000); } if(tst!=null){ tst.setGravity(Gravity.CENTER, 0, 0); tst.show(); } return true; } 
+6
source share

Your variable Toast tst; must be initialized as in if and else .

becasue, if you find your if() condition is true , then the else part will never be called. and the application thread will move on to the next and throw a NullPointerException

+1
source share

Yes, in the if tst branch is not initialized. But if you show Toast in only one branch, I would include the declaration, gravity and show the lines in this branch.

 @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (item.getItemId() == R.id.menuVada) { } else { Toast tst = Toast.makeText(Main.this, "Menu Title: " + item.getTitle() + " Menu ID: " + item.getItemId(), 2000); tst.setGravity(Gravity.CENTER, 0, 0); tst.show(); } return true; } 

Otherwise use Serif.

+1
source share

You must initialize the variable before using it. Try the following:

 Toast tst = null; 

But you will get a null-reference exception if the code does not accept the else branch. So, in a sense, you can still say that the code is not working ... It just compiles.

0
source share

Because if your test if() is true, it will go to the empty branch, where the purpose of tst commented out. tst therefore not initialized when you go to tst.setGravity() .

0
source share

I think you should make it static. Like

 Static tst; 

Or do this to save tst in another variable, then attach your Toast class as

 Toast t = new Toast(); t.tst = // your variable 

Hope it works

0
source share

You can just try:

 @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { if (item.getItemId() == R.id.menuVada) { Toast.makeText(Main.this, " Vada ", 2000); } else { Toast.makeText(Main.this, "Menu Title: " + item.getTitle() + " Menu ID: " + item.getItemId(), 2000); } return true; } 

EDIT to comment.

-one
source share

All Articles