0

我收到此错误空指针异常。
小背景
我有一个主类,添加约会类和约会数据类。约会数据类有一个创建数据库(SQLite)的方法,如果不存在的话。AddAppointment 类从具有几个 editText 的视图中获取值,并且用户插入一些值,并且此数据应传递给将值插入数据库的同一类中的方法 addEvent,该类之前在约会数据类中创建。但是从未调用约会数据类,因此从未创建数据库,因此我得到空指针异常(无法将值插入到数据库中)。
现在我的问题是。我如何在主类中调用类约会数据,以便调用 onCreate 方法并创建数据库。
下面的代码
约会数据类

 public class AppointmentsData extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "appointments.dba";
    private static final int DATABASE_VERSION = 1;
    private String TIME;
    private String DESCRIPTION;
    private String DETAILS;
    private String DATE;

    //create helper object for events database
    public AppointmentsData(Context ctx){
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" 
                + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + TIME + " TEXT NOT NULL," 
                + DATE + " TEXT NOT NULL" 
                + DESCRIPTION + " TEXT NOT NULL"
                + DETAILS + " TEXT NOT NULL");
    }
       }

添加约会类

public class AddAppointment extends Activity implements OnClickListener {
    private AppointmentsData events;
    private EditText appDesctription;
    private EditText appDate;
    private EditText appTime;
    private EditText appDetails;
    SQLiteDatabase myDB;
    //Connection conn; //A connection (session) with a specific database
    ResultSet rs; //A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
    PreparedStatement pst; //An ob
    String TableName = "appointments";


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appointment_details);

        appDate = (EditText) this.findViewById(R.id.appDate);

        appDate.setKeyListener(null);
        Bundle extras = getIntent().getExtras();
        if(extras !=null) {
            String dateSelected = extras.getString("dateselected");
            appDate.setText(dateSelected);

            View saveAppointment = findViewById(R.id.buttonSave);
            saveAppointment.setOnClickListener(this);

            //SQLiteDatabase db = dbHelper.getWritableDatabase();




        }

    }
    private String DESCRIPTION;
    private String DATE;
    private String TIME;
    private String DETAILS;
    @Override
    public void onClick(View v) {

            appDesctription = (EditText) this.findViewById(R.id.appDescription);
            DESCRIPTION = appDesctription.getText().toString();

            appTime = (EditText) this.findViewById(R.id.appTime);
            TIME = appTime.getText().toString();

            appDetails = (EditText) this.findViewById(R.id.appDetails);
            DETAILS = appDetails.getText().toString();

            DATE = appDate.getText().toString();

            AddAppointment aa = new AddAppointment();
            aa.addEvent(DESCRIPTION);
            aa.addEvent(DATE);
            aa.addEvent(DETAILS);
            aa.addEvent(TIME);

            Intent j = new Intent(this, CalendarViewActivity.class);
            startActivity(j);

    }


            private void addEvent(String string) {
                //insert a new record into Events data source
                //would do something similar for delete and update
                SQLiteDatabase db = events.getWritableDatabase();
                ContentValues values = new ContentValues();
                // add key and value
                values.put(DESCRIPTION, string);
                values.put(DATE, string);
                values.put(TIME, string);
                values.put(DETAILS, string);
                db.insertOrThrow(TABLE_NAME, null, values);
            }
            private String[] FROM = { _ID, TIME, DESCRIPTION, };
            private static String ORDER_BY = _ID + "DESC";
            private Cursor getEvents(){
                //perform managed query. The activity will handle closing
                //and requerying the cursor when needed
                SQLiteDatabase db = events.getReadableDatabase();
                Cursor cursor = db.query(TABLE_NAME, FROM ,null, null, null, null, ORDER_BY);
                startManagingCursor(cursor);
                return cursor;              


    }

enter code here
4

1 回答 1

0

我认为您的问题是您在 SQLiteOpenHelper 子类DML中的方法中有不正确的语句。onCreate()

这里:

"CREATE TABLE " + TABLE_NAME + " (" 
                + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + TIME + " TEXT NOT NULL," 
                + DATE + " TEXT NOT NULL" 
                + DESCRIPTION + " TEXT NOT NULL"
                + DETAILS + " TEXT NOT NULL"

您在日期和描述列以及语句末尾的最后一个右括号后面缺少逗号。这就是为什么永远不会创建您的数据库的原因。尝试修复它,它应该可以工作。

所以这里是更正的声明:

"CREATE TABLE " + TABLE_NAME + " (" 
                    + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + TIME + " TEXT NOT NULL," 
                    + DATE + " TEXT NOT NULL," 
                    + DESCRIPTION + " TEXT NOT NULL,"
                    + DETAILS + " TEXT NOT NULL)"
于 2013-03-24T22:28:51.650 回答