Jacob Platin Jacob Platin - 7 months ago 42
Java Question

Android error on Attempt to invoke virtual method; I don't understand it

I am trying to build a simple navigation drawer, but I keep getting this annoying error. I am fairly new to Android so I really don't know how to fix/what it means. Any help is appreciated.

Java:

package com.google.helpstl2;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import java.util.List;

public class Home extends ActionBarActivity implements OnItemClickListener {

private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
planets = getResources().getStringArray(R.array.planets);
listView = (ListView) findViewById(R.id.drawerList);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, planets));
listView.setOnItemClickListener(this);

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, 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 void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this,planets[position],Toast.LENGTH_LONG).show();
}
}


Relevant XML?

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<ListView android:id="@+id/drawerList"
android:layout_width="240dp"

android:layout_height="match_parent"
android:layout_gravity="left"
/>
</android.support.v4.widget.DrawerLayout>


Logcat error output is :

Process: com.google.helpstl2, PID: 2205 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.helpstl2/com.google.helpstl2.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

Answer

May be your xml layout file with content :

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView android:id="@+id/drawerList"
        android:layout_width="240dp"

        android:layout_height="match_parent"
        android:layout_gravity="left"
        />
</android.support.v4.widget.DrawerLayout>

is not activity_main.xml so you are getting NPE when finding id : drawerList

Check this thing one time.

You can add null check at the error line :

if(listView != null ) {    
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, planets));
            listView.setOnItemClickListener(this);
}