-1

I'm having a bit of a problem understanding what's happening here, so any help would be very much appreciated. I'm kind of new to the Android development.

Those are the errors I'm receiving:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
              Process: com.example.benignfella.workorganizer, PID: 9956
              java.lang.RuntimeException: An error occured while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:300)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                  at java.lang.Thread.run(Thread.java:841)
               Caused by: java.lang.NullPointerException
                  at com.example.benignfella.workorganizer.Fragment.RecordsListFragment$GetRecordsTask.doInBackground(RecordsListFragment.java:87)
                  at com.example.benignfella.workorganizer.Fragment.RecordsListFragment$GetRecordsTask.doInBackground(RecordsListFragment.java:77)
                  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) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                  at java.lang.Thread.run(Thread.java:841) 

and here's the class which is apparently problematic (which begins at the line 77, as described by RecordsListFragment.java:77)

public class GetRecordsTask extends AsyncTask<Void, Void, ArrayList<Records>> {

    private final WeakReference<Activity> activityWeakRef;

    public GetRecordsTask(Activity context) {
        this.activityWeakRef = new WeakReference<Activity>(context);
    }

    @Override
    protected ArrayList<Records> doInBackground(Void... arg0) {
        ArrayList<Records> recordsArrayList = recordsDAO.getRecords();
        return recordsArrayList;
    }

