0

I'm trying to transfer to a new fragment when an item in my recycler view is clicked:

FragmentTransaction fragmentTx = ((AppCompatActivity)Activity).SupportFragmentManager.BeginTransaction();
            fragmentTx.Replace(Resource.Id.container, fragment);

But the following error is raised:

System.NullReferenceException: Object reference not set to an instance of an object.

I've tried so many different things but it's not working.

I get the recycler view click from this:

private void MMainView_Click(object sender, EventArgs e)
{
    int position = mRecyclerView.GetChildAdapterPosition((View)sender);
    int indexPosition = (mFavourites.Count - 1) - position;

    favFragment.TransitionOnCardClick(mFavourites[indexPosition].CalcName);
}

which then calls this method in the fragment that has the recycler view:

public void TransitionOnCardClick(string cardName)
{
    Fragment fragment = null;

    switch (cardName)
    {
        case "Pace":
            fragment = splitFragment;
            break;

        case "Watts":
            fragment = wattFragment;
            break;

        case "Weight Adjustment":
            fragment = weightFragment;
            break;
    }

    FragmentTransaction fragmentTx = ((AppCompatActivity)Activity).SupportFragmentManager.BeginTransaction();
    fragmentTx.Replace(Resource.Id.container, fragment);
    fragmentTx.AddToBackStack(null);
    fragmentTx.Commit();
}

How do I fix this? My question is different to others as i have tried so many different ways yet i have not found a solution.

euandeas
  • 91
  • 1
  • 8
  • 1
    It looks like your switch statement isn't matching any of its cases. Are you sure `cardName` is actual equal to one of those three cases? – TheWanderer Oct 18 '18 at 20:04
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Lews Therin Oct 18 '18 at 20:25
  • @TheWanderer the cases are being met ive tested that before – euandeas Oct 19 '18 at 06:30

1 Answers1

0

In the beginning you set your fragment to null, and then there are three cases in which you give it a value, I am guessing you should make sure that at least one of those cases is met, i.e the cardname is actually one of the cases. It could even be a typo somewhere.

Try adding a default statement and making it print out something then run your program, if it does print out it means none of the cases are being met.

Edit: It has to be in the begin transaction method, double check for any null values inside it.

Katada
  • 81
  • 3
  • Why are you using null for addBackToStack? Thats the only other place I can see it go wrong. Can you debug it and check which line causes the error? – Katada Oct 19 '18 at 10:23
  • ```FragmentTransaction fragmentTx = ((AppCompatActivity)Activity).SupportFragmentManager.BeginTransaction();``` it is this line. – euandeas Oct 19 '18 at 12:42
  • I would check the BeginTransaction method, there seems to be some null return value or an object inside it not being initialized correctly. – Katada Oct 19 '18 at 13:48
  • Its not something i have access to it is an android library – euandeas Oct 19 '18 at 16:00