简体   繁体   English

Android SQLite数据库查询问题

[英]Android SQLite database query issue

I've been working on a little android app and I've run into a bizarre problem that I can't work out. 我一直在开发一个小型android应用程序,却遇到了一个我无法解决的怪异问题。 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(). 我正在为该行获取一个NullPointerException:字符串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. Trip是我使用ID和名称创建的对象。 trips is an array list of Trip objects. trips是Trip对象的数组列表。

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: arraylist行程来自一个名为DatabaseHandler的类,该类还具有“ getTrip()”方法,该方法从SQLite数据库中提取实际行程,这就是我想象的问题所在:

/** 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. 显然,我得到了NullPointerException,因为getTrip发送回null,这意味着游标返回为空,我一生都无法弄清楚为什么会这样。

DatabaseHandler creates the trips arraylist: DatabaseHandler创建行程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. 那就是在tripListView中显示的相同arraylist。 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. 然后,唯一的另一种可能性是getID返回错误的ID,但是对于Trip类,这种简单的操作我也不知道怎么可能。

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. 让我知道您是否在这里看到任何东西。

These lines are odd: 这些行很奇怪:

String[] args = new String[] { KEY_TRIP_ID };

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_TRIPS + " WHERE ? = " + id, args);

Typically the args would be the values for the query, so I would expect to see this: 通常,args将是查询的 ,因此我希望看到以下内容:

String[] args = new String[] { Integer.toString(id) };

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_TRIPS + " WHERE " + KEY_TRIP_ID + " = ?", args);

I think the query should still work the way you have it, though. 我认为查询仍然可以按照您的方式工作。


EDIT: 编辑:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    clickedItemIndex = position;
    Trip test = trips.get(clickedItemIndex);  // so if you have the Trip here...
    int testID = test.getID();
    test = dbHandler.getTrip(testID);         // why are you retrieving it here?
    String check = test.getName();            // shouldn't they be exactly the same?
}

It seems like the problem was the id being an int, as was sort of suggested above. 就像上面建议的那样,问题似乎是id为int。 But I can't get it to work with the args at all, which is unfortunate because I really wanted to figure out how to make use of that functionality. 但是我根本无法使其与args一起使用,这是不幸的,因为我真的很想弄清楚如何利用该功能。 Anyway this is the only way it seems to work: 无论如何,这似乎是唯一可行的方法:

Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_TRIPS + " WHERE " + KEY_TRIP_ID + " = " + Integer.toString(id), null);

The weird thing is that using the parameter int id in other methods works perfectly fine. 奇怪的是,在其他方法中使用参数int id可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM