• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps setVisibility on a null object reference

Kurniawan

Newbie
hello. i have error like this :


E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yk.danaberkah, PID: 15413
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference
at com.yk.danaberkah.MainActivity.onNavigationItemSelected(MainActivity.java:154)
at com.google.android.material.navigation.NavigationView$1.onMenuItemSelected(NavigationView.java:170)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
at com.google.android.material.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:352)
at android.view.View.performClick(View.java:4863)
at android.view.View$PerformClick.run(View.java:19981)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
I/Process: Sending signal. PID: 15413 SIG: 9
Application terminated.


this my MainActivity.java

Java:
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    LinearLayout kontenhome, kontendonasi, kontenwakaf, kontenzakat, kontennotifikasi, konteninformasi, kontensyaratdanketentuan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        kontenhome.setVisibility(View.VISIBLE);
        kontendonasi.setVisibility(View.GONE);
        kontenwakaf.setVisibility(View.GONE);
        kontenzakat.setVisibility(View.GONE);
        kontennotifikasi.setVisibility(View.GONE);
        konteninformasi.setVisibility(View.GONE);
        kontensyaratdanketentuan.setVisibility(View.GONE);
    }
    if (id == R.id.nav_donasi) {
        kontendonasi.setVisibility(View.VISIBLE);
        kontenhome.setVisibility(View.GONE);
        kontenwakaf.setVisibility(View.GONE);
        kontenzakat.setVisibility(View.GONE);
        kontennotifikasi.setVisibility(View.GONE);
        konteninformasi.setVisibility(View.GONE);
        kontensyaratdanketentuan.setVisibility(View.GONE);
    } else if (id == R.id.nav_wakaf) {
        kontenwakaf.setVisibility(View.VISIBLE);
        kontenhome.setVisibility(View.GONE);
        kontendonasi.setVisibility(View.GONE);
        kontenzakat.setVisibility(View.GONE);
        kontennotifikasi.setVisibility(View.GONE);
        konteninformasi.setVisibility(View.GONE);
        kontensyaratdanketentuan.setVisibility(View.GONE);

    } else if (id == R.id.nav_zakat) {
        kontenzakat.setVisibility(View.VISIBLE);
        kontenhome.setVisibility(View.GONE);
        kontendonasi.setVisibility(View.GONE);
        kontenwakaf.setVisibility(View.GONE);
        kontennotifikasi.setVisibility(View.GONE);
        konteninformasi.setVisibility(View.GONE);
        kontensyaratdanketentuan.setVisibility(View.GONE);

    } else if (id == R.id.nav_notification) {
        kontennotifikasi.setVisibility(View.VISIBLE);
        kontenhome.setVisibility(View.GONE);
        kontendonasi.setVisibility(View.GONE);
        kontenwakaf.setVisibility(View.GONE);
        kontenzakat.setVisibility(View.GONE);
        konteninformasi.setVisibility(View.GONE);
        kontensyaratdanketentuan.setVisibility(View.GONE);

    } else if (id == R.id.nav_informasi) {
        konteninformasi.setVisibility(View.VISIBLE);
        kontenhome.setVisibility(View.GONE);
        kontendonasi.setVisibility(View.GONE);
        kontenwakaf.setVisibility(View.GONE);
        kontenzakat.setVisibility(View.GONE);
        kontennotifikasi.setVisibility(View.GONE);
        kontensyaratdanketentuan.setVisibility(View.GONE);

    } else if (id == R.id.nav_syarat_dan_ketentuan) {
        kontensyaratdanketentuan.setVisibility(View.VISIBLE);
        kontenhome.setVisibility(View.GONE);
        kontendonasi.setVisibility(View.GONE);
        kontenwakaf.setVisibility(View.GONE);
        kontenzakat.setVisibility(View.GONE);
        kontennotifikasi.setVisibility(View.GONE);
        konteninformasi.setVisibility(View.GONE);

    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

setVisibility on a null object reference, why?
Thanks :)
 
i initialize like this but i found the same error, why?

Java:
navigationView.getMenu().getItem(0).setChecked(true);
kontenhome = findViewById(R.id.nav_home);
kontendonasi = findViewById(R.id.nav_donasi);
kontenwakaf = findViewById(R.id.nav_wakaf);
kontenzakat = findViewById(R.id.nav_zakat);
kontennotifikasi = findViewById(R.id.nav_notification);
konteninformasi = findViewById(R.id.nav_informasi);
kontensyaratdanketentuan = findViewById(R.id.nav_syarat_dan_ketentuan);
 
