com.examples.AddAppointment.addEvent(AddAppointment.java:125)中的空指针异常

时间:2013-03-24 21:01:08

标签: java android sqlite nullpointerexception

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

 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");
    }
       }

AddAppointment Class

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

1 个答案:

答案 0 :(得分: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)"