方法getallcontacts(sqlitedatabase)出错

时间:2015-05-17 20:23:09

标签: android

它之前正在工作,现在它在方法getAllContacts()上抛出错误,存在于dbhelper.java中,即使我在它之前放置了openDataBase(),它也会抛出错误(第59行的错误)。怎么了?

这是dbhelper.java中方法的错误

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class dbhelper extends SQLiteOpenHelper
{
    private SQLiteDatabase database;
    public final static int db_version = 1;
    public final static String db_tab_name ="justtable";
    public final static String db_col_name="f_name";
    public final static String db_f_phone="f_phone";
    public final static String db_col_id ="_id";
    public final static String database_name="databasename";
    public final static String db_create="Create table " + db_tab_name +" ("+db_col_id +" interger  ," +db_col_name +" text,"+db_f_phone +" TEXT )";



    public dbhelper(Context context) {
        super(context, database_name, null, db_version);
    }


    public void openDataBase() {
        database = this.getWritableDatabase();
        database.execSQL("DROP TABLE "+ db_tab_name);
        database.execSQL(db_create);
        Log.d("l", db_create);
    }
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL("DROP TABLE IF NOT EXISTS "+ db_tab_name);
        database.execSQL(db_create);

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
        database.execSQL("DROP TABLE IF NOT EXISTS "+ db_tab_name);
        onCreate( database);
    }
       public boolean insertContact  (String name)
       {
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues contentValues = new ContentValues();

          contentValues.put( "name",name);

          db.insert("justtable", null, contentValues);
          db.close();
          return true;
       }
       public Cursor getAllContacts()
       {

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res1 =  db.rawQuery( "select * from justtable", null );
          res1.moveToFirst();
          while(res1.isAfterLast() == false){
              res1.moveToNext();

              } 
        //c1 = sqlDatabase.rawQuery(query, null);

       return res1;
      }

这是profile.java

private void showList() {

  ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
  contactList.clear();

 /* Cursor c1 =   db.query("justtable", //table name
           new String[] { "f_name" , "f_phone" },//column name
null, //selection
null, //selection
null, // group by
null, //having
null, //order by
null); //limit*/

  Cursor c1 = sqlHandler.getAllContacts();

  if (c1 != null && c1.getCount() != 0) {
   if (c1.moveToFirst()) {
    do {
     ContactListItems contactListItems = new ContactListItems();

     contactListItems.setSlno(c1.getString(c1
       .getColumnIndex("f_name")));
     contactListItems.setName(c1.getString(c1
       .getColumnIndex("f_phone")));
    // contactListItems.setPhone(c1.getString(c1
   //    .getColumnIndex("phone")));
     contactList.add(contactListItems);

    } while (c1.moveToNext());
   }
  }
  c1.close();

  ContactListAdapter contactListAdapter = new ContactListAdapter(
            Profile.this, contactList);
          lvCustomList.setAdapter(contactListAdapter);

 }

Logcat:

05-17 23:28:23.797: E/AndroidRuntime(16075): FATAL EXCEPTION: main
05-17 23:28:23.797: E/AndroidRuntime(16075): Process: com.example.sqlfirst, PID: 16075
05-17 23:28:23.797: E/AndroidRuntime(16075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlfirst/com.example.sqlfirst.Profile}: java.lang.NullPointerException
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.ActivityThread.access$800(ActivityThread.java:163)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.os.Looper.loop(Looper.java:157)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.ActivityThread.main(ActivityThread.java:5335)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at java.lang.reflect.Method.invokeNative(Native Method)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at java.lang.reflect.Method.invoke(Method.java:515)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at dalvik.system.NativeStart.main(Native Method)
05-17 23:28:23.797: E/AndroidRuntime(16075): Caused by: java.lang.NullPointerException
05-17 23:28:23.797: E/AndroidRuntime(16075):    at com.example.sqlfirst.Profile.showList(Profile.java:73)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at com.example.sqlfirst.Profile.onCreate(Profile.java:30)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.Activity.performCreate(Activity.java:5389)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-17 23:28:23.797: E/AndroidRuntime(16075):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
05-17 23:28:23.797: E/AndroidRuntime(16075):    ... 11 more

0 个答案:

没有答案