2011-04-07 30 views
-1

everyone 我是Android新手。我的應用程序有什麼問題? nullpointerExcepttion?

我的應用程序出現錯誤。

這是我捕獲的錯誤圖片。 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); 
     } 


} 
+6

OMG - 太多的代碼tp讀取,格式不正確。如果你得到一個NPE,堆棧跟蹤將會有引發異常的行號。找到它,在一個顯示行號的IDE中打開你的代碼,轉到那行,並盯着它,直到找出那行上的哪個對象可能爲空。 – duffymo 2011-04-07 09:52:45

+0

-1因爲你沒有嘗試自己調試它。此網站不是用於調試您的整個代碼,而是用於幫助解決問題,因爲您必須首先確定問題或至少查明問題。 – 2011-04-07 10:03:43

回答

0

db可能爲空在fetchNote() ..也許你沒有一起open()調用打開數據庫?

0

根據您的堆棧跟蹤,它看起來像dbfetchNote()中爲空,因爲您還沒有調用open()。沒有其他方法出現,它會導致NullPointerException

0

你可以嘗試把一個明確nullcheck在fetchnote方法:

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; 

    } 

但打開的方法似乎是初始化「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; 
    } 
0

空指針在未來fetchNote方法將對DBAdapter類: 光標將返回空值有..

檢查的準確代碼行: DBAdapter行號。 192

相關問題