0

After setting the elevation of the action bar programmatically, the code becomes highlighted in yellow and the warning below gets thrown. What can be done to remove this warning?

getSupportActionBar().setElevation(0);

Warning

Method invocation 'getSupportActionBar().setElevation(0);' may produce 'java.lang.NullPointerException'

wbk727
  • 8,017
  • 12
  • 61
  • 125

2 Answers2

4

Add a null check:

if(getSupportActionBar() != null) {
    getSupportActionBar().setElevation(0);
}
VM4
  • 6,321
  • 5
  • 37
  • 51
2
Try:


ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
   actionBar.setElevation(0);
}
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34