This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
5年前关闭。
基本上,我试图将数据从SQLite加载到Android tableLayout中。
我初始化数据库名称和所有表的DBAdapter.java
我在其中放置SQL查询的SQLController.java
exercise.xml
Exercise.java,我将数据从SQLite加载到tableLayout中
但是,我得到空指针异常作为logCat:
我想知道我的代码哪一部分出错了。提前致谢。
(12个答案)
5年前关闭。
基本上,我试图将数据从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);
}
我在其中放置SQL查询的SQLController.java
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();
}
但是,我得到空指针异常作为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
我想知道我的代码哪一部分出错了。提前致谢。
最佳答案
sqlcon在开始时为null。您必须在调用sqlcon.open()之前对其进行初始化。
放:
sqlcon =新的SQLController(this);
08-07 23:03