I've been working on a little android app and I've run into a bizarre problem that I can't work out. In troubleshooting I narrowed it down with this little bit of test code. I'm getting a NullPointerException for the line: String check = test.getName().
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.******.triptracker.Trip.getName()' on a null object reference
Trip is an object I created with an id and a name. trips is an array list of Trip objects.
tripListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
clickedItemIndex = position;
Trip test = trips.get(clickedItemIndex);
int testID = test.getID();
test = dbHandler.getTrip(testID);
String check = test.getName();
}
});
The arraylist trips comes from a class called DatabaseHandler which also has the "getTrip()" method which pulls the actual trip from the SQLite database, this is the where I imagine the problem could is:
/** PURPOSE: Return trip from database with given ID **/
public Trip getTrip(int id)
{
SQLiteDatabase db = getReadableDatabase();
Trip trip = null;
String[] args = new String[] { KEY_TRIP_ID };
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_TRIPS + " WHERE ? = " + id, args);
if (cursor.moveToFirst())
{
trip = new Trip(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
}
cursor.close();
db.close();
return trip;
}
Obviously I'm getting the NullPointerException because getTrip is sending back null which means the cursor is coming back empty and I can't for the life of me figure out why that might be.
DatabaseHandler creates the trips arraylist:
/** PURPOSE: Fetch all trips in database
RETURNS: List of all trips **/
public List<Trip> getAllTrips()
{
SQLiteDatabase db = getWritableDatabase();
List<Trip> trips = new ArrayList<Trip>();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_TRIPS, null);
if (cursor.moveToFirst())
{
do {
trips.add(new Trip(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3)));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return trips;
}
That is the same arraylist which is displayed in tripListView. So the trip being clicked on must exist otherwise you wouldn't be able to click on it. The only other possibility then is that the getID is returning the wrong id but with a Trip class this simple I can't see how that's possible either.
public class Trip {
private int id;
private String name, startDate, endDate;
public Trip(int id, String name, String startDate, String endDate)
{
this.id = id;
this.name = name;
this.startDate = startDate;
this.endDate = endDate;
}
// Accessors
public int getID() { return id; }
public String getName() { return name; }
}
I'm tearing my hair out here trying to figure this out so maybe a fresh pair of eyes will catch it. Let me know if you see anything here.