Want to set Visibility of Recycle View in other Function But it shows NullPointerExeception

33 Views Asked by At

In MainActivity.java onCreate function I have a setup an Recycler View as suggestList, and using it as for Search Suggestion. However, I want to set it's .setVisibility(View.GONE) (when search suggestion is complete), as beneath it I have a recyclerView as (stockPriceList) which shows whatever is added from suggestList to it, and have created a function outside onCreate as StockPrice() in which I am trying to set the visibility of suggestList to GONE but it shows NullPointerExeception. also I am calling the StockPrice in StockPriceAdapter when someone clicked on any item.

Here is the SS of Error

MainActivity.java

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, this);

    suggestList.setAdapter(suggestAdapter);

    searchBar.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            loadStockApi();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    // 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);

}

public void StockPrice(RecyclerView suggestList) {

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

    stockPriceList = new ArrayList<>();

    if (stockPriceList != null) {
        // It's working
        stockPriceList.add(new defualtWatchListModel(name, Symbol, price));
        suggestList.setVisibility(View.GONE); //This Where I see the NullPointerExeception Error.
    }
}

Here is ViewHolderClass of Adapter used for suggestList (recyclerView) in this I have called the stockPrice() function.

 public class ViewHolder extends RecyclerView.ViewHolder {

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

        name = itemView.findViewById(R.id.name);
        symbol = itemView.findViewById(R.id.symbol);
        exchange = itemView.findViewById(R.id.exchange);
        type = itemView.findViewById(R.id.type);
        listContainer = itemView.findViewById(R.id.search_item_views);

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

               itemOnClickListener.OnClickItemViewListener(getAbsoluteAdapterPosition());
               // Run this one
               MainActivity mainActivity = new MainActivity();
               mainActivity.StockPrice(mainActivity.suggestList);

            }
        });

    }
}

I am trying to set Visibility of suggestList (RecyclerView) to GONE in same class but another Function.

0

There are 0 best solutions below