SQLiteDatabase.rawQuery(java.lang.String,java.lang.String [])'在空对象引用上

时间:2015-06-03 05:30:26

标签: java android sqlite android-sqlite

访问数据库时,会抛出NullPointerException

以下是创建db的代码:

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String KEY_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS {0} ({1})";
    public static final String KEY_DROP_TABLE = "DROP TABLE IF EXISTS {0}";

    private Context context;
    Activity C;
    private static final int CURRENT_DB_VERSION = 1;
    private static final String DB_NAME = "vrochat.db";

SQLiteDatabase Db1;
    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, CURRENT_DB_VERSION);
        this.context = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createUserTable(db);
        createMessageTable(db);
        createDialogTable(db);
//      // //Raj
        createTimelineTable(db);
        createNewsFeedTable(db);
        createProfileTable(db);
        createProfilemediaListTable(db);
        createFriendinfoTable(db);
        //samson
        createMultiImagePostTable(db);


        //

        this.Db1=db;
    }
    private void createMultiImagePostTable(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        StringBuilder MultiImagePostTablefields = new StringBuilder();
        MultiImagePostTablefields.append(MultiImagePostTable.Cols.ID)
                .append(" INTEGER PRIMARY KEY, ")
                .append(MultiImagePostTable.Cols.IMAGE).append(" TEXT, ")
                .append(MultiImagePostTable.Cols.IMAGE_CREATED_AT).append(" TEXT, ")
                .append(MultiImagePostTable.Cols.IMAGE_THUMB).append(" TEXT, ")
                .append(MultiImagePostTable.Cols.POST_ID).append(" TEXT ");
        createTable(db, MultiImagePostTable.TABLE_NAME,
                MultiImagePostTablefields.toString());

    }
    private void createUserTable(SQLiteDatabase db) {
        StringBuilder userTableFields = new StringBuilder();
        userTableFields.append(UserTable.Cols.ID)
                .append(" INTEGER PRIMARY KEY AUTOINCREMENT, ")
                .append(UserTable.Cols.USER_ID).append(" INTEGER, ")
                .append(UserTable.Cols.FULL_NAME).append(" TEXT, ")
                .append(UserTable.Cols.EMAIL).append(" TEXT, ")
                .append(UserTable.Cols.LOGIN).append(" TEXT, ")
                .append(UserTable.Cols.PHONE)
                .append(" TEXT, ")
                .append(UserTable.Cols.WEB_SITE)
                .append(" TEXT, ")
                .append(UserTable.Cols.CUSTOM_DATA)
                .append(" TEXT, ")
                .append(UserTable.Cols.LAST_REQUEST_AT)
                .append(" TEXT, ")
                .append(UserTable.Cols.EXTERNAL_ID)
                .append(" TEXT, ")
                        // .append(UserTable.Cols.FACEBOOK_ID).append(" INTEGER, ")
                        // .append(UserTable.Cols.TWITTER_ID).append(" INTEGER, ")
                .append(UserTable.Cols.BLOB_ID).append(" INTEGER, ")
                .append(UserTable.Cols.AVATAR_URL).append(" TEXT, ")
                .append(UserTable.Cols.STATUS).append(" TEXT, ")
                .append(UserTable.Cols.IS_ONLINE).append(" INTEGER");
        createTable(db, UserTable.TABLE_NAME, userTableFields.toString());
    }

    private void createMessageTable(SQLiteDatabase db) {
        StringBuilder messageTableFields = new StringBuilder();
        messageTableFields.append(MessageTable.Cols.ID)
                .append(" INTEGER PRIMARY KEY, ")
                .append(MessageTable.Cols.MESSAGE_ID).append(" TEXT UNIQUE, ")
                .append(MessageTable.Cols.DIALOG_ID).append(" TEXT, ")
                .append(MessageTable.Cols.SENDER_ID).append(" INTEGER, ")
                .append(MessageTable.Cols.BODY).append(" TEXT, ")
                .append(MessageTable.Cols.TIME).append(" LONG, ")
                .append(MessageTable.Cols.ATTACH_FILE_ID).append(" TEXT, ")
                .append(MessageTable.Cols.ATTACH_TYPE).append(" TEXT, ")
                .append(MessageTable.Cols.IS_READ).append(" INTEGER, ")
                .append(MessageTable.Cols.IS_DELIVERED).append(" INTEGER, ")
                .append(MessageTable.Cols.IS_SYNC).append(" INTEGER, ")
                .append(MessageTable.Cols.FRIENDS_NOTIFICATION_TYPE)
                .append(" INTEGER");
        createTable(db, MessageTable.TABLE_NAME, messageTableFields.toString());
    }

    private void createDialogTable(SQLiteDatabase db) {
        StringBuilder dialogTableFields = new StringBuilder();
        dialogTableFields.append(DialogTable.Cols.ID)
                .append(" INTEGER PRIMARY KEY AUTOINCREMENT, ")
                .append(DialogTable.Cols.DIALOG_ID).append(" TEXT, ")
                .append(DialogTable.Cols.ROOM_JID_ID).append(" TEXT, ")
                .append(DialogTable.Cols.NAME).append(" TEXT, ")
                .append(DialogTable.Cols.COUNT_UNREAD_MESSAGES)
                .append(" INTEGER, ").append(DialogTable.Cols.LAST_MESSAGE)
                .append(" TEXT, ")
                .append(DialogTable.Cols.LAST_MESSAGE_USER_ID)
                .append(" LONG, ").append(DialogTable.Cols.LAST_DATE_SENT)
                .append(" LONG, ").append(DialogTable.Cols.OCCUPANTS_IDS)
                .append(" TEXT, ").append(DialogTable.Cols.PHOTO_URL)
                .append(" TEXT, ").append(DialogTable.Cols.VROId)
                .append(" TEXT, ").append(DialogTable.Cols.TYPE)
                .append(" INTEGER");
        createTable(db, DialogTable.TABLE_NAME, dialogTableFields.toString());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        dropTable(db, UserTable.TABLE_NAME);
        dropTable(db, MessageTable.TABLE_NAME);
        dropTable(db, DialogTable.TABLE_NAME);
        onCreate(db);
    }

    public void dropTable(SQLiteDatabase db, String name) {
        String query = MessageFormat
                .format(DatabaseHelper.KEY_DROP_TABLE, name);
        db.execSQL(query);
    }

    public void createTable(SQLiteDatabase db, String name, String fields) {
        String query = MessageFormat.format(DatabaseHelper.KEY_CREATE_TABLE,
                name, fields);
        db.execSQL(query);
    }

    // ///////////////Raj
    private void createProfilemediaListTable(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        StringBuilder ProfileMediaListTablefields = new StringBuilder();
        ProfileMediaListTablefields.append(ProfileMedialistTable.Cols.ID)
                .append(" INTEGER PRIMARY KEY AUTOINCREMENT, ")
                .append(ProfileMedialistTable.Cols.USERID).append(" INTEGER, ")
                .append(ProfileMedialistTable.Cols.IMAGETHUMB)
                .append(" TEXT, ").append(ProfileMedialistTable.Cols.IMAGEURL)
                .append(" TEXT, ").append(ProfileMedialistTable.Cols.TYPE)
                .append(" TEXT ");
        createTable(db, ProfileMedialistTable.TABLE_NAME,
                ProfileMediaListTablefields.toString());

    }

    private void createProfileTable(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        StringBuilder ProfileTableFields = new StringBuilder();
        ProfileTableFields.append(ProfileTable.Cols.ABOUT).append(" TEXT, ")
                .append(ProfileTable.Cols.CHANNELCOUNT).append(" TEXT, ")
                .append(ProfileTable.Cols.CITY).append(" TEXT, ")
                .append(ProfileTable.Cols.COLLEGEAT).append(" TEXT, ")
                .append(ProfileTable.Cols.COUNTRY)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.DOB)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.EMAIL)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.FIRSTNAME)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.FRIENDSCOUNT)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.FRIENDSHIPSTATUS)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.GENDER)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.HOBBY)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.IMAGECOUNT)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.INTERESTS)
                .append(" TEXT, ")
                .append(ProfileTable.Cols.LASTNAME)
                .append(" TEXT, ")
                        // .append(ProfileTable.Cols.MEDIALIST).append(" TEXT, ")
                .append(ProfileTable.Cols.MEMBERSINCE).append(" TEXT, ")
                .append(ProfileTable.Cols.PRIVACY).append(" TEXT, ")
                .append(ProfileTable.Cols.PROFILEPICURL).append(" TEXT, ")
                .append(ProfileTable.Cols.QBID).append(" TEXT, ")
                .append(ProfileTable.Cols.RELATIONSHIPSTATUS).append(" TEXT, ")
                .append(ProfileTable.Cols.SCHOOLAT).append(" TEXT, ")
                .append(ProfileTable.Cols.STATE).append(" TEXT, ")
                .append(ProfileTable.Cols.TIMELINEPIC).append(" TEXT, ")
                .append(ProfileTable.Cols.USERID)
                .append(" INTEGER PRIMARY KEY, ")
                .append(ProfileTable.Cols.USERNAME).append(" TEXT, ")
                .append(ProfileTable.Cols.VIDEOCOUNT).append(" TEXT, ")
                .append(ProfileTable.Cols.WORKSAT).append(" TEXT ");
        createTable(db, ProfileTable.TABLE_NAME, ProfileTableFields.toString());

    }

    private void createNewsFeedTable(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        StringBuilder NewsFeedTableFields = new StringBuilder();
        NewsFeedTableFields.append(TimelineTable.Cols.ID).append(" INTEGER, ")
                .append(NewsFeedTable.Cols.VROID).append(" INTEGER, ")
                .append(NewsFeedTable.Cols.FIRST_NAME).append(" TEXT, ")
                .append(NewsFeedTable.Cols.LAST_NAME).append(" TEXT, ")
                .append(NewsFeedTable.Cols.POST_TITLE).append(" TEXT, ")
                .append(NewsFeedTable.Cols.POST_CONTENT).append(" TEXT, ")
                .append(NewsFeedTable.Cols.POST_TYPE).append(" TEXT, ")
                .append(NewsFeedTable.Cols.POST_IMAGEURL).append(" TEXT, ")
                .append(NewsFeedTable.Cols.POST_IMAGEURL_THUMB)
                .append(" TEXT, ").append(NewsFeedTable.Cols.POST_VIDEOURL)
                .append(" TEXT, ").append(NewsFeedTable.Cols.POST_CREATED)
                .append(" TEXT, ").append(NewsFeedTable.Cols.POST_UPDATED)
                .append(" TEXT, ").append(NewsFeedTable.Cols.LIKE_COUNT)
                .append(" INTEGER, ").append(NewsFeedTable.Cols.COMMENT_COUNT)
                .append(" INTEGER, ").append(NewsFeedTable.Cols.SHARE_COUNT)
                .append(" INTEGER, ").append(NewsFeedTable.Cols.IS_MY_POST)
                .append(" TEXT, ").append(NewsFeedTable.Cols.IS_SHARED)
                .append(" TEXT, ").append(NewsFeedTable.Cols.IS_LIKED)
                .append(" TEXT, ")
                .append(NewsFeedTable.Cols.FROM_USER_FIRSTNAME)
                .append(" TEXT, ").append(NewsFeedTable.Cols.FROM_USER_ID)
                .append(" TEXT, ")
                .append(NewsFeedTable.Cols.FROM_USER_LAST_NAME)
                .append(" TEXT, ")
                .append(NewsFeedTable.Cols.FROM_USER_PROFILE_PIC)
                .append(" TEXT, ")
                .append(NewsFeedTable.Cols.TO_USER_FIRST_NAME)
                .append(" TEXT, ").append(NewsFeedTable.Cols.TO_USER_LAST_NAME)
                .append(" TEXT, ")
                .append(NewsFeedTable.Cols.TO_USER_PROFILE_PIC)
                .append(" TEXT, ").append(NewsFeedTable.Cols.USERS_POSTID)
                .append(" INTEGER PRIMARY KEY, ")
                .append(NewsFeedTable.Cols.YOUTUBE_VIDEO_URL).append(" TEXT, ")
                .append(NewsFeedTable.Cols.POST__VIDEO_URL_THUMB)
                .append(" TEXT");
        createTable(db, NewsFeedTable.TABLE_NAME,
                NewsFeedTableFields.toString());

    }

    private void createTimelineTable(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        StringBuilder timeLineTableFields = new StringBuilder();
        timeLineTableFields.append(TimelineTable.Cols.ID).append(" INTEGER, ")
                .append(TimelineTable.Cols.VROID).append(" INTEGER, ")
                .append(TimelineTable.Cols.FIRST_NAME).append(" TEXT, ")
                .append(TimelineTable.Cols.LAST_NAME).append(" TEXT, ")
                .append(TimelineTable.Cols.POST_TITLE).append(" TEXT, ")
                .append(TimelineTable.Cols.POST_CONTENT).append(" TEXT, ")
                .append(TimelineTable.Cols.POST_TYPE).append(" TEXT, ")
                .append(TimelineTable.Cols.POST_IMAGEURL).append(" TEXT, ")
                .append(TimelineTable.Cols.POST_IMAGEURL_THUMB)
                .append(" TEXT, ").append(TimelineTable.Cols.POST_VIDEOURL)
                .append(" TEXT, ").append(TimelineTable.Cols.POST_CREATED)
                .append(" TEXT, ").append(TimelineTable.Cols.POST_UPDATED)
                .append(" TEXT, ").append(TimelineTable.Cols.LIKE_COUNT)
                .append(" INTEGER, ").append(TimelineTable.Cols.COMMENT_COUNT)
                .append(" INTEGER, ").append(TimelineTable.Cols.SHARE_COUNT)
                .append(" INTEGER, ").append(TimelineTable.Cols.IS_MY_POST)
                .append(" TEXT, ").append(TimelineTable.Cols.IS_SHARED)
                .append(" TEXT, ").append(TimelineTable.Cols.IS_LIKED)
                .append(" TEXT, ")
                .append(TimelineTable.Cols.FROM_USER_FIRSTNAME)
                .append(" TEXT, ").append(TimelineTable.Cols.FROM_USER_ID)
                .append(" TEXT, ")
                .append(TimelineTable.Cols.FROM_USER_LAST_NAME)
                .append(" TEXT, ")
                .append(TimelineTable.Cols.FROM_USER_PROFILE_PIC)
                .append(" TEXT, ")
                .append(TimelineTable.Cols.TO_USER_FIRST_NAME)
                .append(" TEXT, ").append(TimelineTable.Cols.TO_USER_LAST_NAME)
                .append(" TEXT, ")
                .append(TimelineTable.Cols.TO_USER_PROFILE_PIC)
                .append(" TEXT, ").append(TimelineTable.Cols.USERS_POSTID)
                .append(" INTEGER PRIMARY KEY, ")
                .append(TimelineTable.Cols.YOUTUBE_VIDEO_URL).append(" TEXT, ")
                .append(TimelineTable.Cols.POST__VIDEO_URL_THUMB)
                .append(" TEXT");
        createTable(db, TimelineTable.TABLE_NAME,
                timeLineTableFields.toString());

    }

    private void createFriendinfoTable(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        StringBuilder FriendInfoTablefields = new StringBuilder();
        FriendInfoTablefields.append(FriendinfoTable.Cols.ID)
                .append(" INTEGER PRIMARY KEY, ")
                .append(FriendinfoTable.Cols.FIRST_NAME).append(" TEXT, ")
                .append(FriendinfoTable.Cols.LAST_NAME).append(" TEXT, ")
                .append(FriendinfoTable.Cols.USER_NAME).append(" TEXT, ")
                .append(FriendinfoTable.Cols.EMAIL).append(" TEXT, ")
                .append(FriendinfoTable.Cols.USER_PROFILE_PIC)
                .append(" TEXT, ")
                .append(FriendinfoTable.Cols.USER_TIMELINE_PIC)
                .append(" TEXT, ").append(FriendinfoTable.Cols.COUNTRY)
                .append(" TEXT, ").append(FriendinfoTable.Cols.STATE)
                .append(" TEXT, ").append(FriendinfoTable.Cols.CITY)
                .append(" TEXT, ").append(FriendinfoTable.Cols.ABOUT)
                .append(" TEXT, ").append(FriendinfoTable.Cols.GENDER)
                .append(" TEXT, ").append(FriendinfoTable.Cols.DATEOFBIRTH)
                .append(" TEXT, ")
                .append(FriendinfoTable.Cols.FRIENDSHIP_STATUS)
                .append(" TEXT, ").append(FriendinfoTable.Cols.QUICKBLOX_ID)
                .append(" TEXT ");
        createTable(db, FriendinfoTable.TABLE_NAME,
                FriendInfoTablefields.toString());

    }

    //aswathy

