-2

I'm trying to pass the id of a RecyclerView single item to my DetailsActivity.java, but running into a NullPointerException.

public class RecyclerAdapter extends      RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {


static List<DatabaseModel> dbList;
static Context context;
RecyclerAdapter(Context context, List<DatabaseModel> dbList){
    this.dbList = new ArrayList<DatabaseModel>();
    this.context = context;
    this.dbList = dbList;

}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.item_row, null);

    // create ViewHolder

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {

    holder.address_h.setText(dbList.get(position).getAddress());
    holder.name_h.setText(dbList.get(position).getName());
    holder.date_h.setText("Due:" + dbList.get(position).getSpare1());
    holder.balance_h.setText("Owes £" + dbList.get(position).getBalance());
    holder.amount_h.setText("Regular Price £" + dbList.get(position).getAmount());


    holder.address_h.setSelected(holder.address_h.isSelected());
    holder.name_h.setSelected(holder.name_h.isSelected());
    holder.date_h.setSelected(holder.date_h.isSelected());
    holder.balance_h.setSelected(holder.balance_h.isSelected());
    holder.amount_h.setSelected(holder.amount_h.isSelected());
}

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

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView amount_h, address_h, balance_h, name_h, date_h;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);


        address_h = (TextView) itemLayoutView.findViewById(R.id.rvaddress);
        balance_h = (TextView)itemLayoutView.findViewById(R.id.rvphone);
        name_h = (TextView)itemLayoutView.findViewById(R.id.rvname_h);
        date_h = (TextView)itemLayoutView.findViewById(R.id.rvdate_h);
        amount_h = (TextView)itemLayoutView.findViewById(R.id.rv_regular_price);

        itemLayoutView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int adapterPosition_id=getAdapterPosition();
        int position_id=dbList.get(adapterPosition_id).get_id();

        Bundle extras = new Bundle();
        extras.putInt("position", position_id);
        Intent intent = new Intent(context,DetailsActivity.class);

        context.startActivity(intent);
    }
}
}

public class DetailsActivity extends AppCompatActivity {


    DatabaseHelper helper;
    List<DatabaseModel> dbList, dbPaidList;
    int position, dayPeriod;
    TextView tvid, tvname, tvaddress, tvbalance, posOrNegBalance;
    Button btnDone, btnPaid;
    Cursor cursor;

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

        setContentView(R.layout.activity_details);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        position = bundle.getInt("position"); // **This is where my code breaks down**

        helper = new DatabaseHelper(this);

And my error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.freerolhq.windowscashbook/uk.co.freerolhq.windowscashbook.DetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
                      at android.app.ActivityThread.access$800(ActivityThread.java:178)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:194)
                      at android.app.ActivityThread.main(ActivityThread.java:5637)
                      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:959)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
                      at uk.co.freerolhq.windowscashbook.DetailsActivity.onCreate(DetailsActivity.java:49)
                      at android.app.Activity.performCreate(Activity.java:6092)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                      at android.os.Handler.dispatchMessage(Handler.java:111) 
                      at android.os.Looper.loop(Looper.java:194) 
                      at android.app.ActivityThread.main(ActivityThread.java:5637) 
                      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:959) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)'
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MarkC
  • 59
  • 10
  • Apparently `DetailsActivity` was not started with any extras. Why do you seem to think it was? – OneCricketeer Feb 21 '17 at 14:47
  • I'm pretty new to this, copy and pasted my code and then adapted it to my needs. When I put breakpoints I can see the data added in my recyclerdapter but it doesn't arrive at my detailsactivity – MarkC Feb 21 '17 at 14:56
  • That's fine. Have you learned enough java to know how to simply do `if (someObject != null) { }`? – OneCricketeer Feb 21 '17 at 14:58
  • yes, I know most of my basics like if/ else statements but errors are hard to pinpoint. – MarkC Feb 21 '17 at 15:03
  • Read the stacktrace? `DetailsActivity.onCreate(DetailsActivity.java:49)`. For whatever reason the Bundle is null. Your IDE might even be telling you `Bundle extras` is *unused* – OneCricketeer Feb 21 '17 at 15:05
  • Thank you Cricket, I know you were trying to get me to notice the problem myself but I was stumped as the error was coming from my DetailsActivity. I have spent several hours searching to resolve this problem. I'm unsure how to use the stacktrace but I will do that now, thank you – MarkC Feb 21 '17 at 15:15
  • Regarding the stacktrace - it is *really, really* useful. http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – OneCricketeer Feb 21 '17 at 15:18
  • 2
    Coming back to this thread because my problem wasn't the actual Intent but it was not knowing how to debug. You gave me an AHA moment. I'm well on the way as a developer having released a few apps since my question. While others flamed me for asking a noob question, you helped guide me in the right direction. I wish others could understand that sometimes developers need nudged in the right direction. Sincerely thank you. – MarkC Nov 03 '19 at 14:34

1 Answers1

1

I think you forgot to put the bundle inside the intent you are creating in onClick method

intent.putExtras(extras);
Majeed Khan
  • 505
  • 7
  • 16