-6

I'm trying to follow a given example by the user @sunil-sunny, that says

You can create static varibales like this

static  FragmentB f;

public static FragmentB newInstance(String title) {
        FragmentB f = new FragmentB();
        Bundle b = new Bundle();
        b.putString(ARG_STATION_TITLE, title);
        f.setArguments(b);
        return f;
    }

You can use the getInstance() method to get the instance of fragmentB

public static FragmentB getInstance(){
    return f;
}

Call like this FragmentB.getInstance().methodinFragmentB();

But for some reason I'm getting a NullPointerException.

In my Fragment, I have:

static ProductListFragment fragment;

    public static ProductListFragment newInstance(Category category, SubCategory subCategory) {
        Bundle args = new Bundle();
        args.putParcelable(AppConstants.FLAG_CATEGORY, category);
        args.putParcelable(AppConstants.FLAG_SUBCATEGORY, subCategory);
        ProductListFragment fragment = new ProductListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static ProductListFragment getInstance(){
        return fragment;
    }

And in another fragment I call a method detachFragment to close the current fragment and reload the list updated in the previous fragment:

// We close the fragment and reload the list
public void detachFragment(String tag) {
    transition.reverseTransition(100);
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(tag);
    if (fragment != null) {
        fragmentManager
               .beginTransaction()
               .disallowAddToBackStack()
               .setCustomAnimations(R.anim.slide_up_bottom_sheet, R.anim.slide_down_bottom_sheet)
               .remove(fragment)
               .commitNow();

        ProductListFragment.getInstance().reloadProducts(); // Reload Fragment
    }
}

Logcat sais that the next line return a Null:

...
ProductListFragment.getInstance().reloadProducts(); // Reload Fragment
...
Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Víctor Martín
  • 3,352
  • 7
  • 48
  • 94

3 Answers3

0

when you call ProductListFragment.getInstance().reloadProducts(); you are getting the static ProductListFragment fragment as a return from this function however static ProductListFragment fragment is no where initialised which returns null.

N.Moudgil
  • 709
  • 5
  • 11
0

Use Below code

static ProductListFragment fragment;
public static ProductListFragment newInstance(Category category, SubCategory subCategory) {
    Bundle args = new Bundle();
    args.putParcelable(AppConstants.FLAG_CATEGORY, category);
    args.putParcelable(AppConstants.FLAG_SUBCATEGORY, subCategory);
//Replace this line 
   // ProductListFragment fragment = new ProductListFragment();
//with this
fragment = new ProductListFragment();
    fragment.setArguments(args);
    return fragment;
}

public static ProductListFragment getInstance(){
    return fragment;
}
Himeshgiri gosvami
  • 2,559
  • 4
  • 16
  • 28
0

As Jaydeep Patel sais:

check this line ProductListFragment fragment = new ProductListFragment(); and remove ProductListFragment

I've remove the declaration of a new fragment ProductListFragment and it's working. I was initializing 2 times the same variable (same name).

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94