    @Override
    protected void onPostExecute(ArrayList<Records> empList) {
        if (activityWeakRef.get() != null && !activityWeakRef.get().isFinishing()) {
            records = empList;
            if (empList != null) {
                if (empList.size() != 0) {
                    recordsListAdapter = new RecordsListAdapter(activity, empList);
                    recordsListView.setAdapter(recordsListAdapter);
                } else {
                    Toast.makeText(activity, "No Records about records... wait wot m8?",
                            Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

Error points at the doInBackground method, more specifically, this line:

ArrayList<Records> recordsArrayList = recordsDAO.getRecords();

I've read something about conveying block of code to onPostExecute method, but I'm not sure how. It'd be great if someone could help me out on that one.

Edit: The proposed thread did not solve my problem the reason why I'm posting a question is that I'm not able to solve the problem myself.

According to the thread, NPE can occur because I'm assuming that object was created before the method was called, but wasn't that what I did here: public class GetRecordsTask extends AsyncTask<Void, Void, ArrayList<Records>>?

I genuinely do not understand what I'm doing wrong, so I'm really asking for some concrete help.

Edit 2: here is the RecordsDAO class in its entirety

public class RecordsDAO extends RecordsDBDAO {

    public static final String RECORDS_ID_WITH_PREFIX = "records.id";

    private static final String WHERE_ID_EQUALS = DatabaseHelper.ID + " =?";

    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

    //Constructor
    public RecordsDAO(Context context) {
    super(context);
    }

    public long save(Records records) {
    //Use ContentValues to access database
    ContentValues values = new ContentValues();

    //Should I add the ID field as well?
    values.put(DatabaseHelper.DESCRIPTION, records.getDescription());
    values.put(DatabaseHelper.KEY_CREATED_AT, formatter.format(records.getDateofRecord()));
    values.put(DatabaseHelper.STARTINGHOUR, records.getStart());
    values.put(DatabaseHelper.FINISHINGHOUR, records.getFinish());
    values.put(DatabaseHelper.COMMENTS, records.getComments());

    return database.insert(DatabaseHelper.TABLE_RECORDS, null, values);
    }

    public long update(Records records) {
    //Use ContentValues to access database
    ContentValues values = new ContentValues();
    values.put(DatabaseHelper.DESCRIPTION, records.getDescription());
    values.put(DatabaseHelper.KEY_CREATED_AT, formatter.format(records.getDateofRecord()));
    values.put(DatabaseHelper.STARTINGHOUR, records.getStart());
    values.put(DatabaseHelper.FINISHINGHOUR, records.getFinish());
    values.put(DatabaseHelper.COMMENTS, records.getComments());

    long result = database.update(
            DatabaseHelper.TABLE_RECORDS,
            values,
            WHERE_ID_EQUALS,
            new String[] {
                    String.valueOf(records.getId())
            }
    );

    return result;
    }

    public int deleteRecord(Records records) {
    return database.delete(DatabaseHelper.TABLE_RECORDS,
            WHERE_ID_EQUALS,
            new String[] {
                    records.getId() + ""
            });
    }

    public ArrayList<Records> getRecords() {
    ArrayList<Records> records = new ArrayList<Records>();

    String query = "SELECT " + RECORDS_ID_WITH_PREFIX + ", "
            + DatabaseHelper.DESCRIPTION + " ,"
            + DatabaseHelper.KEY_CREATED_AT + ", "
            + DatabaseHelper.STARTINGHOUR + ", "
            + DatabaseHelper.FINISHINGHOUR + ", "
            + DatabaseHelper.COMMENTS + " FROM "
            + DatabaseHelper.TABLE_RECORDS + " rec,"
            + DatabaseHelper.TABLE_LOCATIONS + " loc,"
            + DatabaseHelper.TABLE_TAGS + " tg WHERE rec."
            + DatabaseHelper.LOCATIONS_ID + " = loc." + DatabaseHelper.ID + " AND "
            + DatabaseHelper.TAGS_ID + " = tg." + DatabaseHelper.ID;

    Cursor cursor = database.rawQuery(query, null);

    while (cursor.moveToNext()) {
        Records records1 = new Records();
        records1.setId(cursor.getInt(0));
        records1.setDescription(cursor.getString(1));

        try {
            records1.setDateofRecord(formatter.parse(cursor.getString(2)));
        } catch (ParseException e) {
            records1.setDateofRecord(null);
        }

        records1.setStart(cursor.getString(3));
        records1.setFinish(cursor.getString(4));
        records1.setComments(cursor.getString(5));

        Locations locations = new Locations();
        locations.setId(cursor.getInt(6));
        locations.setLocation(cursor.getString(7));

        Tags tags = new Tags();
        tags.setId(cursor.getInt(8));
        tags.setName(cursor.getString(9));

        records1.setLocations(locations);
        records1.setTags(tags);
        records.add(records1);
    }
    return records;
    }

    public void loadTags() {
    Tags tag = new Tags("Ljubljana, Faculty");
    Tags tag1 = new Tags("Kranj, OpenLab");
    Tags tag2 = new Tags("Ljubljana, Other");
    Tags tag3 = new Tags("Kranj, Other");

    List<Tags> tagsList = new ArrayList<Tags>();
    tagsList.add(tag);
    tagsList.add(tag1);
    tagsList.add(tag2);
    tagsList.add(tag3);

    for (Tags tg : tagsList) {
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.TAGS, tg.getName());
        database.insert(DatabaseHelper.TABLE_TAGS, null, values);
    }
    }
}

Solution

As @Mangal has pointed out, my mistake was in not initializing the RecordsDAO in its entirety, hence the Null Pointer Exception. The class which includes GetRecordsTask class did declare

RecordsDAO recordsDAO;

but this is incomplete initialization. In reality, it should be

RecordsDAO recordsDAO = new RecordsDAO(getActivity());

and that solved my issue. Big thanks for helping me out on that one, problem solved.

Pero Alex
  • 43
  • 1
  • 1
  • 6

1 Answers1

0
ArrayList<Records> recordsArrayList = recordsDAO.getRecords();

This line is suspicious to me too. You should've posted the recordsDAO's class too. I'd suggest you check your recordsDAO's getRecords() method carefully.
I wanted to comment this but I couldn't.

Man
  • 2,720
  • 2
  • 13
  • 21
  • I have edited my question, would you mind please taking a look at it? The method you are asking, `getRecords()`, is included. The SQLite query that you see is linked to tables created in another class called `DatabaseHelper`. By the way, thank you for responding to my question! – Pero Alex Jun 10 '18 at 09:20
  • Problem solved, will edit the thread to include the solution! Thank you so much for pointing out what I should check, it actually helped a lot. I accepted your answer as the solution. Even though it hasn't explicitly pointed out at my mistake, it helped me know what to look for. – Pero Alex Jun 10 '18 at 09:35
  • You're welcome. – Man Jun 10 '18 at 09:38