Вопрос

What I am doing::

  • I am trying to run an sqlite query in an AsyncTask (since it executes the transaction in a seperate thread).
  • I am getting the log error as shown
  • When I debugged I found out an error is generated on getDataFromSqlite(); code

Questions::

  • Where I am going wrong
  • How can I correct myself

Code Snippet::

@Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        txtBufTime.setText(sendBufTime);
        txtBufDistance.setText(sendDistance);
        txtOnlinePrice.setText(sendOnlinePrice);
        txtReservePrice.setText(sendReservePrice);
        txtBuffetDesc.setText(sendDescription);
        txtBufType.setText(sendBufType);
        setRating(sendRating);
        new LongOperation().execute();
    }


    private class LongOperation extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            getDataFromSqlite();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
           Toast.makeText(getActivity(), "DOne", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }



        public void getDataFromSqlite() {
            ///
            Cursor cursor = null;
            DatabaseHandler mHelper = new DatabaseHandler(getActivity().getApplicationContext());
            SQLiteDatabase db = mHelper.getWritableDatabase();
            String queryString="select menu_type_id , cuisine_type_name , item_name" +
                               " from buffets " +
                               "where buff_off_id = " + sendBuffetId;
            Log.d("queryString", queryString.toString());


            try {
                cursor = db.rawQuery(queryString, null);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                cursor.close();
            }

        }

Log::

    **05-15 13:41:17.547: E/AndroidRuntime(2743): FATAL EXCEPTION: AsyncTask #5
05-15 13:41:17.547: E/AndroidRuntime(2743): java.lang.RuntimeException: An error occured while executing doInBackground()
05-15 13:41:17.547: E/AndroidRuntime(2743):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.lang.Thread.run(Thread.java:856)
05-15 13:41:17.547: E/AndroidRuntime(2743): Caused by: java.lang.NullPointerException
05-15 13:41:17.547: E/AndroidRuntime(2743):     at com.findmybuffet.fragments.FragBuffetContents.getDataFromSqlite(FragBuffetContents.java:154)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at com.findmybuffet.fragments.FragBuffetContents$LongOperation.doInBackground(FragBuffetContents.java:120)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at com.findmybuffet.fragments.FragBuffetContents$LongOperation.doInBackground(FragBuffetContents.java:1)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-15 13:41:17.547: E/AndroidRuntime(2743):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)**
Это было полезно?

Решение

From comments:

154 line is cursor.close();

Cursor cursor = null;

//...

try {
    cursor = db.rawQuery(queryString, null);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    cursor.close();
}

In case there was an exception, cursor is null and attempting to call a method on it results in NPE. Either move the cursor.close() in the try block, or surround it with if (cursor != null) condition.

Why rawQuery() throws is if there is an SQL syntax problem. To get help with that, post the exception for the exception you log in the catch block.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top