Just to preface I've been linked to a thread on null pointer references and couldn't see how it helped for my situation it may just be because I started using java a month ago.. so sorry if this question is redundant. I'm receiving null object reference error to my function MYDBHandler.staffToString() which is being called in my class here.
public class Order extends AppCompatActivity {
Spinner staffSpinner;
MyDBHandler db;
String staffNames = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
staffSpinner = (Spinner) findViewById(R.id.staffSpinner);
populateSpinner(db);
}
public void populateSpinner(MyDBHandler db){
staffNames = db.staffToString();
List<String> Staff = new ArrayList<>();
String[] parts = staffNames.split("/n");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
String part4 = parts[3];
String part5 = parts[4];
Staff.add(part1);
Staff.add(part2);
Staff.add(part3);
Staff.add(part4);
Staff.add(part5);
ArrayAdapter<String> adp1=new ArrayAdapter<>(Order.this,android.R.layout.simple_list_item_1,Staff);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staffSpinner.setAdapter(adp1);
}
}
Pointing to this function in my dbhandler class
public String staffToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_STAFF + " WHERE 1";
//Cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
//Move back to first row
c.moveToFirst();
while (!c.isAfterLast()){
if (c.getString(c.getColumnIndex("staffname")) !=null){
dbString += c.getString(c.getColumnIndex("staffname"));
dbString += "/n";
}
c.moveToNext();
}
db.close();
c.close();
return dbString;
}
if the problem isnt here I can post more but I'm currently stumped as my objects shouldnt be null?
Aucun commentaire:
Enregistrer un commentaire