本文介绍了在一个空对象引用android.content.Context.getContentResolver()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎没有工作可为什么我得到这个空指针?

这是我的AsyncTask,我打电话来获取数据。它传递到JSON解析器和对象数组返回。这将传递给我的DBHelper它被传递到我的数据库通过ContentResolver的......

 公共类getFilms延伸的AsyncTask<字符串,太虚,太虚> {

公众诠释LIMIT_FILMS = 10;
字符串键=apikey;
字符串LIMIT =限制;
私有静态最后字符串的URL =htt​​p://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?;
私有静态最后弦乐API_KEY =******************;
ArrayList的< HashMap的<字符串,字符串>> filmArrayList =新的ArrayList< HashMap的<字符串,字符串>>();
语境mContext;

@覆盖
保护无效doInBackground(字符串... PARAMS){

    乌里RottenUrl = Uri.parse(URL).buildUpon()
            .appendQueryParameter(KEY,API_KEY)
            .appendQueryParameter(LIMIT,Integer.toString(LIMIT_FILMS))
            。建立();

    JSONParser jParser =新JSONParser();
    电影[] JSON = jParser.getJSONFromUrl(RottenUrl.toString());
    sortData(JSON);
    返回null;
}

公共无效sortData(电影[] jsonlist){
    DatabaseHelper dbHelper =新DatabaseHelper(mContext,NULL,NULL,1);
    dbHelper.deleteAll();
    的for(int i = 0; I< jsonlist.length;我++){
        dbHelper.contentAddFilm(jsonlist [I]);
    }
}
}
 

这是我的数据库助手

 公共类DatabaseHelper扩展SQLiteOpenHelper {

私人ContentResolver的myCR;

公共DatabaseHelper(上下文的背景下,字符串名称,
                      SQLiteDatabase.CursorFactory工厂,INT版){
    超(背景下,FilmDataContract.DATABASE_NAME,工厂,FilmDataContract.DATABASE_VERSION);
    myCR = context.getContentResolver();

}

@覆盖
公共无效的onCreate(SQLiteDatabase DB){
    db.execSQL(FilmDataContract.FilmEntry.SQL_CREATE_TABLE);
}

@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
    db.execSQL(FilmDataContract.FilmEntry.DELETE_TABLE);
    的onCreate(DB);
}

公共无效addFilm(电影胶片){

    ContentValues​​值=新ContentValues​​();
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE,film.getTitle());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING,film.getRating());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME,film.getRuntime());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS,film.getCritics());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE,film.getAudience());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS,film.getSynopsis());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE,film.getProfile());

    SQLiteDatabase DB = this.getWritableDatabase();
    db.insert(FilmDataContract.TABLE_NAME,
            空值,
            值);
    db.close();
}


大众电影getFilm(INT ID){

    SQLiteDatabase DB = this.getReadableDatabase();

    光标光标=
            db.query(FilmDataContract.TABLE_NAME,
                    FilmDataContract.FilmEntry.COLUMNS,
                    _id =?,
                    新的String [] {将String.valueOf(ID)},
                    空值,
                    空值,
                    空值,
                    空值);

    如果(光标!= NULL)
        cursor.moveToFirst();

    电影电影=新电影();
    film.setTitle(cursor.getString(1));
    film.setRating(cursor.getString(2));
    film.setRuntime(cursor.getString(3));
    film.setCritics(c​​ursor.getString(4));
    film.setAudience(cursor.getString(5));
    film.setSynopsis(cursor.getString(6));
    film.setProfile(cursor.getString(7));

    返回膜;
}

公开名单<薄膜> getAllFilms(){
    名单<薄膜>电影=新的LinkedList<薄膜>();

    查询字符串=SELECT * FROM+ FilmDataContract.TABLE_NAME;

    SQLiteDatabase DB = this.getWritableDatabase();
    光标光标= db.rawQuery(查询,NULL);

    电影电影= NULL;
    如果(cursor.moveToFirst()){
        做 {
            电影=新电影();
            film.setId(的Integer.parseInt(cursor.getString(0)));
            film.setTitle(cursor.getString(1));
            film.setRating(cursor.getString(2));
            film.setRuntime(cursor.getString(3));
            film.setCritics(c​​ursor.getString(4));
            film.setAudience(cursor.getString(5));
            film.setSynopsis(cursor.getString(6));
            film.setProfile(cursor.getString(7));

            films.add(膜);
        }而(cursor.moveToNext());
    }

    返回片;
}

