I’m so stuck…Databases…

100 views Asked by At

I try to store the inserted data of my app to a database. So I chose SQLiteDatabase because that’s what I had seen in my Nanodegree. Now I am to the point where I want to tap on an item from the ListView and go the EditorActivity. When I code the CursorLoader in the EditorActivity.java to show the inserted data and to update them, the app crashes before it even starts…

The editor activity (here called MainActivity but here are the EditText fields):

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{

private static final int EXISTING_CONTAINER_LOADER = 0;

DexamenesCursorAdapter mCursorAdapter;
private Uri mCurrentContainerUri;

 (...)

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
// Since the editor shows all container attributes, define a projection that contains
// all columns from the container table
String[] projection = {
        DexameniEntry._ID,
        DexameniEntry.COLUMN_a,
        DexameniEntry.COLUMN_b,
        DexameniEntry.COLUMN_c,};


// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this,
        mCurrentContainerUri,
        projection,
        null,
        null,
        null);

}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data){

if (data.moveToFirst()) {
    if (data.moveToFirst()) {

        // Find the columns of pet attributes that we're interested in
        int aColumnIndex = data.getColumnIndex(DexameniEntry.COLUMN_a);
        int bColumnIndex = data.getColumnIndex(DexameniEntry.COLUMN_b);
        int cColumnIndex = data.getColumnIndex(DexameniEntry.COLUMN_c);


        // Extract out the value from the Cursor for the given column index
        int a = data.getInt(aColumnIndex);
        int b = data.getInt(bColumnIndex);
        int c = data.getInt(cColumnIndex);

        // Update the views on the screen with the values from the database
        mEditX1a.setText(Integer.toString(a));
        mEditX1b.setText(Integer.toString(b));
        mEditX1c.setText(Integer.toString(c));
    }

    mCursorAdapter.swapCursor(data);
}
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}

Here’s the logcat

2018-11-28 15:51:44.853 26449-26470/com.example.android.deyalcalculator 
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.android.deyalcalculator, PID: 26449
java.lang.RuntimeException: An error occurred while executing 
doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:760)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:111)
at android.content.ContentResolver.query(ContentResolver.java:515)
at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
at android.content.CursorLoader.loadInBackground(CursorLoader.java:56)
at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:760)
2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
 2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
 2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: 
 PATH3 /odm/lib64/hw/gralloc.msm8953.so
2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
 2018-11-28 15:51:44.891 26449-26449/com.example.android.deyalcalculator E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so

I don’t know how to solve this issue. I don’t even know if SQLiteDatabase is the best choice to be honest because I want the app to store data of a company’s measurements of water quality and produce graphs (like in a Stocks related app) over a long period of time (Graph of week’s measurement, month, year)…

Does anyone have any ideas about the solution of the spesific problem or in general a better way to store such kind of data?Please…(I’m tired and desperate from compiling lol)

1

There are 1 answers

2
Globe Admin On

It seems this field is mCurrentContainerUri is null value or not initialized .

if you see the source code for the class com.android.internal.util.Preconditions

you can see at line 111

/**
 * Ensures that an object reference passed as a parameter to the calling
 * method is not null.
 *
 * @param reference an object reference
 * @return the non-null reference that was validated
 * @throws NullPointerException if {@code reference} is null
 */
public static @NonNull <T> T checkNotNull(final T reference) {
    if (reference == null) {
        throw new NullPointerException();
    }
    return reference;
}

in your case uri is null and somewhere it the checkNotNull method is called , check if the uri is null , hence there by null pointer exception thrown.