This is probably such a simple fix, and I just can't see it.
I have a class which is used to create an entry into the sqlite table TRANSACTIONS. Part of this transaction creation includes inserting the borrower's name. To do this, I have a method which should get the user's name, by passing the user's B-Number as a parameter.
However, when I try to create the transaction, the app crashes and I am presented with the error
java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at example.anna.com.pi_prototype_30.UsersDAO.cursorToUsers(UsersDAO.java:124)
at example.anna.com.pi_prototype_30.UsersDAO.getUserName(UsersDAO.java:110)
at example.anna.com.pi_prototype_30.BorrowScan$1.onClick(BorrowScan.java:118)
CursorToUsers method:
private Users cursorToUsers(Cursor cursor){
Users user = new Users();
user.setTableID(cursor.getLong(0));
user.setuType(cursor.getString(1));
user.setIdNum(cursor.getString(2));
user.setName(cursor.getString(3));
user.setEmail(cursor.getString(4));
user.setCourse(cursor.getString(5));
user.setPassword(cursor.getString(6));
return user;
}
getUserName Method
public Users getUserName(String m) {
Cursor c = aDatabase.rawQuery("SELECT 1 FROM users WHERE id_number = ?", new String[]{m});
c.moveToFirst();
Users nameL;
if (c.moveToFirst()){
do {
nameL = cursorToUsers(c);
}while (c.moveToNext());
}
else{
return null;
}
c.close();
return nameL;
}
CreateLoan method
public void addListenerOnCreateLoanButton(){
final Context context = this;
saveButton = (Button) findViewById(R.id.borrow_scan_create_loan);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean didi = true;
//Initialises today's date
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
//Initialise loan variables
String bNum = cardContentTxt.getText().toString();
String kNum = contentTxt.getText().toString();
String bDate = df.format(c.getTime());
String rDate = "N/A";
String type = "Loan";
String bName = (usersDAO.getUserName(bNum)).toString();
try {
transDAO.open();
transDAO.createTransactionEntry(type, bDate, rDate, bNum, bName, kNum);
transDAO.close();
}//try
catch (Exception e) {
didi = false;
Log.d(TAG, "Error");
e.printStackTrace();
}//catch
finally {
if (didi) {
Dialog d = new Dialog(BorrowScan.this);
d.setTitle("Database write");
TextView tv = new TextView(BorrowScan.this);
tv.setText("it worked ");
Log.d(TAG, "Created loan");
d.setContentView(tv);
d.show();
}//if
}//finally
}//onClick
});//onClickListener
}//createLoan
The lines being flagged as causing the error are:
Line 124:
user.setuType(cursor.getString(1));
Line 110:
nameL = cursorToUsers(c);
Line 118:
String bName = (usersDAO.getUserName(bNum)).toString();
While I'm sure there is plenty I have done wrong, what is it that is causing this specific error?
Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire