-3

whenever i include the updateUI() method my app crashes, but when i exclude it it won't crash, please explain me why?, and please explain better ways of updating my ListView using ArrayList and ArrayAdapter since i'm confused about using SimpleCursorAdapter, regards in advance...

my MainActivity

package org.example.tasklist;

 import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    HelperMethods localhelpermethods;
    HelperMethods.OpenHelper localopenhelper;
    SQLiteDatabase localdatabase;
    ArrayList<String> task_list;
    ListView listView1;
    EditText editText1;
    ArrayAdapter<String> aa;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Context activitycontext = getApplicationContext();

        localhelpermethods = new HelperMethods(activitycontext);

        task_list  = new ArrayList<String>();
        listView1 = (ListView)findViewById(R.id.listView1);
        editText1 = (EditText)findViewById(R.id.editText1);

        Button button1 = (Button)findViewById(R.id.button1);

        aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,task_list);
        listView1.setAdapter(aa);       
        updateUI();


        button1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {             
               String task;
               localhelpermethods.open_database();
               task = editText1.getText().toString();
               localhelpermethods.add_task(task);

               editText1.setText("");

               updateUI();

            }
         });//working fine, don't know whether the task is being stored in the database.
    }

    public void updateUI()
    {
        task_list = localhelpermethods.getAllTasks();
        aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,task_list);
        listView1 = (ListView)findViewById(R.id.listView1);
        listView1.setAdapter(aa);

        aa.notifyDataSetChanged();
    }




    @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;
    }

}

My HelperMethods.java

package org.example.tasklist;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class HelperMethods
{

public static final String DATABASE_NAME = "TASK_DATABASE";
public final String DATABASE_TABLE = "TASKS";
public static final int DATABASE_VERSION = 1;


//Columns in the table.
public final String KEY_ID = "_id";
public final String KEY_TASK = "task";

private static String CREATE_DATABASE = "create table TASKS (_id integer primary key autoincrement," + "task text not null);";



    private final Context localcontext;
    private OpenHelper localopenhelper;
    private SQLiteDatabase localdatabase;

    public HelperMethods(Context ctx)
    {
        this.localcontext = ctx;
        localopenhelper = new OpenHelper(localcontext);
    }//correct.

    //correct
     public class OpenHelper extends SQLiteOpenHelper
     {
         Context openhelpercontext;
          public OpenHelper(Context context)
          {
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
            this.openhelpercontext = context;
          }

          @Override
          public void onCreate(SQLiteDatabase database)
          {
              database.execSQL(CREATE_DATABASE);
          }

          @Override
          public void onUpgrade(SQLiteDatabase database, int oldversion, int newversion)
          {
             if(newversion > oldversion)
             {
                 database.execSQL("DROP TABLE IF EXISTS tasks" );
                 onCreate(database);
             }
          }//correct
       }

    public HelperMethods open_database() throws SQLException
    {
        localdatabase = localopenhelper.getWritableDatabase();
        return this;
    }//correct.

    public void close_database()
    {
        localopenhelper.close();
    }//correct.

    public long add_task(String task)
    {
        ContentValues cv = new ContentValues();
        cv.put(KEY_TASK, task);
        return localdatabase.insert("TASKS", null, cv);
    }//correct

    public ArrayList<String> getAllTasks()
    { 
        ArrayList<String> task_list = new ArrayList<String>();
        localopenhelper.getReadableDatabase();
        String[] columns = new String[] {KEY_TASK};
        Cursor cursor = localdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null, null);
        while(cursor.moveToNext())
        {
            task_list.add(cursor.getString(cursor.getColumnIndex(KEY_TASK)));
        }
        return task_list;
    }//correct but not verified.

    public void deleteAllTasks()
    {
        localdatabase.delete(DATABASE_TABLE, KEY_TASK, null);
    }

}

my error logcat

05-03 11:12:29.490: E/AndroidRuntime(2714): FATAL EXCEPTION: main
05-03 11:12:29.490: E/AndroidRuntime(2714): Process: org.example.tasklist, PID: 2714
05-03 11:12:29.490: E/AndroidRuntime(2714): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.tasklist/org.example.tasklist.MainActivity}: java.lang.NullPointerException
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.os.Looper.loop(Looper.java:137)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.main(ActivityThread.java:4998)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at java.lang.reflect.Method.invoke(Method.java:515)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at dalvik.system.NativeStart.main(Native Method)
05-03 11:12:29.490: E/AndroidRuntime(2714): Caused by: java.lang.NullPointerException
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.HelperMethods.getAllTasks(HelperMethods.java:88)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.MainActivity.updateUI(MainActivity.java:68)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.MainActivity.onCreate(MainActivity.java:45)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.Activity.performCreate(Activity.java:5243)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-03 11:12:29.490: E/AndroidRuntime(2714):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-03 11:12:29.490: E/AndroidRuntime(2714):     ... 11 more
Giridhar Karnik
  • 2,030
  • 4
  • 25
  • 42

1 Answers1

0

From your logcat...

05-03 11:12:29.490: E/AndroidRuntime(2714): Caused by: java.lang.NullPointerException
05-03 11:12:29.490: E/AndroidRuntime(2714):     at org.example.tasklist.HelperMethods.getAllTasks(HelperMethods.java:88)

You're getting a NullPointerException at line 88 of the HelperMethods class. Looking at your code, the most likely line that would be is...

while(cursor.moveToNext())

...which suggests your query is returning a null for the Cursor. Try checking the Cursor before trying to call moveToNext(). Example...

if (cursor != null)
    while (cursor.moveToNext())
    {
        task_list.add(cursor.getString(cursor.getColumnIndex(KEY_TASK)));
    }
Squonk
  • 48,331
  • 18
  • 101
  • 135
  • Thank-you Squonk, but the doubt is the same updateUI () method is working fine when called from within the onClick method, but is giving a null pointer exception when called from elsewhere, why is that? – Giridhar Karnik May 04 '14 at 05:06