Flowdorio
Flowdorio

Reputation: 157

Getting all database entries from android database

I have an android project, with a database where entries are logged as pairs with the same ID.

I have two for loops, one to get all entries of a certain ID, and one to get all entries in the table. These are then automatically populated into a listview.

The problem I have is that the program crashes when I attempt to use the forloop that gets all entries from the database, but it works perfectly fine when I use the for loop that wants all entries with the same ID. I have been commenting out the for loop im not using.

The for loops:

        List<Assignment> list = db.getPickupDelivery(1);
        for (int i = 0; i < list.size();i++)
            {
                allDeliveries.add(list.get(i));
            }

        List<Assignment> list = db.getAllAssignments();
        for (int i = 0; i < list.size();i++)
        {
            allDeliveries.add(list.get(i));
        }

The methods they call:

public List<Assignment> getPickupDelivery(int i) {
        List<Assignment> assignments = new LinkedList<Assignment>();

        //bygg query
        String query = "SELECT * FROM " + TABLE_ASSIGNMENTS + " WHERE id = " + i;

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

        //iterera och bygg och lagg till
        Assignment assignment = null;
        if (cursor.moveToFirst()) {
            do {
                assignment = new Assignment();
                assignment.setID(Integer.parseInt(cursor.getString(0)));
                assignment.setType(cursor.getString(1));
                assignment.setSenderreceiver(cursor.getString(2));
                assignment.setAdress(cursor.getString(3));
                assignment.setTime(cursor.getString(4));
                assignment.setZipcode(cursor.getString(5));
                assignment.setContact(cursor.getString(6));
                assignment.setPhone(cursor.getString(7));
                assignment.setInstructions(cursor.getString(8));

                //lagg till
                assignments.add(assignment);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("getPickupDelivery()", assignments.toString());
        return assignments;
    }

public List<Assignment> getAllAssignments() {
        List<Assignment> assignments = new LinkedList<Assignment>();

        //bygg query
        String query = "SELECT * FROM " + TABLE_ASSIGNMENTS;

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

        //iterera och bygg och lagg till
        Assignment assignment = null;
        if (cursor.moveToFirst()) {
            do {
                assignment = new Assignment();
                assignment.setID(Integer.parseInt(cursor.getString(0)));
                assignment.setType(cursor.getString(1));
                assignment.setSenderreceiver(cursor.getString(2));
                assignment.setAdress(cursor.getString(3));
                assignment.setTime(cursor.getString(4));
                assignment.setZipcode(cursor.getString(5));
                assignment.setContact(cursor.getString(6));
                assignment.setPhone(cursor.getString(7));
                assignment.setInstructions(cursor.getString(8));

                //lagg till
                assignments.add(assignment);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("getAllAssignments()", assignments.toString());
        return assignments;
    }

The error I get at the time of the crash is a an error that says "...MainActivity}: java.lang.NumberFormatException: Invalid int: "null""

Upvotes: 0

Views: 366

Answers (3)

Sam Dozor
Sam Dozor

Reputation: 40734

My guess is that in your database definition, you didn't specify the ID column as INTEGER NOT NULL. So when you select every row, one or more of those rows has a null ID, which you're trying to parse as an int here:

assignment.setID(Integer.parseInt(cursor.getString(0)));

Change your database definition to require that the ID column is not null. You'll need to uninstall or clear data on your app so the database is recreated.

Also another tip - Android's SQLITE implementation will enforce that you have a column called "_id", so I'd suggest using that, not "id". Otherwise you'll end up with an extra column you don't need.

Upvotes: 1

Simon
Simon

Reputation: 123

The exception is the hint. It says that a string null is being parsed as a number and this fails.

In your code there the place where it happens is:

assignment.setID(Integer.parseInt(cursor.getString(0)));

First you get a string from the cursor and then you pass it to Integer.parseInt for parsing.

So the cause of the problem is that you expect there to be no null values in the first column but there is at least one.

Also, your code is unnecessarily complex and inefficient. Instead of Integer.parseInt(cursor.getString(0)) you could write cursor.getInt(0) to directly get an int. This is also likely to throw an error on the null value though (but the error message might be more informative).

Upvotes: 0

GioLaq
GioLaq

Reputation: 2547

In your code:

                assignment.setID(Integer.parseInt(cursor.getString(0)));

if cursor.getString(0) returns null, then Integer.parseInt(String str) will throw NumberFormatException. Check this line or you can use try-catch and print the appropriate message in catch if you'll get NumberFormatException.

Upvotes: 0

Related Questions