0

Basically I am trying to load data from SQLite into Android tableLayout.

DBAdapter.java where I initialized database name and all tables

// TABLE INFORMATTION
public static final String TABLE_EXERCISE = "exercise";
public static final String EXERCISE_TYPE = "exerciseType";

// DATABASE INFORMATION
static final String DB_NAME = "schoolAssignment";
static final int DB_VERSION = 1;

public DBAdapter(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

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

SQLController.java where I put the SQL query

private DBAdapter dbhelper;
private Context ourcontext;
private SQLiteDatabase database;

public SQLController(Context c) {
    ourcontext = c;
}

public SQLController open() throws SQLException {
    dbhelper = new DBAdapter(ourcontext);
    database = dbhelper.getWritableDatabase();
    return this;
}

public void close() {
}

public Cursor readEntry() {

    String[] allColumns = new String[] { DBAdapter.EXERCISE_TYPE };

    Cursor c = database.query(DBAdapter.TABLE_EXERCISE, allColumns, null,
            null, null, null, null);

    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

exercise.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<TableLayout
    android:id="@+id/TableLayout"
    android:layout_width="280dp"
    android:layout_height="wrap_content" 
    android:layout_gravity="center">
</TableLayout>
</LinearLayout>

Exercise.java where I load the data from SQLite into tableLayout

TableLayout table_layout;
SQLController sqlcon;

ProgressDialog PD;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabhost);
    table_layout = (TableLayout) findViewById(R.id.TableLayout);
    BuildTable();
}

private void BuildTable() {

      sqlcon.open();
      Cursor c = sqlcon.readEntry();

      int rows = c.getCount();
      int cols = c.getColumnCount();

      c.moveToFirst();

      // outer for loop
      for (int i = 0; i < rows; i++) {

       TableRow row = new TableRow(this);
       row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
         LayoutParams.WRAP_CONTENT));

       // inner for loop
       for (int j = 0; j < cols; j++) {

        TextView tv = new TextView(this);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
          LayoutParams.WRAP_CONTENT));
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(18);
        tv.setPadding(0, 5, 0, 5);

        tv.setText(c.getString(j));
        row.addView(tv);

       }

       c.moveToNext();

       table_layout.addView(row);

      }
      sqlcon.close();
     }

However, I am getting null pointer exception as the logCat:

07-29 22:11:54.424: E/AndroidRuntime(22888): FATAL EXCEPTION: main
07-29 22:11:54.424: E/AndroidRuntime(22888): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.nyp.project/edu.nyp.project.Exercise}: java.lang.NullPointerException
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.os.Looper.loop(Looper.java:137)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.ActivityThread.main(ActivityThread.java:4512)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at java.lang.reflect.Method.invoke(Method.java:511)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at dalvik.system.NativeStart.main(Native Method)
07-29 22:11:54.424: E/AndroidRuntime(22888): Caused by: java.lang.NullPointerException
07-29 22:11:54.424: E/AndroidRuntime(22888):    at edu.nyp.project.Exercise.BuildTable(Exercise.java:75)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at edu.nyp.project.Exercise.onCreate(Exercise.java:47)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.Activity.performCreate(Activity.java:4465)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
07-29 22:11:54.424: E/AndroidRuntime(22888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-29 22:11:54.424: E/AndroidRuntime(22888):    ... 11 more
07-29 22:12:03.377: I/Process(22888): Sending signal. PID: 22888 SIG: 9

I wonder which part of my code went wrong. Thanks in advance.

1 Answers1

1

sqlcon is null at the start. You must initialize it before calling sqlcon.open();

Put:

sqlcon = new SQLController(this);

Zoran
  • 1,424
  • 1
  • 10
  • 12
  • It tells me no such table exercise but I did put the SQLite database into my project assets folder. Do you have any idea? –  Jul 29 '14 at 14:19
  • Any guides? Because I am just trying to retrieve only. –  Jul 29 '14 at 14:28
  • 1
    @20Cents The problem in this question is solved. If you have another question, use the "Ask Question" problem. – CL. Jul 29 '14 at 14:32
  • I don't know why it happens. As CL says, problem is solved (mark it!), ask another question and post log. – Zoran Jul 29 '14 at 14:41