Code:
package com.example.shrine;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.Menu;
public class products_act extends AppCompatActivity {
String title[];
String description[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_act);
title=getResources().getStringArray(R.array.pro_title);
description=getResources().getStringArray(R.array.pro_desc);
RecyclerView recyclerView=findViewById(R.id.recyclerr);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ProductsAdapter PA=new ProductsAdapter(this,title,description);
recyclerView.setAdapter(PA);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings,menu);
return true;
}
}
Adapter class:
package com.example.shrine;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProViewHolder> {
Context ct;
String[] data1;
String[] data2;
public ProductsAdapter(Context context,String title[],String description[]){
ct=context;
data1=title;
data2=description;
}
public class ProViewHolder extends RecyclerView.ViewHolder {
TextView head;
TextView info;
ImageView pro_img;
public ProViewHolder(@NonNull View itemView) {
super(itemView);
head=itemView.findViewById(R.id.text1);
info=itemView.findViewById(R.id.text2);
pro_img=itemView.findViewById(R.id.img);
}
}
@NonNull
@Override
public ProViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View layoutview= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_products_act,parent,false);
return new ProViewHolder(layoutview);
}
@Override
public void onBindViewHolder(@NonNull ProViewHolder holder, int position) {
holder.head.setText(data1[position]);
holder.info.setText(data2[position]);
}
@Override
public int getItemCount() {
return data1.length;
}
}
ERROR:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shrine, PID: 4610
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shrine/com.example.shrine.products_act}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
at com.example.shrine.products_act.onCreate(products_act.java:22)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Please resolve this issue and also please explain why NullPointerException occurs so that I can resolve these kind of problems myself in future.Explain NullPointerException in brief in relation with android. Also if possible please do state some points to keep in mind everytime while dealing with RecyclerView.