0

I am trying to add in a tabbed activity the app icon to the app bar and the answer is: method may produce 'java.lang.NullPointerException'. I have read and tried all the answers here and none of them it's working for me. I thought I solve it with the assert getSupportActionBar() != null; the error dissapears in MainActivity.java but then app still crashes with

FATAL EXCEPTION: main Process: com.gadgetcatch.test, PID: 25826 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gadgetcatch.test/com.gadgetcatch.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setLogo(int)' on a null object reference

I'm a beginner in Java and I would really like to understand why this happens.

Here is the MainActivity.java text

    package com.gadgetcatch.test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

        /**
         * The {@link android.support.v4.view.PagerAdapter} that will provide
         * fragments for each of the sections. We use a
         * {@link FragmentPagerAdapter} derivative, which will keep every
         * loaded fragment in memory. If this becomes too memory intensive, it
         * may be best to switch to a
         * {@link android.support.v4.app.FragmentStatePagerAdapter}.
         */
        private SectionsPagerAdapter mSectionsPagerAdapter;

        /**
         * The {@link ViewPager} that will host the section contents.
         */
        private ViewPager mViewPager;

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

            assert getSupportActionBar() != null;
            android.support.v7.app.ActionBar ab = getSupportActionBar();
            ab.setLogo(R.drawable.babyicon);

            ab.setDisplayUseLogoEnabled(true);
            ab.setDisplayShowHomeEnabled(true);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            // Create the adapter that will return a fragment for each of the three
            // primary sections of the activity.
            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager) findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);



        }

    }

I added here only the part that concerns my question.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alex Simion
  • 93
  • 1
  • 14

1 Answers1

0

From what I gather, assert keyword usage is discouraged for production code logic. This line will only be hit if (1) assertions are enabled in your IDE when you run your code, (2) if you use java -ea option on from the command line, or (3) possibly by default when you're in debug mode. I'm guessing your IDE recognizes this and removes the warning about the NPE. My suggestion is to use a regular null check here and figure out why getSupportActionBar() is returning a null value.

misterjake
  • 159
  • 5