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

Apps FATAL EXCEPTION:main in android listview

E/AndroidRuntime(8250): FATAL EXCEPTION: main

E/AndroidRuntime(8250): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sample/com.example.sample.MainActivity}: java.lang.NullPointerException

E/AndroidRuntime(8250): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

E/AndroidRuntime(8250): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

E/AndroidRuntime(8250): at android.app.ActivityThread.access$600(ActivityThread.java:141)

E/AndroidRuntime(8250): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

E/AndroidRuntime(8250): at android.os.Handler.dispatchMessage(Handler.java:99)

E/AndroidRuntime(8250): at android.os.Looper.loop(Looper.java:137)

E/AndroidRuntime(8250): at android.app.ActivityThread.main(ActivityThread.java:5039)

E/AndroidRuntime(8250): at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(8250): at java.lang.reflect.Method.invoke(Method.java:511)

E/AndroidRuntime(8250): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

E/AndroidRuntime(8250): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

E/AndroidRuntime(8250): at dalvik.system.NativeStart.main(Native Method)

E/AndroidRuntime(8250): Caused by: java.lang.NullPointerException

E/AndroidRuntime(8250): at com.example.sample.MainActivity.onCreate(MainActivity.java:30)

E/AndroidRuntime(8250): at android.app.Activity.performCreate(Activity.java:5104)

E/AndroidRuntime(8250): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

E/AndroidRuntime(8250): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
 
Where exactly is this error coming from? What device do you have so we can get you the proper support?

Welcome to the forums!
 
package com.example.sample;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {

private static final Context context = null;
SQLiteDatabase sq;
public String DBPath;
public static Context currentContext;
String arg[]=new String[1000];
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
sq=MainActivity.this.openOrCreateDatabase("sample",MODE_PRIVATE,null);
currentContext = context;
DBPath = "/data/data/" + context.getPackageName() + "/databases";
Cursor cur=sq.rawQuery("select * from labels",null);

if(cur!=null)
{
if(cur.moveToFirst())
{
do
{
arg=cur.getString(cur.getColumnIndex(""));
i++;
}while(cur.moveToNext());
}
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arg);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l,View v,int position,long id)
{
super.onListItemClick(l, v, position, id);
Intent in=new Intent(MainActivity.this,SubActivity.class);
startActivity(in);
in.putExtra("id",arg[position]);
}
@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;
} }
 
Hi Amalthomas,

As the forum description notes, this isn't the place for support questions. As you've confirmed that it concerns Android development I'll move it to the Application Development forum, where it has more chance of receiving attention from fellow-developers.
 
Context is null, so I suspect you are getting a null pointer exception here "context.getPackageName()". An activity is a context, so set "context=this", or just call "getPackageName()" directly.
 
10 is not a context object. You cant assign an int to a pointer to a Context object. In onacreate, just set currentContext to this or this.getApplicationContext().
 
There are a lot of weird things in your code, for example:
Code:
private static final Context context = null;
Here you are defining a context which will always be uninitialized and can never become initialized (it is final and null). I don't think there is a good reason to ever do this.

And this:
Code:
MainActivity.this.openOrCreateDatabase("sample" ,MODE_PRIVATE,null);
is just a long way of saying
Code:
openOrCreateDatabase("sample" ,MODE_PRIVATE,null);

Allocating an array of 1000 strings also seems like a bad idea.

Have you worked through any Android/Java tutorials? I think you will find things much easier going if you work through a few basic sample apps and make sure you understand how they work.
 
Back
Top Bottom