2017-07-26 53 views
-1

我看過這麼多的解決方案和應用所有,但仍然在我的代碼中得到空指針異常。這裏是我的代碼,請儘可能快地看看和回覆。提前致謝。在android中的數據庫保存查詢Java.lang.nullpointerexception

DBHandler.java類代碼

public class DBHandler extends SQLiteOpenHelper { 
//Database Version 
private static final int DATABASE_VERSION=1; 
// Database Name 
private static final String DATABASE_NAME="Patient.db"; 
//Table Name 
private static final String TABLE_NAME="patientDetail"; 

//Table Columns Names 
private static final String ID="id"; 
private static final String NAME="name"; 
private static final String ADDRESS="address"; 
private static final String PHONE_NUMBER="phone_number"; 

//Constructor 

public DBHandler(Context context) 
{ 
    super(context,DATABASE_NAME,null,DATABASE_VERSION); 
} 

//Creating Tables 

@Override 
public void onCreate(SQLiteDatabase db) { 

    String CREATE_PATIENT_DETAIL_TABLE="CREATE TABLE "+TABLE_NAME+"(" 
      + ID+" INTEGER PRIMARY KEY AUTOINCREMENT," 
      +NAME+" TEXT," 
      +ADDRESS +" TEXT," 
      +PHONE_NUMBER + " TEXT)" ; 

    db.execSQL(CREATE_PATIENT_DETAIL_TABLE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    //Drop older tables if exist 

    db.execSQL("DROP TABLE IF EXIST "+TABLE_NAME); 

    /// Create Table again 
    onCreate(db); 
} 
public boolean insertData(String name,String address, String ph_no) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values= new ContentValues(); 
    values.put(NAME,name); 
    values.put(ADDRESS,address); 
    values.put(PHONE_NUMBER,ph_no); 
    long result = db.insert(TABLE_NAME,null,values); 
    if(result == -1) 
     return false; 
    else 
     return true; 

} 
} 


//insertPatient.java Class Code 

public class insertPatient extends Fragment { 

DBHandler mydb=null; 

EditText editname,editaddress,editph_no; 

Button btn_adddata; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.insert_patient, container, false); 

    mydb = new DBHandler(getActivity()); 

    editname=(EditText) rootView.findViewById(R.id.insert_name); 
    editname=(EditText) rootView.findViewById(R.id.insert_address); 
    editname=(EditText) rootView.findViewById(R.id.insert_ph_no); 
    btn_adddata=(Button) rootView.findViewById(R.id.insert_button); 
    btn_adddata.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      adddata(); 
     } 
    }); 
    return rootView; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mydb = new DBHandler(getActivity()); 
} 
public void adddata() 
{ 
    mydb = new DBHandler(getActivity()); 
    Toast.makeText(getActivity(), "working button", Toast.LENGTH_LONG).show(); 
    try { 

     boolean isinserted = mydb.insertData(editname.getText().toString(), editaddress.getText().toString(), editph_no.getText().toString()); 

     if (isinserted) { 
      Toast.makeText(getActivity(), "data saved", Toast.LENGTH_LONG).show(); 
     } else { 
      Toast.makeText(getActivity(), "data Not saved", Toast.LENGTH_LONG).show(); 
     } 
    } 
    catch (Exception e) { 
     Toast.makeText(getActivity(), String.valueOf(e), Toast.LENGTH_LONG).show(); 
    } 
} 
} 

我爲dbhandler對象的initializtion appllied這麼多的解決方案,如任一按鈕被初始化與否,getactivity(),的getContext()和getbasecontext(),還提出了許多更改我的數據庫處理程序與不同的解決方案,但仍然有同樣的錯誤。我沒有檢查我的解決方案的活動,因爲我使用片段類,所以我只在它嘗試過。

+4

提供堆棧跟蹤! –

+0

請添加您的logcat請 –

+0

我是新的數據庫,不知道如何添加logcat以及如何獲取堆棧跟蹤。謝謝 –

回答

2

你的問題是你所得到的editText被分配到編輯名稱,而不是你需要的,並且你正在使用他們的getText方法。

在你insertPatient類:

editname=(EditText) rootView.findViewById(R.id.insert_name); 
editname=(EditText) rootView.findViewById(R.id.insert_address); 
editname=(EditText) rootView.findViewById(R.id.insert_ph_no); 

到:

editname=(EditText) rootView.findViewById(R.id.insert_name); 
editaddress=(EditText) rootView.findViewById(R.id.insert_address); 
editph_no=(EditText) rootView.findViewById(R.id.insert_ph_no); 
+0

非常感謝你伊萬。 –

+0

考慮接受答案,如果它解決了你的問題。 –

+0

謝謝@米格爾斯拉,你是對的。如果它解決了你的問題,請考慮接受。 – Ivan

-1

你還沒有初始化editaddresseditph_no。您只用diff ID初始化editname三次。 見下面的一行代碼中的

editname=(EditText) rootView.findViewById(R.id.insert_name); 
editname=(EditText) rootView.findViewById(R.id.insert_address); 
editname=(EditText) rootView.findViewById(R.id.insert_ph_no); 

您從editaddress和editph_no中獲得價值,並拋出異常

boolean isinserted = mydb.insertData(editname.getText().toString(), editaddress.getText().toString(), editph_no.getText().toString()); 

編輯線

editname=(EditText) rootView.findViewById(R.id.insert_name); 
editaddress =(EditText) rootView.findViewById(R.id.insert_address); 
editph_no =(EditText) rootView.findViewById(R.id.insert_ph_no); 
+0

感謝Mahesh Gawhane。 –