Error NullPointerExeception: When trying to add Value in ArrayList & adapter.notifydatachanged

20 Views Asked by At

I have setup & initiate an RecyclerView mainWatchList, and ArrayList stockPriceList in MainActivity OnCreate() function, and also have StockPrice() function in MainActivity in which I am adding data to ArrayList stockPriceList, and using minWatchList recyclerView adapter stockPriceAdapter.notifyDataSetChanged(); to refresh the data changed.

I have create 2 RecyclerViews suggestList & mainWatchList and when someone clicks itemView of suggestList itemView.OnClickFunction is called and It passes the arrayList postion and also calls the StockPrice Function.

But at last, When I am trying to run program it shows error that stockPriceList.add(new defualtWatchListModel(Name, symbol, price)); stockPriceList is null and you can't add into.

Error:Here is the Image of Error

Code of MainActivity.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    suggestList = findViewById(R.id.suggestList);
    searchBar = findViewById(R.id.searchBar);
    stockModelClassesArray = new ArrayList<>();

    requestQueue = Volley.newRequestQueue(MainActivity.this);

    suggestList.setLayoutManager(new LinearLayoutManager(this));

    suggestAdapter = new stockSuggestAdapter(stockModelClassesArray, MainActivity.this);

    suggestList.setAdapter(suggestAdapter);

    // Setting up Main Activity ArrayList.
    mainWatchList = findViewById(R.id.defaultWatchList);
    stockPriceList = new ArrayList<>();
    mainWatchList.setLayoutManager(new LinearLayoutManager(this));
    stockPriceAdapter = new StockPriceAdapter(stockPriceList, MainActivity.this);
    mainWatchList.setAdapter(stockPriceAdapter);
}

also in Main Activity StockPrice Function Code:

public void StockPrice() {

    String name = Name;
    String Symbol = symbol;
    String price = "$20";

    stockPriceList.add(new defualtWatchListModel(Name, symbol, price));
    stockPriceAdapter.notifyDataSetChanged();

}

}

Code From suggestList RecyclerView Adapter in which StockPrice() function have been called:

 public class ViewHolder extends RecyclerView.ViewHolder {

    TextView symbol, name, exchange, type;
    public ConstraintLayout listContainer;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);

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

                MainActivity mainActivity = new MainActivity();
                stockModelClass positionedStock =  stockModelClassArrayList.get(getAbsoluteAdapterPosition());
                mainActivity.OnClickItemViewListener(positionedStock.getName(), positionedStock.getSymbol());
               // Run this one
                mainActivity.StockPrice();

                stockModelClassArrayList.clear();
                notifyDataSetChanged();
            }
        });

    }
}
0

There are 0 best solutions below