-1

I have watched so many solution and applied all but still getting null pointer exception in my code. Here is my code kindly have a look and reply as fast as anyone can. Thanks in advance.

DBHandler.java Class code

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

I have appllied so many solutions like either button is initialized or not, getactivity(), getcontext() and getbasecontext() for initializtion of dbhandler object, also made many changes in my db handler with different solutions but still got the same error. I did not checked my solution for activity only as i am using fragment classes so i tried it only in it.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

2

Your problem is all your editText you are getting ids are assigned to editname instead of the ones you need, and you are using their getText method.

In your insertPatient class:

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

to:

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);
Ivan
  • 782
  • 11
  • 23
-1

you have not initialize editaddress and editph_no. you are only initializing editname for three times with diff ids. see below line in your code

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

your are getting value from editaddress and editph_no and it throws exception

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

edit that lines to

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);
Mahesh Gawhane
  • 348
  • 3
  • 20