我的申请有什么问题?空指针异常?

时间:2011-04-07 09:49:43

标签: java android

大家 我是Android的新手。

我的申请中有错误。

这是我捕获的错误图片。 enter link description here

你能帮我解决一下吗?

非常感谢。

这是我的代码:

public class DBAdapter 
{
    public static final String UPDATEDATE = "UpdateDate"; 
    //Declare fields in PersonInfo
    public static final String ROWID = "_id";

    public static final String PT_FirstName = "FirstName";
    public static final String PT_SecondName = "SecondName";
    public static final String PT_Gender = "Gender";
    public static final String PT_PatientId = "PatientId";
    public static final String PT_PeopleId = "PeopleId";
    public static final String PT_TreatmentRight = "TreatmentRight";
    public static final String PT_Birthday = "Birthday";
    public static final String PT_Address = "Address";
    public static final String PT_HomePhone = "HomePhone";
    public static final String PT_CellPhone = "CellPhone";
    public static final String PT_Email = "Email";
    public static final String PT_ContactName = "ContactName";
    public static final String PT_ContactPhone = "ContactPhone";
    public static final String PT_Occupation = "Occupation";
    public static final String PT_BloodType ="BloodType";
    public static final String PT_Prefix = "Prefix";
    public static final String PT_Province = "Province";
    public static final String PT_PatientType = "PatientType";  
    private static final String DATABASE_CREATE =
        "create table PatientInfo (_id integer primary key autoincrement, "
        + "PatientId text ,PatientType text , Prefix text , Gender text, "
        + "FirstName text, SecondName text, PeopleId text, TreatmentRight text, "
        + "Birthday text, BloodType text, Occupation text, Address text, "
        + "Province text, HomePhone text, CellPhone text, Email text, "
        + "ContactName text, ContactPhone text);"; 
    private static final String DATABASE_NAME = "clinic.db";
    public static final String tbPatient = "PatientInfo";
    private static final int DATABASE_VERSION = 2;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        DBHelper = new DatabaseHelper(ctx);        
    }


    //start database helper class
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            //nothing for now
        }
    }    
    //end database helper class

    //---opens the database---
    public DBAdapter open() throws SQLiteException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        db.close();
        DBHelper.close();
    }

    public Cursor getPersonList(){
        return db.query(tbPatient, new String[] {
                ROWID,
                PT_PatientId, 
                PT_PatientType, 
                PT_Prefix,
                PT_Gender,
                PT_FirstName,
                PT_SecondName,
                PT_PeopleId,
                PT_TreatmentRight,
                PT_Birthday,
                PT_BloodType,
                PT_Occupation,
                PT_Address,
                PT_Province,
                PT_HomePhone,
                PT_CellPhone,
                PT_Email,
                PT_ContactName,
                PT_ContactPhone}, 
                null, 
                null, 
                null, 
                null, 
                null,
                null);
    }   

    public long insertPerson(String patientid,String patienttype,String prefix,String gender,String firstname,
            String secondname,String peopleid,String treatmentright,String birthday,
            String bloodtype,String occupation,String address,String province,
            String homephone,String cellphone,String email,String contactname,
            String contactphone) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(PT_PatientId,patientid); 
        initialValues.put(PT_PatientType,patienttype); 
        initialValues.put(PT_Prefix,prefix); 
        initialValues.put(PT_Gender,gender); 
        initialValues.put(PT_FirstName,firstname); 
        initialValues.put(PT_SecondName,secondname); 
        initialValues.put(PT_PeopleId,peopleid); 
        initialValues.put(PT_TreatmentRight,treatmentright); 
        initialValues.put(PT_Birthday,birthday); 
        initialValues.put(PT_BloodType,bloodtype); 
        initialValues.put(PT_Occupation,occupation); 
        initialValues.put(PT_Address,address); 
        initialValues.put(PT_Province,province); 
        initialValues.put(PT_HomePhone,homephone);
        initialValues.put(PT_CellPhone,cellphone);
        initialValues.put(PT_Email,email);
        initialValues.put(PT_ContactName,contactname);
        initialValues.put(PT_ContactPhone,contactphone);
        return db.insert(tbPatient, null, initialValues);
     }

    public boolean updatePerson(long recordid, String patientid,String patienttype,String prefix,String gender,String firstname,
                                String secondname,String peopleid,String treatmentright,String birthday,
                                String bloodtype,String occupation,String address,String province,
                                String homephone,String cellphone,String email,String contactname,
                                String contactphone) {
        ContentValues args = new ContentValues();

        args.put(PT_PatientId,patientid); 
        args.put(PT_PatientType,patienttype); 
        args.put(PT_Prefix,prefix); 
        args.put(PT_Gender,gender); 
        args.put(PT_FirstName,firstname); 
        args.put(PT_SecondName,secondname); 
        args.put(PT_PeopleId,peopleid); 
        args.put(PT_TreatmentRight,treatmentright); 
        args.put(PT_Birthday,birthday); 
        args.put(PT_BloodType,bloodtype); 
        args.put(PT_Occupation,occupation); 
        args.put(PT_Address,address); 
        args.put(PT_Province,province); 
        args.put(PT_HomePhone,homephone);
        args.put(PT_CellPhone,cellphone);
        args.put(PT_Email,email);
        args.put(PT_ContactName,contactname);
        args.put(PT_ContactPhone,contactphone);
        return db.update(tbPatient, args, 
              ROWID + "=" + recordid, null) > 0;
    }

    public boolean deletePerson(long recordid) 
    {
        return db.delete(tbPatient, ROWID + 
                "=" + recordid, null) > 0;
    }



    public Cursor fetchNote(long rowId) throws SQLException {

        Cursor mCursor =

                db.query(tbPatient, new String[] {
                        ROWID,
                        PT_PatientId, 
                        PT_PatientType, 
                        PT_Prefix,
                        PT_Gender,
                        PT_FirstName,
                        PT_SecondName,
                        PT_PeopleId,
                        PT_TreatmentRight,
                        PT_Birthday,
                        PT_BloodType,
                        PT_Occupation,
                        PT_Address,
                        PT_Province,
                        PT_HomePhone,
                        PT_CellPhone,
                        PT_Email,
                        PT_ContactName,
                        PT_ContactPhone}, ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }


}






