'How do collect data from a specific database using cursor . I get an illegal argument exception. I want to show the bio data of the person logged in
//error from the logcat
Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:183)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:219)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1967)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1906)
at com.example.covidapp.DBelper.biopage(DBelper.java:182)
at com.example.covidapp.profilepage.onCreate(profilepage.java:52)
//This function is to get the userdetails from the user
public class Register extends AppCompatActivity {
Button nextpage;
DBelper DB;
public TextInputLayout firstname, middlename, lastname, gender, dob;
CheckBox male, female, other;
String id;
public String firstnametxt, middlenametxt, lastnametxt, dobtxt, gendertxt;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registerscreen);
nextpage = findViewById(R.id.nexttbtn);
firstname = findViewById(R.id.firstname);
middlename = findViewById(R.id.middlename);
lastname = findViewById(R.id.lastname);
male = findViewById(R.id.m);
female = findViewById(R.id.f);
other = findViewById(R.id.o);
dob = findViewById(R.id.dob);
user user = new user();
usermodel info = new usermodel();
id = UUID.randomUUID().toString();
user.setuser(id);
DB = new DBelper(this);
// patient patient;
nextpage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
patient patient;
firstnametxt = firstname.getEditText().getText().toString().toUpperCase().trim();
middlenametxt = middlename.getEditText().getText().toString().toUpperCase().trim();
lastnametxt = lastname.getEditText().getText().toString().toUpperCase().trim();
dobtxt = dob.getEditText().getText().toString().toUpperCase().trim();
//datagetter get = new datagetter();
//get.setFirstname(firstnametxt);
if (male.isChecked()) {
gendertxt = male.getText().toString().toUpperCase().trim();
}
if (female.isChecked()) {
gendertxt = male.getText().toString().toUpperCase().trim();
}
if (other.isChecked()) {
gendertxt = male.getText().toString().toUpperCase().trim();
}
patient = new patient(firstnametxt);
DB.getter(patient);
if ((!validatefirstname(firstnametxt) | !validatemiddlename(middlenametxt) | !validatelastname(lastnametxt) | !validatedob(dobtxt))) {
} else {
if (male.isChecked() && female.isChecked()) {
Toast.makeText(Register.this, "Select Only One Gender", Toast.LENGTH_LONG).show();
} else if (male.isChecked() && female.isChecked()) {
Toast.makeText(Register.this, "Select Only One Gender", Toast.LENGTH_LONG).show();
} else if (female.isChecked() && other.isChecked()) {
Toast.makeText(Register.this, "Select Only One Gender", Toast.LENGTH_LONG).show();
} else if (male.isChecked() && other.isChecked()) {
Toast.makeText(Register.this, "Select Only One Gender", Toast.LENGTH_LONG).show();
} else {
DB.getbio(firstnametxt, middlenametxt, lastnametxt, dobtxt, gendertxt);
Intent intent = new Intent(Register.this, Register2.class);
startActivity(intent);
}
}
}
});
}
public String getid() {
return id;
}
public boolean validatefirstname(String val) {
if (val.equals("")) {
firstname.setError("Field is empty");
return false;
} else if (!val.matches("[a-zA-Z]+")) {
firstname.setError("Field Should Only Contain Alphabets");
return false;
} else {
firstname.setError(null);
firstname.setErrorEnabled(false);
return true;
}
}
public boolean validatemiddlename(String val) {
if (val.equals("")) {
middlename.setError(null);
middlename.setErrorEnabled(false);
return true;
} else if (!val.matches("[a-zA-Z]+")) {
middlename.setError("Field Should Only Contain Alphabets");
return false;
} else {
middlename.setError(null);
middlename.setErrorEnabled(false);
return true;
}
}
public boolean validatelastname(String val) {
if (val.equals("")) {
lastname.setError("Field is empty");
return false;
} else if (!val.matches("[a-zA-Z]+")) {
lastname.setError("Field Should Only Contain Alphabets");
return false;
} else {
lastname.setError(null);
lastname.setErrorEnabled(false);
return true;
}
}
public boolean validatedob(String val) {
SimpleDateFormat date = new SimpleDateFormat("dd/mm/yyyy");
Date d;
if (val.equals("")) {
dob.setError("Field is empty");
return false;
} else if (val.matches("[0-9]{2}[/]{2}[0-9]{2}[0-9]{1}[0-9]{4}")) {
return true;
} else {
dob.setError(null);
dob.setErrorEnabled(false);
return true;
}
}
}
//code for the getter and setter methods
public class datagetter {
private String firstname ;
public datagetter(String firstname){
this.firstname = firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getFirstname() {
return firstname;
}
public datagetter(){
}}
//fucntion in the databasehelper class to get information from the table containing the name of the account logged in
public Cursor biopage() {
model u = new model();
String s = u.getfirstname();
datagetter g = new datagetter();
data user = new data();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from USERDATA_BIO ", null);
StringBuilder firstName = new StringBuilder();
String lastName;
String x = g.getFirstname();
// datagetter data = new datagetter();
// Log.i("data",data.getFirstname());
// System.out.println(data.getFirstname());
if (cursor.moveToNext()) {
get = cursor.getString(1);
firstName.append(cursor.getString(1));
}
Log.i(x, "values of firstname");
Cursor cursor1 = db.rawQuery("select * from USERDATA_BIO where firstname = ? ", new String[]{g.getFirstname()});
return cursor1;
}
//class that shows the activity that would show the info of the user logged in
public class profilepage extends AppCompatActivity {
Button logout,bio,personal,account,appoint;
DBelper DB;
TextView fname,lname,mname;
protected void onCreate( Bundle savedInstanceState ){
super.onCreate( savedInstanceState);
setContentView( R.layout.profilepage);
logout = findViewById( R.id.logbtn);
DB = new DBelper(this);
fname = findViewById(R.id.fnametext);
lname = findViewById(R.id.lnametext);
mname = findViewById(R.id.middlenametext);
bio = findViewById(R.id.biobtn);
personal = findViewById(R.id.perbtn);
account = findViewById(R.id.accbtn);
appoint = findViewById(R.id.appbtn);
appoint.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent intent= new Intent(profilepage.this,appointment.class);
startActivity(intent);
}
});
personal.setOnClickListener(new View.OnClickListener(){
public void onClick(View view ){
Intent intent = new Intent(profilepage.this, personal.class);
startActivity(intent);
}
});
account.setOnClickListener(new View.OnClickListener(){
public void onClick(View view ){
Intent intent = new Intent(profilepage.this, account.class);
startActivity(intent);
}
});
bio.setOnClickListener(new View.OnClickListener(){
public void onClick(View view ){
Intent intent = new Intent(profilepage.this, biopage.class);
startActivity(intent);
}
});
Cursor cursor = DB.biopage();
StringBuilder stringname = new StringBuilder();
StringBuilder stringlast=new StringBuilder();
StringBuilder middle = new StringBuilder();
while(cursor.moveToNext()){
stringname.append(cursor.getString(2));
middle.append(cursor.getString(2));
stringlast.append(cursor.getString(3));
}
mname.setText(middle);
fname.setText(stringname);
lname.setText(stringlast);
logout.setOnClickListener( new View.OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(profilepage.this,MainActivity.class);
startActivity(intent);
}
});
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
