0

Please, help! By pressing the button, parameter 0 is always transmitted.

MainActivity.class

this.listResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dbResult.read(RssActivity.this);
            RssData rssData = dbResult.select(id);
            Toast.makeText(getBaseContext(), valueOf(id), Toast.LENGTH_SHORT).show();

DataBase

public class DBResult {
...
SQLiteDatabase database;

public DBResult(Context context){
    DBResult.ResultOpenHelper openHelper = new DBResult.ResultOpenHelper(context);
    database = openHelper.getWritableDatabase();
}
public long insert(RssData rssData) {
    ContentValues cv = new ContentValues();
    cv.put(RSS_NAME, rssData.getRssName());
    cv.put(RSS_TITLE, rssData.getTitle());
    cv.put(RSS_LINK, rssData.getLink());
    cv.put(RSS_DESCRIPTION, rssData.getDescription());
    return database.insert(TABLE_NAME, null, cv);
}
public int update(RssData rssData){
    ...
}
public RssData select(long id){
    Cursor cursor = database.query(TABLE_NAME,null, RSS_ID + " = ?", new String[]{String.valueOf(id)}, null, null, RSS_ID);
    if (cursor.moveToFirst()){
    String rssName = cursor.getString(NUM_RSS_NAME);
    String title = cursor.getString(NUM_RSS_TITLE);
    String link = cursor.getString(NUM_RSS_LINK);
    String description = cursor.getString(NUM_RSS_DESCRIPTION);
    new RssData (id, rssName, title, link, description);}
    while (cursor.moveToNext());
    return null;
}
...
private class ResultOpenHelper extends SQLiteOpenHelper {
    public ResultOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        database=db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
                RSS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                RSS_NAME + " TEXT, " +
                RSS_TITLE + " TEXT, " +
                RSS_DESCRIPTION + " TEXT, " +
                RSS_LINK + " TEXT ); ";
        db.execSQL(sql);
    }
    ...
}

What am I doing wrong? I need to get a link to open it in WebView.

ERROR: "on a null object reference"

I will be very grateful for any help!

Thank you in advance!

1 Answers1

0

The following will always return null.

public RssData select(long id){
    Cursor cursor = database.query(TABLE_NAME,null, RSS_ID + " = ?", new String[]{String.valueOf(id)}, null, null, RSS_ID);
    if (cursor.moveToFirst()){
    String rssName = cursor.getString(NUM_RSS_NAME);
    String title = cursor.getString(NUM_RSS_TITLE);
    String link = cursor.getString(NUM_RSS_LINK);
    String description = cursor.getString(NUM_RSS_DESCRIPTION);
    new RssData (id, rssName, title, link, description);}
    while (cursor.moveToNext());
    return null;
}

Use :-

public RssData select(long id){
    Rssdata rv;
    Cursor cursor = database.query(TABLE_NAME,
        null, 
        RSS_ID + " = ?", 
        new String[]{String.valueOf(id)}, 
        null, null, RSS_ID
    );
    if (cursor.moveToFirst()){
        String rssName = cursor.getString(NUM_RSS_NAME);
        String title = cursor.getString(NUM_RSS_TITLE);
        String link = cursor.getString(NUM_RSS_LINK);
        String description = cursor.getString(NUM_RSS_DESCRIPTION);
    }
    cursor.close(); //<<<< Should always close Cursors when done with them.
    return rv;
}

However, if no row is found then it will still return null, so you should code accordingly for null being returned e.g. :-

    this.listResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            dbResult.read(RssActivity.this);
            RssData rssData = dbResult.select(id);
            if (rssData == null) {
                Toast.maketext(getBaseContext(),"Ooops!!! no such row in DB",Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(getBaseContext(), valueOf(id), Toast.LENGTH_SHORT).show();

NOTE

The above assumes that the Adapter is a CursorAdapter or a Custom Adapter that correctly passes id to the onItemClick method. If the Adapter is not such an Adapter then id will likely not be the id of the respective row rather it will be the position of the selected item.

MikeT
  • 51,415
  • 16
  • 49
  • 68