skip to Main Content

I have a app that read data from SQL and I want to show in a recyclerview. but I have a problem cause to dosen’t show data in list by this warning:

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)’ on a null object reference

public class Remain extends AppCompatActivity {
RecyclerView recyclerViewMain;
ArrayList<ProducModel> producModels;
private Context mContext;
ProductAdapter productAdapter;
Connection connect;
ResultSet rs;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_remain);

    connect = DBHelpers.GetConnection();
   
    SeachProduct();

    recyclerViewMain = findViewById(R.id.ProductSearch_lv2);

}

private void SeachProduct() {

    //internet checking
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    assert cm != null;
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null) {
        Toast.makeText(Remain.this, "please check the internet", Toast.LENGTH_LONG).show();
    } else {

        String query = "exec [Android].[spAgentINV] " + ID +",''"; 

        //Connect to SQL server and read data
        try {

            Statement statement = connect.createStatement();
            rs = statement.executeQuery(query);
            producModels = null;
            producModels = new ArrayList<ProducModel>();

            while (rs.next()) {
                producModels.add(new ProducModel(Integer.parseInt(rs.getString("PartRef")) ,5 ,(int)(rs.getFloat("Rate")), (int) (rs.getFloat("finalQty")) , 1, rs.getString("PartName"),0, false));
              

 }

            productAdapter = new ProductAdapter(mContext, producModels);
            recyclerViewMain.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerViewMain.setAdapter(productAdapter);

         
        } catch (Exception e) {
            Toast.makeText(Remain.this, e + "", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }

}

please help

2

Answers


  1. at first get reference to RecyclerView, at the beginning it is null and you can’t call any methods of it

    recyclerViewMain = findViewById(R.id.ProductSearch_lv2);
    

    and AFTER that make operations on it, as it is not null anymore, so call

    SeachProduct();
    

    so to sum up:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_remain);
        recyclerViewMain = findViewById(R.id.ProductSearch_lv2);
    
        connect = DBHelpers.GetConnection();
       
        SeachProduct();
    }
    
    Login or Signup to reply.
  2. The variable recyclerViewMain has not been assigned a value before it is used.

    You should initialize recyclerViewMain before calling SeachProduct method

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remain);
        recyclerViewMain = findViewById(R.id.ProductSearch_lv2);
        connect = DBHelpers.GetConnection();
        SeachProduct();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search