Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android while !c.isAfterLast() loop not being entered

Question Following tutorial, trying to put some simple string into a database every time I press a button. I believe that the "usernames" are being entered into the database, while debugging it doesn't do anything funny. My problem is when I want to call databaseToString(), I set a Cursor to the first entry. But subsequently it doesn't even enter while(!c.isAfterLast()). What is going on? I want it dbString to pretty much be equal to the database in plaintext, but that's not happening. Thanks

Attempts

  • Checked around SO/Google/API guide/thenewboston source code
  • Tried viewing my SQLite database, but I'm not sure how to do that so I couldn't
  • If you comment out databaseToString then I guess it runs, but it just sits there.
  • It also crashes when I set Text.setText(dbString); but that's completely unrelated

LogCat It doesn't crash, if needed I'll post.

SearchActivity.Java INSERT GETS CALLED WHEN THE BUTTON IS CLICKED

public class SearchActivity extends ActionBarActivity {

    EditText Input;
    TextView Text;
    Database dbHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        Input = (EditText) findViewById(R.id.Input);
        dbHandler = new Database(this, null, null, 1);
        // printDatabase();

    }

    // Add a user to database
    public void insert(View view) {
        Users user = new Users(Input.getText().toString());
        dbHandler.addUser(user);
        printDatabase();
    }


    public void printDatabase() {
        // dbString is returning null
        String dbString = dbHandler.databaseToString();
        //Technically it starts to crash right at this line
        Text.setText(dbString);
        Input.setText("");
    }    
}

Users.java

public class Users {
    private int _id;
    private String _username;

    public Users() {

    }

    public Users(String username){
        this._username = username;
    }

    public String get_username() {
        return _username;
    }

    public void set_username(String username) {
        this._username = username;
    }

    public int get_id() {
        return _id;
    }

    public void set_id(int id) {
        this._id = id;
    }
}

Database.java aka Handler. Look at databaseToString() and the while loop isn't entered whatsoever for some reason.

public class Database extends SQLiteOpenHelper {
    // Version number to upgrade database version
    // each time if you Add, Edit table, you need to change
    // the version number
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "users.db";
    private static final String TABLE_USERS = "users";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_USERNAME = "username";


    public Database(Context context, String DATABASE_NAME, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // There must be a ',' in between every column definition
        String query = "CREATE TABLE " + TABLE_USERS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_USERNAME + " TEXT " +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_USERS);
        onCreate(db);
    }

    //Add a new row to the db
    public void addUser(Users user){
        ContentValues values = new ContentValues();
        values.put(COLUMN_USERNAME, user.get_username());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_USERS, null, values);
        db.close();
    }

    public String databaseToString() {
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_USERS + " WHERE 1";

        // Cursor point to a location in your results
        Cursor c = db.rawQuery(query, null);
        // Move to the first row in your results
        c.moveToFirst();

        while(!c.isAfterLast()) {
            if(c.getString(c.getColumnIndex("username")) != null) {
                dbString += c.getString(c.getColumnIndex("username"));
                dbString += "\n";
            }
        }
        db.close();
        return dbString;
    }
}
like image 350
Script Kitty Avatar asked Jun 24 '26 15:06

Script Kitty


1 Answers

Fixing the NullPointerException:

First, to fix the NullPointerException, initialize Text in onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    Input = (EditText) findViewById(R.id.Input);
    Text = (TextView) findViewById(R.id.Text); //added
    dbHandler = new Database(this, null, null, 1);
    // printDatabase();

}

Debugging tips:

In order to make sure that insertion into the database is successful, capture the return value of insert():

long retVal = db.insert(TABLE_USERS, null, values);

Then, to verify that data is present in the Cursor, get the count of the Cursor:

Cursor c = db.rawQuery(query, null);
int count = c.getCount(); //added

Solution:

Finally, there are a few things to add here. First, you need to progress the Cursor in order to not have an infinite loop. Also, you should check the return value of moveToFirst(), and do nothing if it returns false. Lastly, you should close the Cursor when you're done with it, otherwise you'll have a memory leak.

public String databaseToString() {
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_USERS + " WHERE 1";

    // Cursor point to a location in your results
    Cursor c = db.rawQuery(query, null);
    // Move to the first row in your results
    //check that moveToFirst returns true
    if (c.moveToFirst()){

      while(!c.isAfterLast()) {
        if(c.getString(c.getColumnIndex("username")) != null) {
            dbString += c.getString(c.getColumnIndex("username"));
            dbString += "\n";
        }
        c.moveToNext(); //added
      }

    }
    c.close(); //added
    db.close();
    return dbString;
}
like image 53
Daniel Nugent Avatar answered Jun 26 '26 09:06

Daniel Nugent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!