mardi 29 décembre 2015

java.lang.NullPointerException when adding data to sqlite (because of RadioGroup)

inserting data to SQLite was working until i decided to add a RadioGroup. When running i received:

     12-29 11:52:19.948: E/AndroidRuntime(21086): java.lang.NullPointerException
12-29 11:52:19.948: E/AndroidRuntime(21086):    at com.example.appointapp.PatientFragment$1.onClick(PatientFragment.java:66)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at android.view.View.performClick(View.java:4438)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at android.view.View$PerformClick.run(View.java:18422)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at android.os.Handler.handleCallback(Handler.java:733)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at android.os.Looper.loop(Looper.java:136)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at android.app.ActivityThread.main(ActivityThread.java:5017)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at java.lang.reflect.Method.invokeNative(Native Method)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at java.lang.reflect.Method.invoke(Method.java:515)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-29 11:52:19.948: E/AndroidRuntime(21086):    at dalvik.system.NativeStart.main(Native Method)

PatientFragement.java code:

package com.example.appointapp;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class PatientFragment extends Fragment {
    DatabaseHelper myDB;
    EditText editName,editFamilyname,editDob,editAddress,editPhonenb,editEmail;
    Button btnSave, selectedRadioButton; 
    RadioButton radioB;
    RadioGroup radioG;
    private Patient mPatient;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPatient= new Patient();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_patient, parent, false);
    myDB = new DatabaseHelper(getActivity());
    editName = (EditText)v.findViewById(R.id.name_text);
    editFamilyname = (EditText)v.findViewById(R.id.familyname_text);
    editDob = (EditText)v.findViewById(R.id.date_text);
    editAddress = (EditText)v.findViewById(R.id.address_text);
    editPhonenb = (EditText)v.findViewById(R.id.phone_text);
    editEmail = (EditText)v.findViewById(R.id.email_text);
    btnSave = (Button)v.findViewById(R.id.save_button);
    radioG = (RadioGroup)v.findViewById(R.id.myRadioGroup);
    int selectedId = radioG.getCheckedRadioButtonId();
    if(selectedId != -1) {    
           selectedRadioButton = (RadioButton)v.findViewById(selectedId);

        }



    AddData();

    return v;
    }


    public void AddData() {
        btnSave.setOnClickListener(
                new View.OnClickListener(){
            @Override
                    public void onClick(View v){

            boolean isInserted = myDB.insertData(editName.getText().toString() ,
                        editFamilyname.getText().toString() ,
                        editDob.getText().toString() ,
                        editAddress.getText().toString() ,
                        editPhonenb.getText().toString() , 
                        editEmail.getText().toString(),
                        selectedRadioButton.getText().toString()) ;
            if(isInserted == true)
            Toast.makeText(getActivity(), "Patient inserted", Toast.LENGTH_LONG).show();
            else 
                Toast.makeText(getActivity(), "Patient not inserted", Toast.LENGTH_LONG).show();
                                               }
                }
                                       );

    }   
}

and fragment_patient.xml code:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/name_text"
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:ems="10"
            android:hint="@string/name_text_hint" />

        <EditText
            android:id="@+id/familyname_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:hint="@string/familyname_text_hint" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/date_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:ems="10"
            android:hint="@string/date_text_hint" >
        </EditText>
    </LinearLayout>

    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:checkedButton="@+id/male_text" >

        <RadioButton
            android:id="@+id/female_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female_text" />

        <RadioButton
            android:id="@+id/male_male_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male_text" />
    </RadioGroup>

    <EditText
        android:id="@+id/address_text"
        android:layout_width="348dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/address_text_hint" />

    <LinearLayout
        android:layout_width="340dp"
        android:layout_height="57dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/phone_text"
            android:layout_width="143dp"
            android:layout_height="wrap_content"
            android:hint="@string/phone_text_hint" />

        <EditText
            android:id="@+id/email_text"
            android:layout_width="213dp"
            android:layout_height="wrap_content"
            android:hint="@string/email_text_hint" />
    </LinearLayout>

    <Button
        android:id="@+id/save_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/save_button" />

</LinearLayout>


and databasehelper.java code:

package com.example.appointapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME="appo.db";
    public static final String TABLE_NAME = "patients_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "FAMILYNAME";
    public static final String COL_4 = "DOB";
    public static final String COL_5 = "ADDRESS";
    public static final String COL_6 = "PHONENUMBER";
    public static final String COL_7 = "EMAIL";
    public static final String COL_8 = "GENDER";

    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL( "create table "+ TABLE_NAME + "( ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, FAMILYNAME TEXT, DOB TEXT, ADDRESS TEXT, PHONENUMBER TEXT, EMAIL TEXT, GENDER TEXT )" );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);

    }

    public boolean insertData(String name, String familyname, String dob, String address, String phonenumber, String email, String gender) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,familyname);
        contentValues.put(COL_4,dob);
        contentValues.put(COL_5,address);
        contentValues.put(COL_6,phonenumber);
        contentValues.put(COL_7,email);
        contentValues.put(COL_8,gender);
        long result = db.insert(TABLE_NAME, null , contentValues);
        if(result == -1)
            return false;
        else 
            return true;
}

}

Please help am a new programmer. Note that my application was working before trying to add the result of the RadioGroup to the database. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire