1

I am trying to add same values on listView by user clicks, when user clicks a button, the system graps the value of two EditTexts and add it into the list.

But when I click a button Error displays:

01-14 12:46:31.623 21297-21297/com.example.transportor E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.transportor, PID: 21297 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.widget.ArrayAdapter.init(ArrayAdapter.java:310) at android.widget.ArrayAdapter.(ArrayAdapter.java:153) at com.example.transportor.product_list_adapter.(product_list_adapter.java:24) at com.example.transportor.ui.home.HomeFragment$1.onClick(HomeFragment.java:59) at android.view.View.performClick(View.java:4757) at android.view.View$PerformClick.run(View.java:19757) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5235) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

My Homefragment:

 Context mcontext;
 Button addListe = (Button)root.findViewById(R.id.addL);
        final ListView productListe = (ListView)root.findViewById(R.id.productList);
        addListe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                product_list PL = new product_list(quantity.getText().toString(),produit.getText().toString());
                ArrayList<product_list> PLL = new ArrayList<>();
                 PLL.add(PL);
                product_list_adapter p_l_a = new product_list_adapter(mcontext,R.layout.adapter_product_list,PLL);
                productListe.setAdapter(p_l_a);
            }
        });

and My Product list:

  private String quantity;
    private String product;

    public product_list(String quantity, String product) {
        this.quantity = quantity;
        this.product = product;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

and product_list_adapter:

 private Context mContext;
    int mRessource;
    public product_list_adapter(@NonNull Context context, int resource, @NonNull ArrayList<product_list> objects) {
        super(context, resource, objects);
        this.mContext = mContext;
        mRessource = resource;
    }



    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //get infos
        String quatity = getItem(position).getQuantity();
        String product = getItem(position).getProduct();
        product_list PL = new product_list(quatity,product);
        LayoutInflater iniat = LayoutInflater.from(mContext);
        convertView = iniat.inflate(mRessource,parent,false);
        TextView Tquantity = (TextView)convertView.findViewById(R.id.TextView1);
        TextView Tproduct = (TextView)convertView.findViewById(R.id.TextView2);
        Tquantity.setText(quatity);
        Tproduct.setText(product);
        return convertView;

    }
bb azeae
  • 87
  • 2
  • 8

2 Answers2

0

You are setting mContext wrongly inside product_list_adapter constructor,so your mContext is always null.

hyhashemi
  • 43
  • 4
0

Don't pass context in adapter constructor.

Just replace LayoutInflater iniat = LayoutInflater.from(mContext); with

LayoutInflater iniat = LayoutInflater.from(parent.getContext());

Prashant Jha
  • 574
  • 6
  • 21
  • Btw, you have forgot to initialize mContext in your fragment FYI. – Prashant Jha Jan 14 '20 at 13:09
  • when I replace `LayoutInflater iniat = LayoutInflater.from(mContext);` with `LayoutInflater iniat = LayoutInflater.from(parent.getContext());` and remove mcontext I got the dsame error – bb azeae Jan 14 '20 at 14:05