0

In my case I have DbHandler.java,and ActivityKategori.java.

My problem is at DbHandler.java, I have query condition

SELECT * FROM TBL_**** WHERE COLUMN1 = 'VARIABEL_FROM_ACT_KATEGORI' "

and that is variable value is from bundle :D

I already make full of changes but nothing solved.

Here's the full code

ActivityKategori.java

This activity is get data from bundle and send it value to DBHandler to get sql CONDITION.

I know someone will suggest to using SharedPreference but im confused about it.

Right now, I want to learn passing using Bundle or Intents

package ptacs.ekatalog.com.e_katalogproduk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import ptacs.ekatalog.com.e_katalogproduk.R;
import ptacs.ekatalog.com.e_katalogproduk.adapter.KategoriAdapter;
import ptacs.ekatalog.com.e_katalogproduk.helper.Constant;
import ptacs.ekatalog.com.e_katalogproduk.helper.DBHandler;
import ptacs.ekatalog.com.e_katalogproduk.helper.RecyclerItemClickListener;
import ptacs.ekatalog.com.e_katalogproduk.model.Produk;

 public class ActivityKategori extends AppCompatActivity {

     private SwipeRefreshLayout swLayout2;
     private LinearLayout llayout2;
     private RecyclerView recyclerView;
     private LinearLayoutManager layoutManager;
     private KategoriAdapter adapter;
     private DBHandler dbHandler;
     private List<Produk> kategoriList = new ArrayList<>();
     private TextView tv1;
     String mMerkProduk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kategori);

        initRecyclerView();
        cekDataRecyclerView();


        if (getIntent().getExtras() != null) {
            Bundle bundle = getIntent().getExtras();
            mMerkProduk = bundle.getString(Constant.BUNDLE_MERK_PRODUK); //MERK kategori

        //Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle(mMerkProduk);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        dbHandler = new DBHandler(this);
        }

        //dbHandler.getKategoryProduk(mMerkProduk);
        }
     private void initRecyclerView(){

         recyclerView = (RecyclerView) findViewById(R.id.rv_kategori);
         recyclerView.setHasFixedSize(true);
         layoutManager = new LinearLayoutManager(this);
         recyclerView.setLayoutManager(layoutManager);
         dbHandler = new DBHandler(ActivityKategori.this);
         kategoriList = dbHandler.getKategoryProduk(mMerkProduk); //GET VALUE STRING 
         //kategoriList = dbHandler.getKategoryProduk(); //OLD CODE to GET OBJECT
         adapter = new KategoriAdapter(kategoriList);
         recyclerView.setAdapter(adapter);
         adapter.notifyDataSetChanged();
        }




     private void cekDataRecyclerView() {

         if (adapter.getItemCount() == 0) {
             recyclerView.setVisibility(View.GONE);
         } else {
             recyclerView.setVisibility(View.VISIBLE);

             recyclerView.addOnItemTouchListener(
                     new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
                         @Override
                         public void onItemClick(View view, int position) {
                             // TODO Handle item click

                             Bundle bundle = new Bundle();

                             //COMMIT MAS INDRA CS

                             bundle.putString(Constant.BUNDLE_JENIS_PRODUK, adapter.getItem(position).getJenis_produk());

                             Intent intent = new Intent(ActivityKategori.this, ActivityList.class);
                             intent.putExtras(bundle);
                             startActivity(intent);

                         }
                     })
             );
         }

         swLayout2 = (SwipeRefreshLayout) findViewById(R.id.sw_layout2);
         llayout2 = (LinearLayout) findViewById(R.id.ll_Layout);

         //Mengeset warna yang berputar
         swLayout2.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary);

         //Setting Listener yang akan dijalankan saat layar difresh
         swLayout2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {
                 refreshItem();
             }
             void refreshItem(){
                 initRecyclerView();
                 cekDataRecyclerView();
                 onItemLoad();
             }
             void onItemLoad(){
                 swLayout2.setRefreshing(false);
             }
         });
     }

     }

ActivityKategori.java

package ptacs.ekatalog.com.e_katalogproduk.helper;

/**
 * Created by Maxoto on 1/15/2018.
 */

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

import java.util.ArrayList;
import java.util.List;

import ptacs.ekatalog.com.e_katalogproduk.model.Produk;

