NullPointerException - exception handling

1

I had the following error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

This error is generated when generating a PDF file that is already open, if it is closed it creates it but if it is open it jumps that exception. It should be controlled.

My question is: Can a try include two catch ? Can two exceptions be within a catch?

Code :

private void button_listaActionPerformed(java.awt.event.ActionEvent evt) {                                             
        try {
            ArrayList<Cliente> clientes = Conexiones.listado_clientes();
            PDF_Clientes.crearPDF(clientes);
            JOptionPane.showMessageDialog(null, "PDF generado correctamente en directorio actual.");
        } catch (DocumentException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Error al crear el PDF.");
        } catch (NullPointerException npe) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, npe);
            JOptionPane.showMessageDialog(null, "El fichero está abierto. Debe cerrarlo.");
        }
    } 

How should it be controlled? Is the solution fine?

The JOptionPane messages work for me but I get the following error in the console / Output:

GRAVE: null
java.lang.NullPointerException
    
asked by omaza1990 17.01.2017 в 20:01
source

2 answers

5

If , it is possible and you can do it in the following ways:

  • catch (IOException ex) {
         logger.log(ex);
         throw ex;
    catch (SQLException ex) {
         logger.log(ex);
         throw ex;
    }
    
  • Valid from java 7

    catch (IOException|SQLException ex) {
            logger.log(ex);
            throw ex;
        }
    
  • Reference link for more information link

        
    answered by 17.01.2017 в 20:12
    0

    In addition to the answers you have already received, the error continues to come up in the console because you have a

    Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
    

    Leaving only the JOptionPane will prevent the error in the console.

        
    answered by 10.04.2017 в 03:02