Search code examples
androidlistviewbuttonnullpointerexceptionexpandable

java.lang.NullPointerException : displaying records from database in an expandable listview


As the question dictates, i need to display records from database, different record for each child.ie, child 1= record 1 data. child 2 =record 2 data.so and so. i came up with the following code for display:

package com.example.moolah;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.Button;

public class DisplayActivity extends ListActivity {
DBAdapter db=new DBAdapter(this);


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_cursor); 

    Button viewbutton=(Button)findViewById(R.id.button2);
 //    viewbutton.setOnClickListener(new Buttonx());


}

public class Buttonx extends DisplayActivity implements Button.OnClickListener{
 @SuppressWarnings("deprecation")
  @Override
    public void onClick(View v)
    { 


Button viewbutton=(Button)v.findViewById(R.id.button2);
        Object viewid=viewbutton.getTag();
            int i=Integer.parseInt(viewid.toString());          
MyCustomAdapter c=new MyCustomAdapter();
            long id=c.getChildId(0,i);
        //DisplayCursor d;
            //d.filldata();                
         db.open();
           // View view;
           Cursor cursor = db.getRecord(id);
           startManagingCursor(cursor);

            String[] columns = new String[] {DBAdapter.invest_type,DBAdapter.curr_per_share_price, DBAdapter.share_name,
                    DBAdapter.no_of_shares,DBAdapter.share_identity,DBAdapter.purchase_price,
                    DBAdapter.purchase_from,DBAdapter.purchase_date,DBAdapter.purchase_contact};

            int[] to = new int[] { R.id.investmenttype,R.id.currpershareprice,R.id.sharename,R.id.shareno,R.id.shareid, R.id.purprice,
                    R.id.purfrom,R.id.purdate,R.id.purcon};
            SimpleCursorAdapter mAdapter = new SimpleCursorAdapter( this, R.layout.row, cursor, columns, to);
            this.setListAdapter(mAdapter);


           db.close();
    } 
}   

This displays "no data" in text, although my database has data, as confirmed using command prompt adb shell. So i assume it doesn't have access to the onClick method. Now if i uncomment the line commented in onCreate method,it gets access, but results in a NullPointerException at viewbutton.setOnClickListener(new Buttonx());//onCreate method

How do i get access to onClick without generating this exception and display the records as desired? i think i am going fundamentally wrong somewhere. if someone could point out the flaw i would be greatful.you see, this thing is driving me nuts.:/

LOGCAT:

     06-17 17:33:32.444: E/AndroidRuntime(569):
     FATAL EXCEPTION: main
     06-17 17:33:32.444: E/AndroidRuntime(569): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.example.moolah/com.example.moolah.DisplayActivity}: java.lang.NullPointerException
   06-17 17:33:32.444: E/AndroidRuntime(569):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at android.app.ActivityThread.access$600(ActivityThread.java:122)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at android.os.Handler.dispatchMessage(Handler.java:99)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at android.os.Looper.loop(Looper.java:137)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at android.app.ActivityThread.main(ActivityThread.java:4340)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at java.lang.reflect.Method.invokeNative(Native Method)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at java.lang.reflect.Method.invoke(Method.java:511)
   06-17 17:33:32.444: E/AndroidRuntime(569):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    06-17 17:33:32.444: E/AndroidRuntime(569):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    06-17 17:33:32.444: E/AndroidRuntime(569):  at dalvik.system.NativeStart.main(Native Method)        
    06-17 17:33:32.444: E/AndroidRuntime(569): Caused by: java.lang.NullPointerException
    06-17 17:33:32.444: E/AndroidRuntime(569):  at com.example.moolah.DisplayActivity.onCreate(DisplayActivity.java:21)
    06-17 17:33:32.444: E/AndroidRuntime(569):  at android.app.Activity.performCreate(Activity.java:4465)
    06-17 17:33:32.444: E/AndroidRuntime(569):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    06-17 17:33:32.444: E/AndroidRuntime(569):  at             android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
    06-17 17:33:32.444: E/AndroidRuntime(569):  ... 11 more

Solution

  • It should be like below. Also post the stack trace so that we can help you further

    public class DisplayActivity extends ListActivity {
    DBAdapter db;
    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_cursor); 
     db=new DBAdapter(this);  
    Button viewbutton=(Button)findViewById(R.id.button2);
    viewbutton.setOnClickListener(new OnClickListener()
    {
           @override
           public void onClick(View v)
           {
                 // do something
           }   
    
    });
    }
    

    Edit:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
      <ListView 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/button1"
    android:layout_width="fill_parent" 
    android:id="@+id/lv" />
    
      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:text="Button" />
    
    </RelativeLayout>
    

    MainActivity.java

    public class MainActivity extends Activity {
    
        ListView lv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button b = (Button) findViewById(R.id.button1);
            lv= (ListView) findViewById(R.id.lv);
            b.setOnClickListener(new OnClickListener()
            {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    // set your adapter to listview here
                }
    
            });
        }
    }