2

How to fix

method invocation setIcon may produce java.lang.NullPointerException?

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]); 
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
} 

I'm getting this message when setting icons to a tab layout.

Eldelshell
  • 6,683
  • 7
  • 44
  • 63
chinu
  • 37
  • 6
  • Welcome to Stack Overflow! To give you a great answer, it might help us if you have a glance at [ask] if you haven't already. It might be also useful if you could provide a [mcve]. – Mat Jan 10 '17 at 07:40
  • In this example this probably means the `tabLayout` variable is null at the moment. Please provide more of your code to show where it should initialize. – Simon Baars Jan 10 '17 at 12:08
  • 1
    @SimonBaars It that variable were `null`, then his IDE would complain about `getTabAt`, not `setIcon`. – Tom Jan 13 '17 at 18:28
  • @SimonBaars That could be null, but `getTabAt(n)` can also return null. – Christopher Schneider Jan 13 '17 at 18:30
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – nasukkin Jan 13 '17 at 19:20

4 Answers4

1

Something like this should fix your warning:

private void setupTabIcons() {
    if (tabLayout!=null){
      if (tabLayout.getTabAt(0)!=null)
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
      if (tabLayout.getTabAt(1)!=null)
        tabLayout.getTabAt(1).setIcon(tabIcons[1]); 
      if (tabLayout.getTabAt(2)!=null)
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    }
} 
Alexey Rogovoy
  • 691
  • 6
  • 13
  • While this resolves it, it's not a clean solution. I'm assuming this is Android TabLayout. `getTabCount` is available, so you could just check the number of tabs once. – Christopher Schneider Jan 13 '17 at 18:40
1

You need to check that tabLayout is not null

if(tabLayout == null){
    return;
}

And also check that whatever getTabAt returns is not null

ActionBar.Tab x = tabLayout.getTabAt(0);
if(x != null){
    x.setIcon(tabIcons[0]);
}

BTW, you probably have some misconfiguration on your IDE since this is usually a warning, not an error.

Eldelshell
  • 6,683
  • 7
  • 44
  • 63
0

You need to check that getTabAt(x) returns is not null:

private void setupTabIcons(TabLayout tabs) {
    int tabIcons[] = {R.drawable.icon1, R.drawable.icon2, R.drawable.icon3};
    TabLayout.Tab tab;

    for (int x=0; x<3; x++) {
        tab = tabs.getTabAt(x);
        if(tab != null){
            tab.setIcon(tabIcons[x]);
        }
    }
}
B.HoucinE
  • 31
  • 4
0

When your setIcon method called before the tabLayout is setup with tabLayout.setupWithViewPager(viewPager);, this will throw a null pointer exception in runtime.

To fix the run time error, you should call setupTabIcons() after your tabLayout.setupWithViewPager(viewPager); line in onCreate method of the activity.

But this still shows the warning in android studio, so to remove the warning and also prevent run time error, you should change your code to get tab index instead of setting it manually such as 0,1,2 as in your .getTabAt(0), .getTabAt(1), .getTabAt(2) parts

For the clearance, I will put an example from my code:

private TabAdapter tabAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;

private int[] tabIcons = {
        R.drawable.ic_action_profile,
        R.drawable.ic_action_people,
        R.drawable.ic_action_messages
};

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

    viewPager = findViewById(R.id.viewPager);
    tabLayout = findViewById(R.id.tabLayout);

    // Create the adapter that will return a fragment for each of the two
    // primary sections of the activity.
    tabAdapter = new TabAdapter(getSupportFragmentManager());
    tabAdapter.addFragment(new AccountFragment(), "Account");
    tabAdapter.addFragment(new HomeFragment(), "People");
    tabAdapter.addFragment(new CommunicateFragment(), "Messages");

    viewPager.setAdapter(tabAdapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setupWithViewPager(viewPager);

    for (int i=0; i<tabLayout.getTabCount();i++) {
        tabLayout.getTabAt(i).setIcon(tabIcons[i]);
    }
}

Note: Please make sure to have equal amount of icons in tabIcons array as you are creating tabs.

Janaka R Rajapaksha
  • 3,585
  • 1
  • 25
  • 28