Search code examples
javaandroidlistviewarraylistbaseadapter

Java BaseAdapter - ArrayList as Return Value - NullpointerException


i´m trying to display a ListView of the last ten Database Objects. If i display those Objects in Labels everything works just fine but now i created a ListView + DataAdapter but the App crashes with a NullPointerException. I googled a lot but the only solution i found was that the ArrayList was not initialized with = new ArrayList() or the name of the View does not match the one in the findViewById Method but none of them are wrong in my Project. Thanks for helping :)

MainActivity.java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); //res/layout/activity_main.xml Layout
    list = (ListView) findViewById(R.id.lastpushlist);
    list.setAdapter(new DataAdapter(this));

DataAdapter.java

Controller aController = new Controller();
Context context;
ArrayList<SingleRow> list = aController.getLastLogs(MainActivity.regIDglobal, "10");

DataAdapter(Context c){

    context = c;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.single_row, parent,false);
    TextView Message = (TextView) row.findViewById(R.id.textView1);
    TextView URL = (TextView) row.findViewById(R.id.textView2);

    SingleRow temp = list.get(position);
    Message.setText(temp.message);
    URL.setText(temp.url);
    return row;
}

Controller.java

ArrayList<SingleRow> list = new ArrayList<SingleRow>();
// Lots of Database Calls and get JSON Objects but for Testing i just create and add some Test Data into the list and return it.
list.add(new SingleRow("bla","bla2"));
return list;

activity_main.xml

 <ListView
    android:id="@+id/lastpushlist"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" >
</ListView>

Logcat:

02-24 12:20:23.468: D/AndroidRuntime(1475): Shutting down VM
02-24 12:20:23.472: D/dalvikvm(1475): GC_CONCURRENT freed 440K, 19% free 2720K/3336K, paused 4ms+0ms, total 16ms
02-24 12:20:23.476: W/dalvikvm(1475): threadid=1: thread exiting with uncaught exception (group=0xa615d908)
02-24 12:20:23.476: E/AndroidRuntime(1475): FATAL EXCEPTION: main
02-24 12:20:23.476: E/AndroidRuntime(1475): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.fhdortmund.pushprojekt/de.fhdortmund.pushprojekt.MainActivity}: java.lang.NullPointerException
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.os.Looper.loop(Looper.java:137)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at java.lang.reflect.Method.invoke(Method.java:511)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at dalvik.system.NativeStart.main(Native Method)
02-24 12:20:23.476: E/AndroidRuntime(1475): Caused by: java.lang.NullPointerException
02-24 12:20:23.476: E/AndroidRuntime(1475):     at de.fhdortmund.pushprojekt.DataAdapter.<init>(DataAdapter.java:16)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at de.fhdortmund.pushprojekt.MainActivity.onCreate(MainActivity.java:49)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.Activity.performCreate(Activity.java:5104)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-24 12:20:23.476: E/AndroidRuntime(1475):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-24 12:20:23.476: E/AndroidRuntime(1475):     ... 11 more

DataAdapter Line 16 is : ArrayList list = aController.getLastLogs(MainActivity.regIDglobal, "10"); MainActivity Line 49 is: list.setAdapter(new DataAdapter(this));


Solution

  • The only thing that could throw a NPE at line 16 is if aController is null. You didn't post the code where this object is created so I assume it simply wasn't.