公众诠释updateFilm(电影胶片){

    SQLiteDatabase DB = this.getWritableDatabase();

    ContentValues​​值=新ContentValues​​();
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE,film.getTitle());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING,film.getRating());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME,film.getRuntime());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS,film.getCritics());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE,film.getAudience());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS,film.getSynopsis());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE,film.getProfile());

    INT I = db.update(FilmDataContract.FilmEntry.TABLE_NAME,
            值,
            _id + =?,
            新的String [] {将String.valueOf(film.getId())});

    db.close();

    返回我;
}

公众诠释getFilmsCount(){
    字符串countQuery =SELECT * FROM+ FilmDataContract.FilmEntry.TABLE_NAME;
    SQLiteDatabase DB = this.getReadableDatabase();
    光标光标= db.rawQuery(countQuery,NULL);
    INT CNT = cursor.getCount();
    cursor.close();
    返回CNT;
}

公共无效用deleteAll(){
    SQLiteDatabase DB = this.getWritableDatabase();
    db.delete(FilmDataContract.FilmEntry.TABLE_NAME,NULL,NULL);
}

公共布尔contentDelete(字符串filmName){

    布尔结果= FALSE;
    串选择=TITLE = \+ filmName +\;

    INT rowsDeleted = myCR.delete(FilmProvider.CONTENT_URI,
            选择,NULL);

    如果(rowsDeleted大于0)
        结果=真;

    返回结果;
}

大众电影contentFindFilm(字符串filmName){
    的String []投影= FilmDataContract.FilmEntry.COLUMNS;

    串选择=TITLE = \+ filmName +\;

    光标光标= myCR.query(FilmProvider.CONTENT_URI,
            投影,选择,空,
            空值);

    电影电影=新电影();

    如果(cursor.moveToFirst()){
        cursor.moveToFirst();

        film.setId(的Integer.parseInt(cursor.getString(0)));
        film.setTitle(cursor.getString(1));
        film.setRating(cursor.getString(2));
        film.setRuntime(cursor.getString(3));
        film.setCritics(c​​ursor.getString(4));
        film.setAudience(cursor.getString(5));
        film.setSynopsis(cursor.getString(6));
        film.setProfile(cursor.getString(7));

        cursor.close();
    } 其他 {
        电影= NULL;
    }
    返回膜;
}

公共无效contentAddFilm(电影胶片){

    ContentValues​​值=新ContentValues​​();
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE,film.getTitle());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING,film.getRating());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME,film.getRuntime());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS,film.getCritics());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE,film.getAudience());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS,film.getSynopsis());
    values​​.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE,film.getProfile());

    myCR.insert(FilmProvider.CONTENT_URI,价值观);
}
 

这是我的堆栈跟踪......似乎当我路过的背景下发生的。

 产生的原因:显示java.lang.NullPointerException:尝试在空对象引用调用虚拟方法android.content.ContentResolver android.content.Context.getContentResolver()
        在com.purewowstudio.topmovies.data.DatabaseHelper< INIT>(DatabaseHelper.java:25)
        在com.purewowstudio.topmovies.util.getFilms.sortData(getFilms.java:48)
        在com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:43)
        在com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:16)
        在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
        在java.util.concurrent.FutureTask.run(FutureTask.java:237)
 

在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:231)

解决方案

  DatabaseHelper dbHelper =新DatabaseHelper(mContext,NULL,NULL,1);
 

mContext ,因为你永远不分配一个值给它。

I can't seem work out why I am getting a null pointer on this?

This is my AsyncTask that I call to grab the data. It passes it to a JSON Parser and an array of Objects is returned. This is then passed to my DBHelper where it was passing to my database through a ContentResolver....

public class getFilms extends AsyncTask<String, Void, Void> {

public int LIMIT_FILMS = 10;
String KEY = "apikey";
String LIMIT = "limit";
private static final String URL = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?";
private static final String API_KEY = "******************";
ArrayList<HashMap<String, String>> filmArrayList = new ArrayList<HashMap<String, String>>();
Context mContext;

@Override
protected Void doInBackground(String... params) {

    Uri RottenUrl = Uri.parse(URL).buildUpon()
            .appendQueryParameter(KEY, API_KEY)
            .appendQueryParameter(LIMIT, Integer.toString(LIMIT_FILMS))
            .build();

    JSONParser jParser = new JSONParser();
    Film[] json = jParser.getJSONFromUrl(RottenUrl.toString());
    sortData(json);
    return null;
}

public void sortData(Film[] jsonlist) {
    DatabaseHelper dbHelper = new DatabaseHelper(mContext, null, null, 1);
    dbHelper.deleteAll();
    for (int i = 0; i < jsonlist.length; i++) {
        dbHelper.contentAddFilm(jsonlist[i]);
    }
}
}

