为什么我在数据库调用上有空指针异常,我该怎么办才能解决?

时间:2016-05-18 12:16:56

标签: android nullpointerexception activeandroid

new AsyncTask<Ticket, Void, List<TPVLine>>() {
        @Override
        protected List<TPVLine> doInBackground(Ticket... params) {
            List<TPVLine> lines;
            while (true){
                Log.d(TAG, "Waiting for data base response");
                try {
                    lines = params[0].getLines();
                    Log.d(TAG, "Data base response completed");
                    break;
                }catch (SQLiteException | NullPointerException ex){
                    ActiveAndroid.clearCache();
                    Log.d(TAG, "Cleaning cache");
                    Log.wtf(TAG, ex.toString());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
            return lines;
        }

        @Override
        protected void onPostExecute(List<TPVLine> aVoid) {
            super.onPostExecute(aVoid);
            linesTPV = new ArrayList<TPVLine>();
            if (aVoid != null){
                linesTPV = aVoid;
            }
            linesTPV.addAll(noSavedLines);
            mainActivity.getTpvFragment().resetPrice();
            notifyDataSetChanged();
            if (linesTPV.size() == 0){
                mainActivity.getTpvFragment().getListContainer().setVisibility(View.INVISIBLE);
                mainActivity.getTpvFragment().getMessageContainer().setVisibility(View.VISIBLE);
            }else {
                mainActivity.getTpvFragment().getListContainer().setVisibility(View.VISIBLE);
                mainActivity.getTpvFragment().getMessageContainer().setVisibility(View.INVISIBLE);
            }
            notifyDataSetChanged();
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mainActivity.getCurrentTicket());

这是调用,首先在Ticket.java

public List<TPVLine> getLines() {
    return new Select().from(TPVLine.class).where("Ticket = ?", this.getId()).execute();
}

第二个是TPVLine.java

public static List<TPVLine> getLines(Ticket ticket){
    return new Select().from(TPVLine.class).where("Ticket = ?", ticket.getId()).orderBy("Id ASC").execute();
}

问题是在我调用TPVLine.class时引起的,我首先确定Ticket!= null。我使用ActiveAndroid管理数据库

1 个答案:

答案 0 :(得分:1)

您在asynctask null事件中返回lines而不是doInBackground

return lines;