-2

My main activity

   public void removefunction(){
    List<CartItem> cartItems = getCartItems(cart);
    cart.remove(cartItems.get(position).getProduct());
    cartItems.remove(position);
    cartItemAdapter.updateCartItems(getCartItems(cart));
    cartItemAdapter.notifyDataSetChanged();
    tvTotalPrice.setText(Constant.CURRENCY+String.valueOf(cart.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_UP)));
}

Adapter code

 btnremove= (Button) convertView.findViewById(R.id.btnrem);

    btnremove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ((ShoppingCartActivity) context).removefunction();


        }
    });

Cause null pointer exception in tvTotalPrice.setText

FATAL EXCEPTION: main Process: com.android.tonyvu.sc.demo, PID: 7601 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.android.tonyvu.sc.demo.ShoppingCartActivity.removefunction(ShoppingCartActivity.java:263) at com.android.tonyvu.sc.demo.adapter.CartItemAdapter$1.onClick(CartItemAdapter.java:98) at android.view.View.performClick(View.java:6205) at android.widget.TextView.performClick(TextView.java:11103)

Sufian
  • 6,405
  • 16
  • 66
  • 120
Hamza Javed
  • 72
  • 10

1 Answers1

0

The ButtonClick event is alright, as your method triggers the setText method. The problem is that your tvTotalPrice-Textview is not set (or wrong set), which is the reason why it is null. That's why you get the NullPointerException.

Antonio Vlasic
  • 337
  • 3
  • 15