-5

I've been trying to retrieve the data from a preloaded database and I always getting an error of a NullPointerException. I'm trying to retrieve the data to a listview. I don't know the what the problem is. Please help me. Thanks.

public class Retrieve extends Activity {  
Dbhelper dbhelper;  
protected ListAdapter adapter;
SQLiteDatabase db;  
@Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.ret_all_info);    
String[] from = new String[] { "_id" };  
int[] to = new int[] { R.id.studid};  
dbhelper = new Dbhelper(this);  
try {  
dbhelper.createDataBase();  
} catch (IOException e) {  
 // TODO Auto-generated catch block  
e.printStackTrace();  
}  

Cursor c = dbhelper.getData();  

adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.ret_all, c, from, to, 0);  

ListView list = (ListView) findViewById(R.id.ListView1);  

list.setAdapter(adapter);  

list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub

Intent intent = new Intent(Retrieve.this, RetrieveDetails.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("COMMAND_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);


}

});
}  
}  

Here is the Logcat

02-13 21:05:11.402: E/Trace(22808): error opening trace file: No such file or directory (2)
02-13 21:05:11.572: E/dalvikvm(22808): GC_CONCURRENT freed 314K, 9% free 6590K/7239K, paused 2ms+12ms, total 28ms
02-13 21:05:22.832: E/Trace(22895): error opening trace file: No such file or directory (2)
02-13 21:05:22.972: E/dalvikvm(22895): GC_CONCURRENT freed 305K, 10% free 6577K/7239K, paused 13ms+3ms, total 37ms
02-13 21:05:23.192: E/AndroidRuntime(22895): FATAL EXCEPTION: main
02-13 21:05:23.192: E/AndroidRuntime(22895): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutsiroll/com.example.tutsiroll.Retrieve}: java.lang.NullPointerException
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.access$600(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.os.Handler.dispatchMessage(Handler.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.os.Looper.loop(Looper.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.ActivityThread.main(ActivityThread.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at java.lang.reflect.Method.invokeNative(Native Method) 
02-13 21:05:23.192: E/AndroidRuntime(22895):    at java.lang.reflect.Method.invoke(Method.java:511) 
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at dalvik.system.NativeStart.main(Native Method)
02-13 21:05:23.192: E/AndroidRuntime(22895): Caused by: java.lang.NullPointerException
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.example.tutsiroll.Retrieve.onCreate(Retrieve.java:43)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.Activity.performCreate(Activity.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
02-13 21:05:23.192: E/AndroidRuntime(22895):    at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
02-13 21:05:23.192: E/AndroidRuntime(22895):    ... 12 more
Ace123
  • 15
  • 1
  • 8
  • `list.setAdapter(adapter); ` – Ace123 Feb 13 '15 at 13:30
  • most possible causes: there is no `R.id.ListView1` inside `R.layout.ret_all_info` or you are using wrong `dbhelper` (as you have two: local and field) or cursor `c` is null (less possible as there is no sign of `getData` inside stacktrace) – Selvin Feb 13 '15 at 13:33

1 Answers1

0

You indicated the NPE occurs here:

   ListView list = (ListView) findViewById(R.id.ListView1);  
-> list.setAdapter(adapter);  

This means list is null, probably because there is no view with id R.id.ListView1 in R.layout.ret_all_info.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60