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