public class DBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "db_ekatalog"; // NAMA DATABASE
    private static final String TABLE_PRODUK = "tb_produk"; // NAMA TABEL
    private static final String COLUMN_ID = "id_produk"; // NAMA KOLOM ID
    private static final String COLUMN_KD = "kd_produk"; // KODE PRODUK
    private static final String COLUMN_NAMA = "nama_produk"; // NAMA KOLOM NAMA
    private static final String COLUMN_MERK = "merk_produk"; //MERK PRODUK
    private static final String COLUMN_JENIS = "jenis_produk"; //JENIS PRODUK
    private static final String COLUMN_VARIASI = "variasi_produk"; //VARIASI PRODUK
    private static final String COLUMN_FOTO = "foto_produk"; // FOTO PRODUK

    public DBHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    // TODO : LANJUT SECTION 2
    // FUNGSI UNTUK MEMBUAT DATABASENYA
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_USER_TABLE = "CREATE TABLE "
                + TABLE_PRODUK +
                "(" + COLUMN_ID + " INTEGER PRIMARY KEY,"
                + COLUMN_KD + " TEXT,"
                + COLUMN_NAMA + " TEXT, "
                + COLUMN_MERK + " TEXT, "
                + COLUMN_JENIS + " TEXT, "
                + COLUMN_VARIASI + " TEXT, "
                + COLUMN_FOTO + " TEXT" + ")";
        db.execSQL(CREATE_USER_TABLE);
    }

    // FUNGSI UNTUK MENGECEK DATABASE ADA ATAU TIDAK.
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUK);
        onCreate(db);
    }

    // FUNGSI UNTUK TAMBAH DATA PRODUK
    public void tambahProduk(Produk produk){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_KD, produk.getKd_produk());
        values.put(COLUMN_NAMA, produk.getNama_produk());
        values.put(COLUMN_MERK, produk.getMerk_produk());
        values.put(COLUMN_JENIS, produk.getJenis_produk());
        values.put(COLUMN_VARIASI, produk.getVariasi_produk());
        values.put(COLUMN_FOTO, produk.getFoto_produk());

        db.insert(TABLE_PRODUK, null, values);
        db.close();
    }

    // FUNGSI UNTUK AMBIL 1 DATA PRODUK
    public Produk getProduk(int id_produk){
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_PRODUK, new String[]{COLUMN_ID, COLUMN_KD ,COLUMN_NAMA, COLUMN_MERK
                ,COLUMN_JENIS, COLUMN_VARIASI , COLUMN_FOTO },
                COLUMN_ID + "=?", new String[]{String.valueOf(id_produk)}, null, null,null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Produk produk = new Produk(cursor.getString(1), cursor.getString(2),cursor.getString(3),
                cursor.getString(4),cursor.getString(5),cursor.getString(6));
        return produk;
    }

    // FUNGSI UNTUK AMBIL SEMUA DATA PRODUK
    public List<Produk> getSemuaProduk(){
        List<Produk> produkList = new ArrayList<>();
        String selectQuery = " SELECT * FROM " + TABLE_PRODUK ;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()){
            do {
                Produk produk = new Produk(cursor.getString(1), cursor.getString(2),cursor.getString(3),
                        cursor.getString(4),cursor.getString(5),cursor.getString(6));
                produkList.add(produk);
            } while (cursor.moveToNext());
        }
        return produkList;
    }

    // FUNGSI MENGHITUNG ADA BEBERAPA DATA
    public int getProdukCount(){
        String countQuery = "SELECT * FROM " + TABLE_PRODUK;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();
        return cursor.getCount();
    }

    // FUNGSI UPDATE DATA PRODUK
    public int updateDataProduk(Produk produk) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_KD, produk.getKd_produk());
        values.put(COLUMN_NAMA, produk.getNama_produk());
        values.put(COLUMN_MERK, produk.getMerk_produk());
        values.put(COLUMN_JENIS, produk.getJenis_produk());
        values.put(COLUMN_VARIASI, produk.getVariasi_produk());
        values.put(COLUMN_FOTO, produk.getFoto_produk());

        return db.update(TABLE_PRODUK, values, COLUMN_ID + " = ?",
                new String[]{String.valueOf(produk.getId())});
    }

    // FUNGSI HAPUS DATA 1 PRODUK
    public void hapusDataProduk(Produk produk) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_PRODUK, COLUMN_ID + " = ?",
                new String[]{String.valueOf(produk.getId())});
        db.close();
    }

    // FUNGSI UNTUK MENGHAPUS SEMUA DATA PRODUK
    public void hapusSemuaDataProduk(){
        SQLiteDatabase db = this.getWritableDatabase();

        db.execSQL("DELETE FROM " + TABLE_PRODUK);
    }

    //FUNGSI MENGAMBIL DATA WHERE DI ACTIVITY KATEGORY
    public List<Produk> getKategoryProduk(String mMerkProduk) {

        List<Produk> kategoriList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + TABLE_PRODUK + " WHERE " + COLUMN_MERK + " =? " ;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery,new String[]{ mMerkProduk } );

        if (cursor.moveToFirst()) {
            do {
                Produk kategori = new Produk(cursor.getString(1), cursor.getString(2), cursor.getString(3),
                        cursor.getString(4), cursor.getString(5), cursor.getString(6));
                kategoriList.add(kategori);
            } while (cursor.moveToNext());
        }
        return kategoriList;
    }
}

Here's the error:

