0

you have been fantastic with my previous questions :)

unfortunately ive come across another problem, i've implemented my recycler view but for some reason my app crashes when i navigate to the activity holding it the error is

"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference at rgu.ac.uk.RepRepo.CompleteWorkoutActivity.onCreate(CompleteWorkoutActivity.java:33)"

this tells me that its something to do with my adapter I believe as line 33 is "recyclerView.setAdapter(customAdapter);"

The code for my adapter is as follows:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private Context context;
public ArrayList exercise, sets, reps;
CustomAdapter(Context context, ArrayList exercise, ArrayList sets, ArrayList reps){
    this.context = context;
    this.exercise = exercise;
    this.sets = sets;
    this.reps = reps;

}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.my_row, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.ExerciseText.setText(String.valueOf(exercise.get(position)));
    holder.SetsText.setText(String.valueOf(sets.get(position)));
    holder.RepsText.setText(String.valueOf(reps.get(position)));

}

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

public static class MyViewHolder extends RecyclerView.ViewHolder {

    TextView ExerciseText, SetsText, RepsText;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        ExerciseText = itemView.findViewById(R.id.ExerciseText);
        SetsText = itemView.findViewById(R.id.SetsText);
        RepsText = itemView.findViewById(R.id.RepsText);

    }
}

The full code for my activity is:

public class CompleteWorkoutActivity extends AppCompatActivity {

RecyclerView recyclerView;
DBHelper DB;
ArrayList<String> exercise, sets, reps;
CustomAdapter customAdapter;

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

    DB = new DBHelper(CompleteWorkoutActivity.this);
    exercise = new ArrayList<>();
    sets = new ArrayList<>();
    reps = new ArrayList<>();

    storeDataInArrays();

    customAdapter = new CustomAdapter(CompleteWorkoutActivity.this, exercise, sets, reps);
    recyclerView.setAdapter(customAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(CompleteWorkoutActivity.this));

}

void storeDataInArrays(){
    Cursor cursor = DB.readAllData();
    if (cursor.getCount() == 0){
        Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
    }else{
        while (cursor.moveToNext()){
            exercise.add(cursor.getString(0));
            sets.add(cursor.getString(1));
            reps.add(cursor.getString(2));
        }
    }
}

The full error message is:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: rgu.ac.uk.RepRepo, PID: 23165
java.lang.RuntimeException: Unable to start activity ComponentInfo{rgu.ac.uk.RepRepo/rgu.ac.uk.RepRepo.CompleteWorkoutActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
    at rgu.ac.uk.RepRepo.CompleteWorkoutActivity.onCreate(CompleteWorkoutActivity.java:33)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Ive been scratching my head at this one for a while and i really cant see the issue, id appreciate it if anyone could shed some light on this as my assignment is dependant on this working, You guys have been great with all my other issues - thanks, Ian

Ian Smith
  • 1
  • 1
  • `recyclerView` is never set in the code posted in `onCreate` (therefore it's null). –  Dec 09 '21 at 23:05
  • Hi Andy, was this in the first or second piece of code? – Ian Smith Dec 09 '21 at 23:09
  • I do import it in the custom adapter – Ian Smith Dec 09 '21 at 23:09
  • You must initialize `recyclerView` (in CompleteWorkoutActivity) before using it - likely using your layout - if you post that then it can help - you would do a `findViewById` (again assuming it's coming from your layout). –  Dec 09 '21 at 23:10
  • Andy you are a legend, I would have never noticed, thankyou so much! – Ian Smith Dec 09 '21 at 23:13
  • Here's a good complete example for later reference: https://stackoverflow.com/a/40584425/2711811. –  Dec 09 '21 at 23:14

0 Answers0