-1

I'm trying to use a table to show students data as the following :

    public class Main5Activity extends AppCompatActivity {
    DataBaseConnection db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);
    setContentView(R.layout.activity_main);

    final TableLayout stk = findViewById(R.id.datatable);
    TableRow tbrow0 = new TableRow(this);
    TextView SNo = new TextView(this);
    SNo.setText(" Student NO ");
    SNo.setTextColor(Color.WHITE);
    tbrow0.addView(SNo);
    TextView StudentName = new TextView(this);
    StudentName.setText(" Student Name ");
    StudentName.setTextColor(Color.WHITE);
    tbrow0.addView(StudentName);
    TextView hmewrk = new TextView(this);
    hmewrk.setText(" Homework mark ");
    hmewrk.setTextColor(Color.WHITE);
    TextView orlmrk = new TextView(this);
    orlmrk.setText(" Oral Mark ");
    orlmrk.setTextColor(Color.WHITE);
    TextView ttlwrkmrk = new TextView(this);
    ttlwrkmrk.setText(" Total Works Mark");
    ttlwrkmrk.setTextColor(Color.WHITE);
    TextView orlexm = new TextView(this);
    orlexm.setText(" Oral Exam");
    orlexm.setTextColor(Color.WHITE);
    TextView exm = new TextView(this);
    exm.setText(" Exam ");
    exm.setTextColor(Color.WHITE);
    TextView exmttl = new TextView(this);
    exmttl.setText(" Exam total");
    exmttl.setTextColor(Color.WHITE);
    TextView textView9 = new TextView(this);
    textView9.setText(" Average");
    textView9.setTextColor(Color.WHITE);
    tbrow0.addView(hmewrk);
    tbrow0.addView(orlexm);
    stk.addView(tbrow0);
    for (String i = db.stdntnmbr();; ) {

When calling the students number value from the database to create rows as the same value, it gives me the following error: The code is :

public class DataBaseConnection {

private final DataBase dataBase;

DataBaseConnection(Context context) {
    dataBase = new DataBase(context);
}
public String stdntnmbr(){
    SQLiteDatabase sqLiteDatabase = dataBase.getWritableDatabase();
    String uid = DataBase.UID;
    Cursor cursor = sqLiteDatabase.query(DataBase.tablename1 , null, uid, null
    , null , null , null , null);
    StringBuilder stringBuilder = new StringBuilder();
    while (cursor.moveToNext()) {
        int studentnmbr = cursor.getInt(1);
        stringBuilder.append(studentnmbr + " " +"+/n");
    }
    return String.valueOf(stringBuilder);   

the database type is SQLite and I'm using SQLiteOpenHelper. Any help would be greatly appreciated.

dchi
  • 19
  • 2

1 Answers1

0

You probably don't want the line setContentView(R.layout.activity_main); just the line setContentView(R.layout.activity_main5); (or vice-versa) depending upon which layout has the TableLayout and the TextViews etc that you use.

and then you want to include a line to instantiate the DataBaseConnection db. So add a line db = new DataBaseConnection(this); (immediately after the setContentView used).

If you still have issues with a null pointer exception then edit your question to include the stack-trace (i.e. not just the error message).

MikeT
  • 51,415
  • 16
  • 49
  • 68