title: Update Listview in fragment from another fragment input
body: <p>I want to make a POS app in my tablet device. I have 2 fragments here. I want to update listview in my cart in the right fragment. Data to fill the cart come from my numpad fragment. So when I click the plus button, cart list will update data with my input.</p>
  <p>My problem is the data can't update and I get error null in my custom adapter.</p>
  <p><a href="https://i.stack.imgur.com/ikuwO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ikuwOm.png" alt="UI" /></a></p>
  <p>RegisterFragment.Java</p>
  <pre><code>import android.os.Bundle;
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import com.project.pos.posandroid.Adapter.CartAdapter;
  import com.project.pos.posandroid.Adapter.Pager;
  import com.project.pos.posandroid.Model.CartModel;
  import com.project.pos.posandroid.R;
  import com.project.pos.posandroid.Utils.CartPreference;
  
  import java.util.List;
  
  /**
   * A simple {@link Fragment} subclass.
   */
  public class RegisterFragment extends Fragment implements TabLayout.OnTabSelectedListener{
  
      private TabLayout tabLayout;
      private ViewPager viewPager;
      private CartPreference cartPreference;
      List&lt;CartModel&gt; cart;
      private CartAdapter cartAdapter;
      ListView lvCart;
  
      public RegisterFragment() {
          // Required empty public constructor
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View rootview = inflater.inflate(R.layout.fragment_register, container, false);
          getActivity().setTitle(&quot;Register&quot;);
  
          tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout);
          tabLayout.addTab(tabLayout.newTab().setText(&quot;Library&quot;));
          tabLayout.addTab(tabLayout.newTab().setText(&quot;Numpad&quot;));
  
          viewPager = (ViewPager) rootview.findViewById(R.id.pager);
          Pager adapter = new Pager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
  
          tabLayout.setupWithViewPager(viewPager);
          tabLayout.setOnTabSelectedListener(this);
  
          cartPreference = new CartPreference();
          cart=cartPreference.getFavorites(getActivity());
          cartAdapter=new CartAdapter(getActivity(),cart);
  
          lvCart=(ListView) rootview.findViewById(R.id.lvCart);
  
          if(cart==null) {
              Toast.makeText(getActivity(), &quot;No Data Available&quot;, Toast.LENGTH_SHORT).show();
          }
          else {
              if(cart.size()==0){
                  Toast.makeText(getActivity(), &quot;Your Cart Still Empty&quot;, Toast.LENGTH_SHORT).show();
              }
              else {
                  int temp = 0, temp2 = 0;
  
                  for (int i = 0; i &lt; cart.size(); i++) {
                      temp = temp + (Integer.parseInt(cartAdapter.getItem(i).getHarga())* Integer.parseInt(cartAdapter.getItem(i).getQty()));
                  }
              }
              refreshDisplay();
          }
  
          return rootview;
      }
  
      public void refreshDisplay() {
  
          cartAdapter= new CartAdapter(getActivity(), cart);
          lvCart.setAdapter(cartAdapter);
      }
  
      @Override
      public void onTabSelected(TabLayout.Tab tab) {
          viewPager.setCurrentItem(tab.getPosition());
      }
  
      @Override
      public void onTabUnselected(TabLayout.Tab tab) {
  
      }
  
      @Override
      public void onTabReselected(TabLayout.Tab tab) {
  
      }
  }
  </code></pre>
  <p>NumpadFragment.Java</p>
  <pre><code>import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.TextView;
  
  import com.project.pos.posandroid.Model.CartModel;
  import com.project.pos.posandroid.R;
  import com.project.pos.posandroid.Utils.CartPreference;
  
  import java.util.List;
  
  /**
   * A simple {@link Fragment} subclass.
   */
  public class NumpadFragment extends Fragment{
      private TextView txtScreen;
      private boolean stateError;
      private boolean lastNumeric;
      CartPreference cartPreference;
      Button nol,satu,dua,tiga,empat,lima,enam,tujuh,delapan,sembilan,clear,add;
      private RegisterFragment registerFragment;
      List&lt;CartModel&gt; cart;
  
  
      String curentText;
  
  
      public NumpadFragment() {
          // Required empty public constructor
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootview = inflater.inflate(R.layout.fragment_numpad, container, false);
          // Inflate the layout for this fragment
          registerFragment = new RegisterFragment();
          cartPreference = new CartPreference();
          txtScreen = (TextView) rootview.findViewById(R.id.txtScreen);
          curentText = txtScreen.getText().toString();
          add = (Button) rootview.findViewById(R.id.btnAdd);
         registerFragment = new RegisterFragment();
  
          add.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  CartModel prod=new CartModel(&quot;Amount&quot;,&quot;0&quot;,txtScreen.getText().toString());
                  cartPreference.addFavorite(getActivity(),prod);
              registerFragment.refreshDisplay();
  
              }
          });
  
  
          return rootview;
      }
  }
  </code></pre>
  <p>CartAdapter.Java</p>
  <pre><code>public class CartAdapter extends ArrayAdapter&lt;CartModel&gt; {
  private Context context;
  List&lt;CartModel&gt; cartModels;
  CartPreference cartPreference;
  
  public CartAdapter(Context context, List&lt;CartModel&gt; cartModels){
      super(context, R.layout.list_cart,cartModels);
      this.context=context;
      this.cartModels=cartModels;
      cartPreference = new CartPreference();
  }
  
  private class ViewHolder {
      TextView nama;
      TextView qty;
      TextView harga;
  }
  
  @Override
  public int getCount() {
      return cartModels.size();
  }
  
  @Override
  public CartModel getItem(int position) {
      return cartModels.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      return 0;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder = null;
      if (convertView == null) {
          LayoutInflater inflater = (LayoutInflater) context
                  .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.list_cart, null);
          holder = new ViewHolder();
          holder.nama = (TextView) convertView.findViewById(R.id.tvItemName);
          holder.qty= (TextView) convertView.findViewById(R.id.tvQty);
          holder.harga= (TextView) convertView.findViewById(R.id.tvPrice);
          convertView.setTag(holder);
      } else {
          holder = (ViewHolder) convertView.getTag();
      }
      CartModel product = (CartModel) getItem(position);
      holder.nama.setText(product.getNama());
      holder.qty.setText(product.getQty());
      holder.harga.setText(product.getHarga());
  
      return convertView;
  }
  
  
  /*Checks whether a particular product exists in SharedPreferences*/
  public boolean checkFavoriteItem(CartModel checkProduct) {
      boolean check = false;
      List&lt;CartModel&gt; favorites = cartPreference.getFavorites(context);
      if (favorites != null) {
          for (CartModel product : favorites) {
              if (product.equals(checkProduct)) {
                  check = true;
                  break;
              }
          }
      }
      return check;
  }
  
  @Override
  public void add(CartModel product) {
      super.add(product);
      cartModels.add(product);
      notifyDataSetChanged();
  }
  
  @Override
  public void remove(CartModel product) {
      super.remove(product);
      cartModels.remove(product);
      notifyDataSetChanged();
  }
  </code></pre>
  <p>}</p>
  <p>Error</p>
  <pre class="lang-none prettyprint-override"><code>E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.project.pos.posandroid, PID: 3693
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.view.LayoutInflater.from(LayoutInflater.java:232)
        at android.widget.ArrayAdapter.&lt;init&gt;(ArrayAdapter.java:181)
        at android.widget.ArrayAdapter.&lt;init&gt;(ArrayAdapter.java:166)
        at com.project.pos.posandroid.Adapter.CartAdapter.&lt;init&gt;(CartAdapter.java:0)
        at com.project.pos.posandroid.Fragment.RegisterFragment.refreshDisplay(RegisterFragment.java:85)
        at com.project.pos.posandroid.Fragment.NumpadFragment$12.onClick(NumpadFragment.java:215)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  </code></pre>