public void getfriendsdata()
{

    Cursor c=Db1.rawQuery("SELECT * FROM friendinfotable",null);

    try
    {
        //put cursor on the first position
        c.moveToFirst();

        //fetching all  records from cursor until reaching last record
        while(!c.isAfterLast())
        {
                /*
                 * Display record on the screen
                 * Note : here in the cursor there are two columns.
                 * So be careful while fetching record.
                 * If no column is there, it will give you exception
                 */
            Toast.makeText(context,c.getString(0)+ " "+c.getString(1),Toast.LENGTH_SHORT).show();

            //moving cursor to next record
            c.moveToNext();
        }
        //closing cursor
        c.close();
    }catch(Exception e)
    {
        Log.d("Error", "error in cursor");
        e.printStackTrace();
    }

}

这就是它的调用方式:

public class FindPeopleFragment extends BaseFragment{
....
  private void loadMoreData() {
DatabaseHelper dh=new DatabaseHelper(getActivity());


                dh.getfriendsdata();
}
}

以下是显示异常的logcat输出:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference at below line
Cursor c=Db1.rawQuery("SELECT * FROM friendinfotable",null);

2 个答案:

答案 0 :(得分:4)

在运行onCreate之前,似乎未调用getfriendsdata();。因为Db1仅在onCreate()中初始化。

您必须致电Cursor c= getReadableDatabase().rawQuery("SELECT * FROM friendinfotable",null);

答案 1 :(得分:1)

  在创建数据库时,在only once中调用

onCreate()。

 Cursor c=Db1.rawQuery("SELECT * FROM friendinfotable",null);

因此,当应用程序第二次打开时,您的数据库Db1实例将为null。

所以你需要使用数据库参考。

SQLiteDatabase Db1=getReadableDatabase();

然后致电。

Cursor c=Db1.rawQuery("SELECT * FROM friendinfotable",null);