0

newbie to android programming and started learning. I hope that anyone will be able to guide me to solve this issue. Thanks. The problem I'm having is I can't seem to get the data to display in the RestaurantInfoActivity when the user clicks on the list. It will keep prompting that error. Please do let me know if you need further information

RestaurantAdapter

@Override
protected void onBindViewHolder(@NonNull RestaurantViewHolder holder, final int position, @NonNull 
final RestaurantDetails details) {

    holder.restaurant_Name.setText(details.getName());
    holder.restaurant_Category.setText(details.getCategory());
    holder.restaurant_Location.setText(details.getLocation());

    holder.itemView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            String post_key = getRef(position).getKey();

            Intent intent = new Intent(view.getContext(), RestaurantInfoActivity.class);
            intent.putExtra("Post_key", position);
            view.getContext().startActivity(intent);
        }
    });
}

'''

RestaurantInfoActivity

public class RestaurantInfoActivity extends AppCompatActivity{

TextView iRestaurantName, iRestaurantCategory, iRestaurantAddress, iRestaurantLocation;
private static final String TAG = RestaurantInfoActivity.class.getSimpleName();
DatabaseReference mReference;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.restaurant_details);
    Log.d(TAG, "onCreate: started.");

    FirebaseRecyclerOptions<RestaurantDetails> options =
            new FirebaseRecyclerOptions.Builder<RestaurantDetails>()
                    .setQuery(FirebaseDatabase.getInstance().getReference().child("Restaurant"), RestaurantDetails.class)
                    .build();

    //Toast.makeText(RestaurantInfoActivity.this, post_key,Toast.LENGTH_LONG).show();
    //Log.d(TAG,"onClick" + post_key );



    mReference = FirebaseDatabase.getInstance().getReference().child("Restaurant");

    final String post_key = getIntent().getExtras().get("post_key").toString();

    iRestaurantName = (TextView) findViewById(R.id.restaurantName);
    iRestaurantAddress = (TextView) findViewById(R.id.restaurantAddress);
    iRestaurantCategory = (TextView) findViewById(R.id.restaurantCategory);
    iRestaurantLocation = (TextView) findViewById(R.id.restaurantLocation);

    mReference.child(post_key).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            RestaurantDetails details = dataSnapshot.getValue(RestaurantDetails.class);
            if(dataSnapshot.exists()){
                RestaurantDetails nameRestaurant = dataSnapshot.child("name").getValue().toString();
                RestaurantDetails locationRestaurant = dataSnapshot.child("location").getValue().toString();
                RestaurantDetails addressRestaurant = dataSnapshot.child("address").getValue().toString();
                RestaurantDetails categoryRestaurant = dataSnapshot.child("category").getValue().toString();


                iRestaurantName.setText( nameRestaurant);
                iRestaurantLocation.setText(locationRestaurant);
                iRestaurantAddress.setText(addressRestaurant);
                iRestaurantCategory.setText(categoryRestaurant);


            }

'''

Errorlog

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.isaac.foodie/com.isaac.foodie.RestaurantInfoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7078)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at com.isaac.foodie.RestaurantInfoActivity.onCreate(RestaurantInfoActivity.java:49)
    at android.app.Activity.performCreate(Activity.java:7327)
    at android.app.Activity.performCreate(Activity.java:7318)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7078) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964) 
I.Z.
  • 23
  • 8
  • At which particular line of code are you getting that error? – Alex Mamo Mar 28 '20 at 17:50
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ryan M Mar 28 '20 at 20:45
  • Accepted answer notes this is a typo, sending the wrong key in the intent. – Ryan M May 28 '21 at 01:19

2 Answers2

0

I am seeing in your click events that you are not sending post key with intent instead you are sending position.Change your click event to this

holder.itemView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
        String post_key = getRef(position).getKey();

        Intent intent = new Intent(view.getContext(), RestaurantInfoActivity.class);
        intent.putExtra("Post_key", post_key);
        view.getContext().startActivity(intent);
    }
});
shakac
  • 356
  • 3
  • 14
  • thank you for your answer. It worked. I have changed the position to post_key. Also, @KasimOzdemir also pointed my issue due to the spelling of post_key. Thank you – I.Z. Mar 29 '20 at 03:14
  • please accept my answer and give some reputation to me – shakac Mar 29 '20 at 05:01
0

Difference:

intent.putExtra("Post_key", position);

and

final String post_key = getIntent().getExtras().get("post_key").toString();

Post_key instead of post_key

Kasım Özdemir
  • 4,028
  • 3
  • 12
  • 24