0

为什么此应用程序显示错误空指针异常。在此应用程序中,我想检索“student_login”表中的条目编号并显示相同的编号。出勤表.xml 中的复选框

logcat 显示以下错误:

08-11 20:44:52.662: W/System.err(333): java.lang.NullPointerException
08-11 20:44:52.672: W/System.err(333):  at com.example.attendancesystem.TeacherLoggedInPage.retrieveStringRoll(TeacherLoggedInPage.java:115)
08-11 20:44:52.672: W/System.err(333):  at com.example.attendancesystem.TeacherLoggedInPage.getRollnoList(TeacherLoggedInPage.java:98)
08-11 20:44:52.672: W/System.err(333):  at com.example.attendancesystem.TeacherLoggedInPage.onCreate(TeacherLoggedInPage.java:56)
08-11 20:44:52.682: W/System.err(333):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-11 20:44:52.694: W/System.err(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-11 20:44:52.702: W/System.err(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-11 20:44:52.702: W/System.err(333):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-11 20:44:52.712: W/System.err(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-11 20:44:52.712: W/System.err(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 20:44:52.712: W/System.err(333):  at android.os.Looper.loop(Looper.java:123)
08-11 20:44:52.722: W/System.err(333):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-11 20:44:52.722: W/System.err(333):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 20:44:52.732: W/System.err(333):  at java.lang.reflect.Method.invoke(Method.java:521)
08-11 20:44:52.732: W/System.err(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-11 20:44:52.732: W/System.err(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-11 20:44:52.732: W/System.err(333):  at dalvik.system.NativeStart.main(Native Method)
08-11 20:44:52.742: D/AndroidRuntime(333): Shutting down VM
08-11 20:44:52.752: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-11 20:44:52.772: E/AndroidRuntime(333): FATAL EXCEPTION: main
08-11 20:44:52.772: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.attendancesystem/com.example.attendancesystem.TeacherLoggedInPage}: java.lang.NullPointerException
08-11 20:44:52.772: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-11 20:44:52.772: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-11 20:44:52.772: E/AndroidRuntime(333):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-11 20:44:52.772: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-11 20:44:52.772: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 20:44:52.772: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
08-11 20:44:52.772: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-11 20:44:52.772: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 20:44:52.772: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:521)

这是TeacherLoggedIn.class的代码

           protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attendancesheet);
        Bundle extras = getIntent().getExtras();
        if (savedInstanceState == null) {
            extras = getIntent().getExtras();
            if (extras == null) {
                t_id = (String) null;

            } else {
                t_id = extras.getString("key");

            }
        } else {
            t_id = (String) savedInstanceState.getSerializable("key");
        }
        try {
            Roll_array = getRollnoList(TeacherLoggedInPage.this);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Array_Count = Roll_array.size();

        LinearLayout my_layout = (LinearLayout) findViewById(R.id.layout_checklist);

        for (int i = 0; i < Array_Count; i++) {
            TableRow row = new TableRow(this);
            row.setId(i);
            row.setLayoutParams(new LayoutParams(
                    android.widget.TableRow.LayoutParams.FILL_PARENT,
                    android.widget.TableRow.LayoutParams.WRAP_CONTENT));

            CheckBox checkBox = new CheckBox(this);

            checkBox.setId(i);
            checkBox.setText(Roll_array.get(i));
            row.addView(checkBox);
            my_layout.addView(row);
        }

        UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
        UpdateAttendanceButton.setOnClickListener(this);

    }

    private ArrayList<String> getRollnoList(Context context) {
        // TODO Auto-generated method stub
        try {

            SQLiteDatabase db = myDb.getReadableDatabase();

            String[] columns = { "s_roll_no" };

            Cursor cursor = db.query(DbHelper.STUDENT_TABLE, columns, null,
                    null, null, null, null);
            if (cursor != null) {
                System.out.println("fething roll nos");
                startManagingCursor(cursor);
                ArrayList<String> temp = retrieveStringRoll(cursor);
                return temp;
            }
            System.out.println("Cursor NuLL");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private ArrayList<String> retrieveStringRoll(Cursor cursor) {
        // TODO Auto-generated method stub

        while (cursor != null && cursor.moveToNext()) {
            String roll = cursor.getString(0);
            temp1.add(roll);
        }
        if (!cursor.isClosed()) {
            cursor.close();
        }
        return temp1;
    }

这是我创建表 studentLogin.class 的 DbHelper.class

private static final int DATABASE_VERSION = 1;


    public static final String ATTENDANCE_TABLE = "Student_attendance";
        public static final String TEACHER_TABLE = "Teacher_login";
        public static final String STUDENT_TABLE = "Student_login";
        public static final String T_ROW_ID = "teacher_id";

        private static final String ATTENDANCE_TABLE_CREATE = "CREATE TABLE "
                + ATTENDANCE_TABLE + "(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "teacher_id INTEGER , " + "time TEXT NOT NULL, "
                + "rl_one INTEGER , rl_two INTEGER , rl_three INTEGER ,"
                + "rl_four INTEGER , rl_five INTEGER , rl_six INTEGER); ";

        private static final String TEACHER_TABLE_CREATE = "CREATE TABLE "
                + TEACHER_TABLE
                + "( teacher_id INTEGER PRIMARY KEY AUTOINCREMENT , "
                + "t_name TEXT NOT NULL , t_pass TEXT NOT NULL , t_email TEXT NOT NULL);";

        private static final String STUDENT_TABLE_CREATE = "CREATE TABLE "
                + STUDENT_TABLE + "( s_id INTEGER PRIMARY KEY AUTOINCREMENT , "
                + "s_name TEXT NOT NULL , s_pass TEXT NOT NULL , "
                + "s_roll_no TEXT NOT NULL , " + "s_email TEXT NOT NULL);";

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            System.out.println("In constructor");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            try {
                db.execSQL(TEACHER_TABLE_CREATE);
                db.execSQL(ATTENDANCE_TABLE_CREATE);
                db.execSQL(STUDENT_TABLE_CREATE);

                System.out.println("In onCreate");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
4

1 回答 1

0

改变

if (!cursor.isClosed()) {
    cursor.close();
}

if (cursor != null && !cursor.isClosed()) {
    cursor.close();
}

确保temp1变量已正确初始化。

于 2013-08-11T15:59:35.677 回答