Make sure they are in activity_main.xml.
i dont know still not working and the same error, thanks for ur help :)
Java:
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    LinearLayout kontenhome, kontendonasi, kontenwakaf, kontenzakat, kontennotifikasi, konteninformasi, kontensyaratdanketentuan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.getMenu().getItem(0).setChecked(true);
        kontenhome = findViewById(R.id.nav_home);
        kontendonasi = findViewById(R.id.nav_donasi);
        kontenwakaf = findViewById(R.id.nav_wakaf);
        kontenzakat = findViewById(R.id.nav_zakat);
        kontennotifikasi = findViewById(R.id.nav_notification);
        konteninformasi = findViewById(R.id.nav_informasi);
        kontensyaratdanketentuan = findViewById(R.id.nav_syarat_dan_ketentuan);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
  
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {
            kontenhome.setVisibility(View.VISIBLE);
            kontendonasi.setVisibility(View.GONE);
            kontenwakaf.setVisibility(View.GONE);
            kontenzakat.setVisibility(View.GONE);
            kontennotifikasi.setVisibility(View.GONE);
            konteninformasi.setVisibility(View.GONE);
            kontensyaratdanketentuan.setVisibility(View.GONE);
        }
        if (id == R.id.nav_donasi) {
            kontendonasi.setVisibility(View.VISIBLE);
            kontenhome.setVisibility(View.GONE);
            kontenwakaf.setVisibility(View.GONE);
            kontenzakat.setVisibility(View.GONE);
            kontennotifikasi.setVisibility(View.GONE);
            konteninformasi.setVisibility(View.GONE);
            kontensyaratdanketentuan.setVisibility(View.GONE);
        } else if (id == R.id.nav_wakaf) {
            kontenwakaf.setVisibility(View.VISIBLE);
            kontenhome.setVisibility(View.GONE);
            kontendonasi.setVisibility(View.GONE);
            kontenzakat.setVisibility(View.GONE);
            kontennotifikasi.setVisibility(View.GONE);
            konteninformasi.setVisibility(View.GONE);
            kontensyaratdanketentuan.setVisibility(View.GONE);

        } else if (id == R.id.nav_zakat) {
            kontenzakat.setVisibility(View.VISIBLE);
            kontenhome.setVisibility(View.GONE);
            kontendonasi.setVisibility(View.GONE);
            kontenwakaf.setVisibility(View.GONE);
            kontennotifikasi.setVisibility(View.GONE);
            konteninformasi.setVisibility(View.GONE);
            kontensyaratdanketentuan.setVisibility(View.GONE);

        } else if (id == R.id.nav_notification) {
            kontennotifikasi.setVisibility(View.VISIBLE);
            kontenhome.setVisibility(View.GONE);
            kontendonasi.setVisibility(View.GONE);
            kontenwakaf.setVisibility(View.GONE);
            kontenzakat.setVisibility(View.GONE);
            konteninformasi.setVisibility(View.GONE);
            kontensyaratdanketentuan.setVisibility(View.GONE);

        } else if (id == R.id.nav_informasi) {
            konteninformasi.setVisibility(View.VISIBLE);
            kontenhome.setVisibility(View.GONE);
            kontendonasi.setVisibility(View.GONE);
            kontenwakaf.setVisibility(View.GONE);
            kontenzakat.setVisibility(View.GONE);
            kontennotifikasi.setVisibility(View.GONE);
            kontensyaratdanketentuan.setVisibility(View.GONE);

        } else if (id == R.id.nav_syarat_dan_ketentuan) {
            kontensyaratdanketentuan.setVisibility(View.VISIBLE);
            kontenhome.setVisibility(View.GONE);
            kontendonasi.setVisibility(View.GONE);
            kontenwakaf.setVisibility(View.GONE);
            kontenzakat.setVisibility(View.GONE);
            kontennotifikasi.setVisibility(View.GONE);
            konteninformasi.setVisibility(View.GONE);

        }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
 
Last edited:
Is there an app or any way to access the ability to see and/or use hidden apps and functions on a Galaxy Note 9? I don't have a PC or a laptop to fix it by applying the code via the USB port...
 
You have to put all your linearLayout in activity_main.xml. Otherwise you will get null pointer exception since the compiler can't find the id.
 
You have to put all your linearLayout in activity_main.xml. Otherwise you will get null pointer exception since the compiler can't find the id.
oh i know now, i placed the wrong id. i should put ID linear layout but i put ID on menu navigation. Thanks wseng92 :D:D
 
Back
Top Bottom