01-29 14:10:25.342 370-370/ptacs.ekatalog.com.e_katalogproduk E/AndroidRuntime: FATAL EXCEPTION: main
   Process: ptacs.ekatalog.com.e_katalogproduk, PID: 370
   java.lang.RuntimeException: Unable to start activity ComponentInfo{ptacs.ekatalog.com.e_katalogproduk/ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori}: java.lang.IllegalArgumentException: the bind value at index 1 is null
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
       at android.app.ActivityThread.access$800(ActivityThread.java:144)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5235)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
    Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
       at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
       at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
       at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
       at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
       at ptacs.ekatalog.com.e_katalogproduk.helper.DBHandler.getKategoryProduk(DBHandler.java:152)
       at ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori.initRecyclerView(ActivityKategori.java:68)
       at ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori.onCreate(ActivityKategori.java:43)
       at android.app.Activity.performCreate(Activity.java:6001)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) 
       at android.app.ActivityThread.access$800(ActivityThread.java:144) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:135) 
       at android.app.ActivityThread.main(ActivityThread.java:5235) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
01-29 14:10:25.364 370-370/? I/Process: Sending signal. PID: 370 SIG: 9

ACTIVITY_MENU

ACTIVITY_KATEGORI

Pang
  • 9,564
  • 146
  • 81
  • 122
Mosto Dani
  • 1
  • 10
  • 2
    By the way, the Android documentation recommends not using SQLiteOpenHepler anymore https://developer.android.com/training/data-storage/room/index.html – OneCricketeer Jan 29 '18 at 07:00
  • thanks , and what is the solution ? am i need to change all of the code ? iam beginner :D so be easy on me . – Mosto Dani Jan 29 '18 at 07:04
  • And as a beginner, it would be beneficial to read the guides and recommendations within the official documentation – OneCricketeer Jan 29 '18 at 07:05
  • `kategoriList = dbHandler.getKategoryProduk(getString(mMerkProduk));` what does `getString(mMerkProduk)` as the argument do? what is `getString()` function? – Aswin P Ashok Jan 29 '18 at 07:10
  • uhm its wrong ,sorry . I undo it already – Mosto Dani Jan 29 '18 at 07:12
  • https://stackoverflow.com/questions/4572090/illegalargumentexception-the-bind-value-at-index-1-is-null. Make sure `mMerkProduk` does have some value. – Aswin P Ashok Jan 29 '18 at 07:17
  • it describe that value is null ? but my code value it doesnt null , because it has value from previous activity(RecyclerOnClick) . and it work when i set to "Action Bar Title" – Mosto Dani Jan 29 '18 at 07:20
  • call `initRecyclerView();` after `if (getIntent().getExtras() != null) {....}` block – Aswin P Ashok Jan 29 '18 at 07:21

3 Answers3

2

You are calling initRecyclerView(); before you initialise mMerkProduk. So just call initRecyclerView(); after if (getIntent().getExtras() != null) {....} block. Just like this

    if (getIntent().getExtras() != null) {
        Bundle bundle = getIntent().getExtras();
        mMerkProduk = bundle.getString(Constant.BUNDLE_MERK_PRODUK); //MERK kategori

    //Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setTitle(mMerkProduk);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    dbHandler = new DBHandler(this);
    }

    initRecyclerView();
    cekDataRecyclerView();
Aswin P Ashok
  • 702
  • 8
  • 27
0

variable value is from bundle

Not sure why that matters... Extract the value from the Bundle and use its value as a parameter to the database methods like any other value

You seem to already know how to use bundle.getString(), for example

If all you want is to see the data, your OnCreate should look like this.

Most importantly, you need to assign the value before you query the database and populate the list.

If you look at these lines of the error, then look at your code, the intent is not read yet, and your string is null

at ptacs.ekatalog.com.e_katalogproduk.helper.DBHandler.getKategoryProduk(DBHandler.java:152)
at ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori.initRecyclerView(ActivityKategori.java:68)

Try instead separating the code that inits the list View object from the code the queries the database and populates the list

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kategori);

    dbHandler = new DBHandler(ActivityKategori.this);

    //Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
     getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    initRecyclerView();

    if (getIntent().getExtras() != null) {
        Bundle bundle = getIntent().getExtras();
        mMerkProduk = bundle.getString(Constant.BUNDLE_MERK_PRODUK); 
        setTitle(mMerkProduk);
        fillRecyclerView(mMerkProduk);
     } 
   // cekDataRecyclerView();
}

 private void initRecyclerView(){

     recyclerView = (RecyclerView) findViewById(R.id.rv_kategori);
     recyclerView.setHasFixedSize(true);
     layoutManager = new LinearLayoutManager(this);
     recyclerView.setLayoutManager(layoutManager);
 }

private void fillRecyclerView(String product) {
     if (product!=null)  {
         kategoriList = dbHandler.getKategoryProduk(product);
         adapter = new KategoriAdapter(kategoriList);
         recyclerView.setAdapter(adapter);
     }
} 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

As Logcat says

Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null

I think your mMerkProduk is null. Check mMerkProduk is null before query.

Check this answer. I think it's same case as yours.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50