0

I'm trying ti insert a tuple (record) on a sqlite db. it has one integer primary key. but when I try to insert I got an error "null object reference". I searched a lot but unfortunately I can't resolved the error with respect to my case. Would you please give me hand?

here are the codes:

and the logcat says there is fatal exception.

this is how I created the the table. my sql insert function works correctly , but I think there is a problem with the datatype I have.

public class AccountHolder_to implements Parcelable {

    private  Integer A_A_accountNumber;
    private  String A_email;
    private  String A_username;
    private  String A_password;
    private  String A_syncedURL;

    private  Demographics_to A_A_patientID;

    public AccountHolder_to () {
        super();
    }

    private AccountHolder_to(Parcel in) {
        super();
        this.A_A_accountNumber= in.readInt();
        this.A_email = in.readString();
        this.A_username = in.readString();
        this.A_password = in.readString();
        this.A_syncedURL = in.readString();

        this.A_A_patientID = in.readParcelable(Demographics_to.class.getClassLoader());

    }

    public int getA_A_accountNumber() {return A_A_accountNumber;}

    public void setA_A_accountNumber(int id) {
        this.A_A_accountNumber = id;
    }

    public String getA_email() {
        return A_email;
    }

    public void setA_email(String email) {
        this.A_email = email;
    }

    public String getA_username() {
        return A_username;
    }

    public void setA_username(String un) {
        this.A_username = un;
    }

    public String getA_password() {
        return A_password;
    }

    public void setA_password(String pw) {
        this.A_password = pw;
    }

    public String getA_syncedURL() {
        return A_syncedURL;
    }

    public void setA_syncedURL(String sr) {
        this.A_syncedURL = sr;
    }

    public Demographics_to getA_A_patientID() {
        return A_A_patientID;
    }

   public void setA_A_patientID(Demographics_to pi) {
       this.A_A_patientID = pi;
   }

    @Override
    public String toString() {
        return "accountHolder_Table [A_accountNumber:" + A_A_accountNumber + ", name:" + A_email
                + ",username:" + A_username + "password:" + A_password
                + "syncedurl:" +A_syncedURL +"patientid:" + A_A_patientID+ "]";
    }

    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeInt(getA_A_accountNumber());
        parcel.writeString(getA_email());
        parcel.writeString(getA_username());
        parcel.writeString(getA_password());
        parcel.writeString(getA_syncedURL());

        parcel.writeParcelable(getA_A_patientID(), flags);
    }

    public static final Parcelable.Creator<AccountHolder_to> CREATOR = new Parcelable.Creator<AccountHolder_to>() {
        public AccountHolder_to createFromParcel(Parcel in) {
            return new AccountHolder_to(in);
        }

        public AccountHolder_to[] newArray(int size) {
            return new AccountHolder_to[size];
        }
    };

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + A_A_accountNumber;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        AccountHolder_to other = (AccountHolder_to) obj;
        if (A_A_accountNumber != other.A_A_accountNumber)
            return false;
        return true;
    }
}

Thank you in advance !

Jasmine
  • 222
  • 2
  • 12

1 Answers1

2

Well thanks for your comments , Luckily I could fix the error by doing the following:

firstly, as you told I changed the definition for the primary key like this:

private  int A_A_accountNumber;

it was Integer , and cause problem since it's a reference type and I had to create an object for this. But I simply changed it to "int" and it worked.

secondly, in AccountHolderDAO, which I created for CRUD operations, I changed the insert operation like this:

public int save(AccountHolder_to acchold) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(DataBaseHelper.email, acchold.getA_email());
    values.put(DataBaseHelper.username, acchold.getA_username());
    values.put(DataBaseHelper.password, acchold.getA_password());
    values.put(DataBaseHelper.syncedURL, acchold.getA_syncedURL());
    values.put(DataBaseHelper.A_patientID, "");

    // Inserting Row
    long acc_Id = db.insert(dbHelper.accountHolder_Table, null, values);
    db.close(); // Closing database connection
    return (int) acc_Id;
}

Another error was because I was trying to send a value and call the "get" method for a primary key which is basically "AUTOINCREMENT" . Actually I simply do not pass anything.

Jasmine
  • 222
  • 2
  • 12