0

I am trying to communicate with DAO class through getters/setters either providing or fetching data to DAO. I am beginner and here is what I did so far:

public class Bean {
private String[] categories;

public Bean(String[] categories) {
    super();
    this.categories = categories;
}

protected String[] getCategories() {
    return categories;
}

protected void setCategories(String[] categories) {
    this.categories = categories;
    System.out.print(categories);
}
}

Here is DAO code:

> // INSERT
    protected boolean insert(Bean b) throws SQLException {
        int[] result = null;
        String[] categories = b.getCategories();
        
        try {
            cnn = getConnection();
            ps = cnn.prepareStatement(INSERT);
            for (int i = 0; i < categories.length; i++) {
                System.out.print(categories[i]);
                if (categories[i] != null ) {
                    ps.clearParameters();
                    ps.setArray(1, cnn.createArrayOf("VARCHAR", categories));
                    ps.addBatch();
                }
                result = ps.executeBatch();
            }
            ps.clearBatch();
            cnn.commit();
            

I am getting parameter values in servlet get method as:

String[] categories = request.getParameterValues("category");

In controller servlet I am writing code like this:

Bean b = new Bean(categories);
    b.setCategories(categories);
    DAO db = new DAO();
    try {
        boolean inserted = db.insert(b);
        if(inserted) {
            out.print(inserted);
        }
        else {
            out.print(inserted);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

The above code is throwing the following error:

[Ljava.lang.String;@7c5da1becom.hello.user.categories.DAO

java.lang.NullPointerException: Cannot invoke "com.hello.commons.DBController.getJdbcDriver()" because "dbc" is null Feb 11, 2021 10:02:31 AM org.apache.catalina.core.StandardWrapperValve invoke

I am sure there are many mistakes. Being a beginner and learner please correct my code and advise best standards

Best regards

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Murteza
  • 57
  • 9
  • This is so confusing I cant even trace it... what is dbc ? Can you please paste the actual error you are getting.... and I want to see this DBController cod. <-- dbc is probably your instance of DBCController <-- That is null.... – JCompetence Feb 11 '21 at 07:21
  • It is telling you that the variable dbc is null, that is why it cant call the getJdbcDriver() method on it... – JCompetence Feb 11 '21 at 07:22

0 Answers0