Android Null Pointer Exception on My Database [dup

2019-09-16 05:19发布

问题:

This question already has an answer here:

  • What is a NullPointerException, and how do I fix it? 12 answers

My insert function

public void AddBakim(){
    btn_bakimkaydet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            db.insertBakim("1","2","3");
        }
    });
}

My database function

 public boolean insertBakim(String bAd,String bFiyat,String bTarih){ /*String FK_aID*/
  SQLiteDatabase db = this.getWritableDatabase();
  ContentValues deger = new ContentValues();
  deger.put(COLUMN_bAdi,bAd);
  deger.put(COLUMN_bFiyat,bFiyat);
  deger.put(COLUMN_bTarih,bTarih);
  //deger.put(COLUMN_FK_aID,FK_aID);
  long sonuc = db.insert(TABLE_BAKIMLAR,null,deger);
  if (sonuc== -1)
      return false;
  else
      return true;
}

My other insert method doesn't work and is giving me the following error. Please help me friends.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
      at com.bakim.synergy.bakimson.BakimKayit.AddBakim(BakimKayit.java:42)
      at com.bakim.synergy.bakimson.BakimKayit.onCreate(BakimKayit.java:38)

回答1:

I think you have not initialized your button 'btn_bakimkaydet' . Please first initialize your button and then setOnClickListener on it.