NullPointerException when setting adapter
- by Sorin Grecu
I'm trying to get the filenames in a folder inside a listview(that listview is inside a layout of a ViewPager) but i'm getting a NullPointerException and i can't figure what's wrong.
public void citire() {
ListView lv1 = (ListView) findViewById(R.id.lv1);
myList = new ArrayList<String>();
File directory = Environment.getExternalStorageDirectory();
file = new File(directory + "/InterSRL");
File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
lv1.setAdapter(adapter);
}
I'm getting the error at lv1.setAdapter(adapter);
06-28 13:10:57.987: E/AndroidRuntime(30450): Caused by: java.lang.NullPointerException
06-28 13:10:57.987: E/AndroidRuntime(30450): at com.example.testlayout.Gestionare.citire(Gestionare.java:169)
06-28 13:10:57.987: E/AndroidRuntime(30450): at com.example.testlayout.Gestionare.onCreate(Gestionare.java:33)
06-28 13:10:57.987: E/AndroidRuntime(30450): at android.app.Activity.performCreate(Activity.java:5104)
06-28 13:10:57.987: E/AndroidRuntime(30450): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-28 13:10:57.987: E/AndroidRuntime(30450): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)