0

I`m trying to update my table but I am getting ERROR SELECTING DB: Invalid int: "null" Here is my codes

public class DatabaseUtility extends SQLiteAssetHelper{

public static final String DATABASE = "contacts.db";
public static final String TABLE = "contacts";
public static final String C1_NAME = "name";
public static final String C2_PHONE = "phone";
public static final String C3_LOCATE_TOP = "locateTop";
public static final String C4_RATING = "rating";

public DatabaseUtility(Context context) {
    super(context, DATABASE, null, 1);
 }

}

Database Access Class Update Function

  public void updateContact(String name, String oldPhone,String newPhone, int locateTop, int rating){
        try {
            this.open();
            String updateWhere = String.format("%s=%s",this.databaseUtility.C2_PHONE, oldPhone);
            ContentValues contentValues = new ContentValues();
            contentValues.put(this.databaseUtility.C1_NAME, name);
            contentValues.put(this.databaseUtility.C2_PHONE,newPhone);
            contentValues.put(this.databaseUtility.C3_LOCATE_TOP, locateTop);
            contentValues.put(this.databaseUtility.C4_RATING, rating);
            this.sqLiteDatabase.update(this.databaseUtility.TABLE, contentValues, updateWhere,null);
            this.close();

        }catch (Exception e){
            Log.e("Error Update Contact",e.getMessage());
        }
    }

Contact Utility Class Update Function

 public void updateContact(String name, String oldPhone, String newPhone, boolean locateTop, int rating){
    int lt = locateTop ? 1 : 0;
    this.databaseAccess.updateContact(name, oldPhone, newPhone, lt, rating);
}

Update Contact Activity

public class UpdateContactActivity extends AppCompatActivity {

private ContactUtility contactUtility;
private EditText txtName, txtPhone;
private Button btnUpdate;
private CheckBox chkTop;
private RatingBar ratingContact;
private Contact contact;
private  String oldPhone;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.add_contact);
    getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
    ContactUtility contactUtility = new ContactUtility(getApplicationContext());
    this.contact =(Contact) getIntent().getSerializableExtra(Requests.EXTRA_CONTACT);
    this.oldPhone = this.contact.getPhone();
    this.init();
}


private void init(){
    this.txtName    = (EditText) findViewById(R.id.txtName);
    this.txtName.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
    this.txtName.setText(this.contact.getName());



    this.txtPhone   = (EditText) findViewById(R.id.txtPhone);
    this.txtPhone.setText(this.contact.getPhone());

    this.chkTop = (CheckBox) findViewById(R.id.chkTop);
    this.chkTop.setChecked(this.contact.isLocateTop());

    this.ratingContact = (RatingBar) findViewById(R.id.ratingContact);


    if (chkTop.isChecked()){
        this.ratingContact.setEnabled(true);
        this.ratingContact.setRating(contact.getRating());
    }

    else{
        this.ratingContact.setEnabled(false);
    }


    this.chkTop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (chkTop.isChecked()) {
                ratingContact.setEnabled(true);
                ratingContact.setRating(contact.getRating());
            }
            else {
                ratingContact.setEnabled(false);
                ratingContact.setRating(0);
            }
        }
    });


    this.btnUpdate    = (Button) findViewById(R.id.btnSave);
    this.btnUpdate.setText(R.string.update_contact);
    this.btnUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // update
            String name = txtName.getText().toString();
            String phone = txtPhone.getText().toString();

            if (chkTop.isChecked()){
                float rate = ratingContact.getRating();
                contactUtility.updateContact(name,oldPhone, phone, true,(int)rate); // error 
            }else{
                contactUtility.updateContact(name,oldPhone, phone, false, 0); // error
            }


            String message = String.format("%s is updated",name );
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }
    });

}

}

Error

06-29 11:45:21.802 13556-13556/com.umitkas.contacts E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.umitkas.contacts, PID: 13556
                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.umitkas.contacts.ContactUtility.updateContact(java.lang.String, java.lang.String, java.lang.String, boolean, int)' on a null object reference
                                                                      at com.umitkas.contacts.UpdateContactActivity$2.onClick(UpdateContactActivity.java:95)
                                                                      at android.view.View.performClick(View.java:5198)
                                                                      at android.view.View$PerformClick.run(View.java:21147)
                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-29 11:45:25.570 13788-13788/com.umitkas.contacts E/ERROR SELECTING DB: Invalid int: "null"

Program is running out after the clicking button, How can i fix my error

umtkas
  • 62
  • 9
  • On what line does this error occur? Is `this.databaseUtility.TABLE` the right value? Should this not be `DatabaseUtility.TABLE`, as it is static? – Marco7757 Jun 29 '16 at 12:05
  • @Marco7757 Basically it does not give specific line for updating code it occurs `contactUtility.updateContact(name,oldPhone, phone, true,(int)rate); ` here also this refers Database Access Class Update Function – umtkas Jun 29 '16 at 12:10

1 Answers1

1

Judging by your error message and your code, I'd say that the variably contactUtility is never instantiated and thus a null object. You cannot call a function from a null object, obviously.

You have to instantiate contactUtility before using it:

ContactUtility contactUtility = new ContactUtility();

This can be done anywhere before first usage (either in the very line you create the variable or in the init() function.

Marco7757
  • 735
  • 9
  • 16
  • You are right it was a mistake but still I am getting `java.lang.NullPointerException: Attempt to invoke virtual method 'void com.umitkas.contacts.ContactUtility.updateContact(java.lang.String, java.lang.String, java.lang.String, boolean, int)' `on a null object reference``this error after I instantiate contactUtility – umtkas Jun 29 '16 at 12:29
  • Are you initializing BEFORE calling it? – Marco7757 Jun 29 '16 at 12:32
  • @Marci7757 ofcourse, I initialized it in onCreate Method – umtkas Jun 29 '16 at 12:33
  • I'm sorry, I meant to say, are you initializing it before calling `init()`? – Marco7757 Jun 29 '16 at 12:34
  • i have edited code above, yes i have initialized it before init() – umtkas Jun 29 '16 at 12:35
  • Hm, I'm at a loss here. Can you debug? How does it get set to `null` after initializing? – Marco7757 Jun 29 '16 at 12:44
  • Ok, i have fixed problem with sending object to mainActivity with intent, Basically ContactUtility is initilized in there and it is working correctly for now. But I do not know why this error occured. – umtkas Jun 29 '16 at 12:59
  • Okay, glad it worked out! – Marco7757 Jun 29 '16 at 13:01