//////////////////////////////////////////////////////////////////////////////////////


public class PatientInfo extends Activity{
    private TextView firstNameEdt;
    private TextView patientIdEdt;
    private TextView lastNameEdt;
    private TextView peopleIdEdt;
    private TextView birthdayEdt;
    private TextView addressEdt;
    private TextView homePhoneEdt;
    private TextView cellPhoneEdt;
    private TextView emailEdt;
    private TextView contactNameEdt;
    private TextView contactPhoneEdt;
    private TextView GenderEdt;
    private TextView OccupationEdt;
    private TextView BloodTypeEdt;
    private TextView PrefixEdt;
    private TextView ProvinceEdt;
    private TextView TreatmentRightEdt;
    private TextView PatientTypeEdt ;
    private DBAdapter dbPatient;
    private Button exitBtn;
    private Button editBtn;

    private Button addAppointmentBtn;
    private Long rowId;
    String S_PatientType=" ";
    String S_Gender=" ";
    String S_Occupation=" ";
    String S_BloodType=" ";
    String S_Prefix=" ";
    String S_Province=" ";
    String S_TreatmentRight=" ";
    String S_Birthday=" ";
    String S_PatientId=" ";
    String S_FirstName=" ";
    String S_SecondName=" ";
    String S_PeopleId=" ";
    String S_Address=" ";
    String S_HomePhone=" ";
    String S_CellPhone=" ";
    String S_Email=" ";
    String S_ContactName =" ";
    String S_ContactPhone =" ";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbPatient = new DBAdapter(this);
        dbPatient.open();
        setContentView(R.layout.patientinfo);
        setBinding();


        rowId = savedInstanceState != null ? savedInstanceState.getLong(DBAdapter.ROWID) 
                : null;
if (rowId == null) {
Bundle extras = getIntent().getExtras();            
rowId = extras != null ? extras.getLong(DBAdapter.ROWID) 
        : null;
}

populateFields();



       // dataPreparation();
        setEvent();


}



    private void populateFields() {
        if (rowId != null) {
            Cursor note = dbPatient.fetchNote(rowId);
            startManagingCursor(note);
            firstNameEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_FirstName)));
            lastNameEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_SecondName)));
            GenderEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_Gender)));
            patientIdEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_PatientId)));
            peopleIdEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_PeopleId)));
            TreatmentRightEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_TreatmentRight)));
            birthdayEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_Birthday)));

            addressEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_Address)));

            homePhoneEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_HomePhone)));

            cellPhoneEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_CellPhone)));

            emailEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_Email)));

            contactNameEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_ContactName)));

            contactPhoneEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_ContactPhone)));

            OccupationEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_Occupation)));

            BloodTypeEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_BloodType)));

            PrefixEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_Prefix)));

            ProvinceEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_Province )));

            PatientTypeEdt.setText(note.getString(
                    note.getColumnIndexOrThrow(DBAdapter.PT_PatientType)));
        }
    }

    private void setBinding(){
         editBtn=(Button) findViewById(R.id.edit);
         exitBtn=(Button) findViewById(R.id.exit);
         dbPatient =  new DBAdapter(this);
         addAppointmentBtn=(Button) findViewById(R.id.addAppointment);
         PatientTypeEdt = (TextView) findViewById(R.id.patient_Type);
         GenderEdt = (TextView) findViewById(R.id.gender);
         OccupationEdt = (TextView) findViewById(R.id.occupation);
         BloodTypeEdt = (TextView) findViewById(R.id.blood_Type);
         PrefixEdt = (TextView) findViewById(R.id.prefix);
         ProvinceEdt = (TextView) findViewById(R.id.province);
         TreatmentRightEdt = (TextView) findViewById(R.id.treatment_Right);
         birthdayEdt=(TextView) findViewById(R.id.birthday);
         patientIdEdt = (TextView) findViewById(R.id.patient_id);
         firstNameEdt = (TextView) findViewById(R.id.first_Name);
         lastNameEdt= (TextView) findViewById(R.id.last_Name);
         peopleIdEdt = (TextView) findViewById(R.id.id2);
         addressEdt = (TextView) findViewById(R.id.address);
         homePhoneEdt = (TextView) findViewById(R.id.home_Phone);
         cellPhoneEdt = (TextView) findViewById(R.id.cell_Phone);
         emailEdt = (TextView) findViewById(R.id.email);
         contactNameEdt = (TextView) findViewById(R.id.contact_Name);
         contactPhoneEdt = (TextView) findViewById(R.id.contact_Phone);

     } 


     private void setEvent() {
            // TODO Auto-generated method stub
         addAppointmentBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent showLinearLayout = new Intent(PatientInfo.this,AddAppointment.class);
                    startActivity(showLinearLayout);
                }
            });





         exitBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent showLinearLayout = new Intent(PatientInfo.this,MainActivity.class);
                    startActivity(showLinearLayout);
                }
            });

         editBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent showLinearLayout = new Intent(PatientInfo.this,AddPatient.class);
                    startActivity(showLinearLayout);
                }
            });

}


