-2

I have a button and I want to set a field of row in SQLite to 1 when I click on it. The default value of the row is 0. I want to pass the id and then update the Liked row of the id. I wrote the update method as below:

public void setLiked(long id) {
    walldb = this.getWritableDatabase() ;
    ContentValues values = new ContentValues();
    values.put(Colwall_Liked,1);
    walldb.update(TABLE_NAME , values, "=" + id,null) ;
    walldb.close();
}

Here is my recyclerView Adapter class.

public class wallAdapter extends RecyclerView.Adapter<wallAdapter.ViewHolderWall> {

private Context ctx;
List<ModelWallpaper> modelWallpaper;
WallpaperHelper myHelper;

public wallAdapter(Context ctx, List<ModelWallpaper> modelWallpaper) {
    this.ctx = ctx;
    this.modelWallpaper = modelWallpaper;
}

@NonNull
@Override
public ViewHolderWall onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(ctx).inflate(R.layout.recyclerview_wallpaper,viewGroup,false);
    return new wallAdapter.ViewHolderWall(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolderWall viewHolderWall, int i) {
    final ModelWallpaper model = modelWallpaper.get(i);

    Glide.with(ctx)
            .load(model.getImageURL())
            .apply(new RequestOptions().centerCrop())
            .into(viewHolderWall.image);
    viewHolderWall.id = model.getID() ;

    viewHolderWall.setWall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Glide.with(ctx)
                    .asBitmap()
                    .load(model.getImageURL())
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                            try {
                                WallpaperManager.getInstance(ctx).setBitmap(resource) ;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });

        }
    });



}

@Override
public int getItemCount() {
    return modelWallpaper.size();
}

class ViewHolderWall extends RecyclerView.ViewHolder {

private ImageView image;
private ShineButton heart;
private Button setWall;
long id;


public ViewHolderWall(View itemView) {
    super(itemView);

    image = itemView.findViewById(R.id.wallimage) ;
    heart = itemView.findViewById(R.id.po_image1);
    setWall = itemView.findViewById(R.id.setWall);

    final ModelWallpaper modelWallpaper = new ModelWallpaper();

    heart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(modelWallpaper.getLiked() == 0) {
                myHelper.setLiked(id);
            }
        }
    });
}
}


}

I wrote the onClick method inside my viewholder class in recyclerview Adapter.

long id; 

final ModelWallpaper modelWallpaper = new ModelWallpaper();

heart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(modelWallpaper.getLiked() == 0) {
                myHelper.setLiked(id);
            }
        }
    });

Here is database Hepler class

public class WallpaperHelper extends SQLiteOpenHelper{


SQLiteDatabase walldb ;

private static final String DATABASE_NAME = "Wallpaper.db" ;
private static final String TABLE_NAME = "Wallpaper_table" ;
private static final String Colwall_id = "ID" ;
private static final String Colwall_Url = "URL" ;
private static final String Colwall_Liked = "Liked" ;

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

}


@Override
public void onCreate(SQLiteDatabase db) {
    String query = "create table if not exists " + TABLE_NAME + "(" + Colwall_id + " integer primary key autoincrement , " + Colwall_Url + " text not null , " + Colwall_Liked + " integer default 0" + ")";

    try {
        db.execSQL(query);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + TABLE_NAME);
    onCreate(db);
}

But when I click on the Button the app force stops. Here is my Log

   java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.hp.finalproject.Activities.Wallpaper.WallpaperHelper.setLiked(long)' on a null object reference
        at com.example.hp.finalproject.Activities.Wallpaper.wallAdapter$ViewHolderWall$1.onClick(wallAdapter.java:112)
        at com.sackcentury.shinebuttonlib.ShineButton$OnButtonClickListener.onClick(ShineButton.java:343)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22429)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Unicorn
  • 37
  • 8

1 Answers1

0

initialize myHelper = new WallpaperHelper(ctx) inside your ViewHolderWall