填充TableLayout时的Android Java NullPointerException

时间:2014-07-29 14:13:42

标签: java android sqlite nullpointerexception

基本上我正在尝试将SQLite中的数据加载到Android tableLayout中。

DBAdapter.java 我初始化数据库名称和所有表格

// 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 我把SQL查询放在哪里

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 我从SQLite加载数据到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();
     }

但是,我正在将log指针异常作为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

我想知道我的代码的哪一部分出了问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

sqlcon在开始时为null。您必须在调用sqlcon.open();

之前初始化它

放:

sqlcon = new SQLController(this);