Question

Hi i am trying to filter products by BRAND,FLAVOR and display them on page.For that i have used check box for selecting different Brands and Flavors. for that i have used a function in which i have written a SOL query. this functions works fine if i select both the options BRANDS and FLAVOR but when i filter by only Brands or Flavor It gives me NULL POINTER EXCEPTION. CODE IS AS FOLLOWS:

 public List<Products> DisplayProducts2(String[] a, String b[]) {

    ResultSet rs;
    List<Products> data = null;
    PreparedStatement stmt;
    try {
        StringBuilder param = new StringBuilder();
        for (String str : a) {
            param.append("'").append(str).append("', ");
        }
        StringBuilder param1 = new StringBuilder();
        for (String str : b) {
            param1.append("'").append(str).append("', ");
        }

        String query = "select  * from products where Brand in (" + param.substring(0, param.length() - 2) + ") or Flavour in (" + param1.substring(0, param1.length() - 2) + ")";
        stmt = DataBaseConnection.DBConn.getConnection().prepareStatement(query);
        rs = stmt.executeQuery();
        if (rs != null) {
            data = new ArrayList<Products>();
            while (rs.next()) {
                Products p = new Products();
                p.setTitle(rs.getString("Ttile"));
                p.setCategory(rs.getString("Category"));
                p.setSubCategory(rs.getString("SubCategory"));
                p.setSubCategoryTwo(rs.getString("SubCategorytwo"));
                p.setPrice(rs.getInt("Price"));
                p.setFlavour(rs.getString("Flavour"));
                p.setSize(rs.getString("Size"));
                p.setImage(rs.getString("image"));
                p.setBrand(rs.getString("Brand"));
                p.setInstock(rs.getString("instock"));
                p.setInstockQty(rs.getInt("instockqty"));
                data.add(p);
            }
        }
    } catch (Exception e) {

        System.out.println(e.getStackTrace());
        System.out.println("----------------------");
        System.out.println(e.getMessage());
        System.out.println("----------------------");
        System.out.println(e.getSuppressed());
        System.out.println("----------------------");
        e.printStackTrace();
        return null;
    }
    return data;

}

i Tried to use if condition to check which value is null for that purpose there would be so many comparisons.. Please tell how do i do

Was it helpful?

Solution

change

StringBuilder param = new StringBuilder();
        if (a != null) {
            for (String str : a) {
                param.append("'").append(str).append("', ");
            }
        }
        StringBuilder param1 = new StringBuilder();
        if (b != null) {
            for (String str : b) {
                param1.append("'").append(str).append("', ");
            }
        }

        String prm = param.toString().length() > 2 ? param.toString()
                .substring(0, param.toString().length() - 2) : null;
        String prm1 = param1.toString().length() > 2 ? param1.toString()
                .substring(0, param1.toString().length() - 2) : null;

        String query = "select  * from products where ";

        if (prm != null && prm1 != null) {
            query += "Brand in (" + prm + ") or Flavour in (" + prm1 + ")";
        } else if (prm != null && prm1 == null) {
            query += "Brand in (" + prm + ")";
        } else if (prm1 != null && prm == null) {
            query += "Flavour in (" + prm1 + ")";
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top