Converted a Resulset to an Object when returning the object returns a NullPointerException? with JDBC

0
public TaxCategory findByName(int taxId) {
         TaxCategory taxCategory=null;
         Statement stmt = null;
         ResultSet rst = null;
                Connection acceDB = conexion.getConexion();

      String query ="Select *  from tax_categories where tax_category_id=" + taxId;
  try{
             stmt = acceDB.createStatement();


      rst =  stmt.executeQuery(query);

                       if(rst.next()){
             taxCategory= new TaxCategory();
             taxCategory.setTaxCategoryId(rst.getInt("tax_category_id"));
             taxCategory.setTaxCategoryName(rst.getString("tax_category_name")); 
             } else {
                                  }

  }catch(SQLException | java.lang.NullPointerException ex ){
JOptionPane.showMessageDialog(null,ex.getMessage());
  } finally{
         try {
             if(rst !=null){
                 rst.close();
             }

         } catch (SQLException ex) {
             Logger.getLogger(TaxCategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
         }

         if(stmt != null){
             try {
                stmt.close();
             } catch (SQLException ex) {
                 Logger.getLogger(TaxCategoryDAO.class.getName()).log(Level.SEVERE, null, ex);
             }
         }
  }

    return taxCategory;  
 }

Now I'm going to use the function with a JList

frmTax.jList1.addListSelectionListener((ListSelectionEvent e) ->{

     Tax tax = null;
     TaxCategory taxCategory = null;

    frmTax.jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);



      tax = (Tax) frmTax.jList1.getSelectedValue();



         taxCategory = modeloTaxCategory.findByName(tax.getTax_category_id());
    frmTax.jComboBox1.setSelectedItem(taxCategory);




     });
    
asked by george garcia 11.04.2017 в 15:31
source

3 answers

1

The taxCategory object that you create to null, you initialize it within the if , which means that if the query does not have any results you will return null . I have seen that in the finally you close the ResultSet and the Statement, but you never close the database connection and that is a bad practice that can give you errors in many occasions since each time you execute the method a connection is opened. saves in memory until the Garbage Collector . Try closing the connection in the finally like this: acceDB.Close(); After the if in which you check if there is something in the ResultSet, if you are not going to do anything, you should delete the empty Else that you have written.

    
answered by 11.04.2017 / 16:57
source
2

Return taxCategory but do not always believe it. You only create taxCategory when rst.next () returns true but when the query result is empty rst.next () returns false and taxCategory is not created. You must decide what to do when the result of the query is empty.

    
answered by 11.04.2017 в 17:01
0

If your tax_category_id field is varchar type, try to put your taxId in quotation marks.

Now instead of using Statement, try using PreparedStatement, like this:

PreparedStatement preparedStatement = null;
preparedStatement = acceDB.prepareStatement(query);
rst=preparedStatement.executeQuery();
while( rst.next() ) {
taxCategory= new TaxCategory();
             taxCategory.setTaxCategoryId(rst.getInt("tax_category_id"));
             taxCategory.setTaxCategoryName(rst.getString("tax_category_name")); 
}
//Comprobamos que no venga vacío tu objeto
if(taxCategory != null && !taxCategory.getTaxCategoryName.equals(""){
//Aquí pones algo en caso de que no venga vacío
}

Here I leave the documentation

    
answered by 11.04.2017 в 16:21