//////////////////////////////////////////////////////////////////////////////////////
public class SearchPatient extends ListActivity{
    private DBAdapter dbPatient;
    private EditText appointmentDateEdt;
    private Spinner typeSelectSpn;
    private Button exitBtn;
    private Button searchBtn;
    private Cursor personListCursor;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchpatient);
        setBinding();
        dataPreparation();
        listData();
        setEvent();

}
     private void setBinding(){
         typeSelectSpn = (Spinner) findViewById(R.id.search_Type);
         exitBtn=(Button) findViewById(R.id.exit);
         searchBtn=(Button) findViewById(R.id.search);
         dbPatient =  new DBAdapter(this);
     } 


     private void setEvent() {
            // TODO Auto-generated method stub
         searchBtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent showLinearLayout = new Intent(SearchPatient.this,PatientInfo.class);
                    startActivity(showLinearLayout);
                    Bundle b = new Bundle();
                    b.putLong("ROWID", 0);
                    showLinearLayout.putExtras(b);
                    startActivity(showLinearLayout);
                }

            });

        }


     private void listData(){
         dbPatient.open();
            personListCursor = dbPatient.getPersonList();
            startManagingCursor(personListCursor);
            String[] from = new String[] { DBAdapter.PT_PatientId,DBAdapter.PT_FirstName, DBAdapter.PT_SecondName,DBAdapter.PT_PatientType };
            int[] to = new int[] { R.id.patient_id2, R.id.firstname2, R.id.lastname2, R.id.patient_type2};

            // Now create an array adapter and set it to display using our row
            SimpleCursorAdapter personList =
                new SimpleCursorAdapter(this, R.layout.datarow, personListCursor, from, to){

            };
            setListAdapter(personList);
            //dbPatient.close();
        }


     @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Intent i = new Intent(this, PatientInfo.class);
            i.putExtra(DBAdapter.ROWID, id);
            startActivity(i);
        }







     private void dataPreparation()

        {
            ArrayAdapter<CharSequence> Adapter1 = ArrayAdapter.createFromResource(this, R.array.search, 
                        android.R.layout.simple_spinner_item);     
                        Adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        typeSelectSpn.setAdapter(Adapter1);
        }


}

4 个答案:

答案 0 :(得分:0)

db

fetchNote()可能为空。也许您没有通过调用open()打开数据库?

答案 1 :(得分:0)

根据您的堆栈跟踪,db中的fetchNote()似乎为空,因为您尚未调用open()。该方法中没有任何其他内容会显示会导致NullPointerException

答案 2 :(得分:0)

您可以尝试在fetchnote方法中进行显式的null检查:

public Cursor fetchNote(long rowId) throws SQLException {
     if(db==null){
        return null;
     }
        Cursor mCursor =

                db.query(tbPatient, new String[] {
                        ROWID,
                        PT_PatientId, 
                        PT_PatientType, 
                        PT_Prefix,
                        PT_Gender,
                        PT_FirstName,
                        PT_SecondName,
                        PT_PeopleId,
                        PT_TreatmentRight,
                        PT_Birthday,
                        PT_BloodType,
                        PT_Occupation,
                        PT_Address,
                        PT_Province,
                        PT_HomePhone,
                        PT_CellPhone,
                        PT_Email,
                        PT_ContactName,
                        PT_ContactPhone}, ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

但是你的open方法似乎初始化了“db”。也许它不叫?或者DBHelper.getWritableDatabase()可能返回null?

我还建议如下:

 //---opens the database---
    public DBAdapter open() throws SQLiteException
    {
        db = DBHelper.getWritableDatabase();
        if(db==null)
            throw new RuntimeException("Database could not be opened");
        return this;
    }

答案 3 :(得分:0)

NullPointer进入 DBAdapter 类中的 fetchNote 方法: 光标在那里返回null ..

检查确切的代码行: DBAdapter行号。 192