This is my Database Helper

public class DatabaseHelper extends SQLiteOpenHelper {

private ContentResolver myCR;

public DatabaseHelper(Context context, String name,
                      SQLiteDatabase.CursorFactory factory, int version) {
    super(context, FilmDataContract.DATABASE_NAME, factory, FilmDataContract.DATABASE_VERSION);
    myCR = context.getContentResolver();

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(FilmDataContract.FilmEntry.SQL_CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(FilmDataContract.FilmEntry.DELETE_TABLE);
    onCreate(db);
}

public void addFilm(Film film) {

    ContentValues values = new ContentValues();
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());

    SQLiteDatabase db = this.getWritableDatabase();
    db.insert(FilmDataContract.TABLE_NAME,
            null,
            values);
    db.close();
}


public Film getFilm(int id) {

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor =
            db.query(FilmDataContract.TABLE_NAME,
                    FilmDataContract.FilmEntry.COLUMNS,
                    "_id = ?",
                    new String[]{String.valueOf(id)},
                    null,
                    null,
                    null,
                    null);

    if (cursor != null)
        cursor.moveToFirst();

    Film film = new Film();
    film.setTitle(cursor.getString(1));
    film.setRating(cursor.getString(2));
    film.setRuntime(cursor.getString(3));
    film.setCritics(cursor.getString(4));
    film.setAudience(cursor.getString(5));
    film.setSynopsis(cursor.getString(6));
    film.setProfile(cursor.getString(7));

    return film;
}

public List<Film> getAllFilms() {
    List<Film> films = new LinkedList<Film>();

    String query = "SELECT  * FROM " + FilmDataContract.TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    Film film = null;
    if (cursor.moveToFirst()) {
        do {
            film = new Film();
            film.setId(Integer.parseInt(cursor.getString(0)));
            film.setTitle(cursor.getString(1));
            film.setRating(cursor.getString(2));
            film.setRuntime(cursor.getString(3));
            film.setCritics(cursor.getString(4));
            film.setAudience(cursor.getString(5));
            film.setSynopsis(cursor.getString(6));
            film.setProfile(cursor.getString(7));

            films.add(film);
        } while (cursor.moveToNext());
    }

    return films;
}

public int updateFilm(Film film) {

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());

    int i = db.update(FilmDataContract.FilmEntry.TABLE_NAME,
            values,
            "_id+ = ?",
            new String[]{String.valueOf(film.getId())});

    db.close();

    return i;
}

public int getFilmsCount() {
    String countQuery = "SELECT  * FROM " + FilmDataContract.FilmEntry.TABLE_NAME;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int cnt = cursor.getCount();
    cursor.close();
    return cnt;
}

public void deleteAll() {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(FilmDataContract.FilmEntry.TABLE_NAME, null, null);
}

public boolean contentDelete(String filmName) {

    boolean result = false;
    String selection = "title = \"" + filmName + "\"";

    int rowsDeleted = myCR.delete(FilmProvider.CONTENT_URI,
            selection, null);

    if (rowsDeleted > 0)
        result = true;

    return result;
}

public Film contentFindFilm(String filmName) {
    String[] projection = FilmDataContract.FilmEntry.COLUMNS;

    String selection = "title = \"" + filmName + "\"";

    Cursor cursor = myCR.query(FilmProvider.CONTENT_URI,
            projection, selection, null,
            null);

    Film film = new Film();

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();

        film.setId(Integer.parseInt(cursor.getString(0)));
        film.setTitle(cursor.getString(1));
        film.setRating(cursor.getString(2));
        film.setRuntime(cursor.getString(3));
        film.setCritics(cursor.getString(4));
        film.setAudience(cursor.getString(5));
        film.setSynopsis(cursor.getString(6));
        film.setProfile(cursor.getString(7));

        cursor.close();
    } else {
        film = null;
    }
    return film;
}

public void contentAddFilm(Film film) {

    ContentValues values = new ContentValues();
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
    values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());

    myCR.insert(FilmProvider.CONTENT_URI, values);
}

This is my stack trace... Seems to be happening when I am passing the context.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
        at com.purewowstudio.topmovies.data.DatabaseHelper.<init>(DatabaseHelper.java:25)
        at com.purewowstudio.topmovies.util.getFilms.sortData(getFilms.java:48)
        at com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:43)
        at com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:16)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)            

解决方案
DatabaseHelper dbHelper = new DatabaseHelper(mContext, null, null, 1);

mContext is null, because you never assign a value to it.

这篇关于在一个空对象引用android.content.Context.getContentResolver()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:01