Error showing BD data in Java

2

I have the following:

Home

 public Principal() {
        tabla = new DefaultTableModel(null, getColumnas());
        setFilas();
        initComponents();
    }

    private String[] getColumnas(){
        String columna[]= new String[]{"titulo","descripcion","observacion","tipo"};
        return columna;
    }
    private void setFilas(){

        try{
            String consulta = "Select titulo_incidencia, decripcion_incidencia, observcion,tipo from incidencias";
            PreparedStatement ps = con.conexionBD().prepareStatement(consulta);
            ResultSet res = ps.executeQuery();

            Object datos[] = new Object[4];
            while(res.next()){
            for(int i=0; i<4;i++){
                   datos[i] = res.getObject(i+1);
            }
            tabla.addRow(datos);
            }
            res.close();
        } catch(SQLException ex){
          Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Connection

public class MariaDBConexion {


    static String user="root";
    static String pass="xxxx";
    static String url="jdbc:mariadb://localhost:3306/asistente";
    public Connection con = null;

    public Connection conexionBD(){
        try {

            try {
                 Class.forName("org.mariadb.jdbc.Driver"); 
            } catch (ClassNotFoundException ex) {
                System.out.println("Error al registrar el driver de MariaDB: " + ex);
            }
           Connection connection = DriverManager.getConnection(  
                url, user, pass);  
            boolean valid = connection.isValid(50000);
            System.out.println(valid ? "Conexión exitosa" : "conexión fallida");
        } catch (java.sql.SQLException sqle) {
            System.out.println("Error: " + sqle);
        }
    return con;
    }

When I run it, I skip the following:

  

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException       at ayudantesistemas.Principal.setFilas (Principal.java:36)

In this line:

    PreparedStatement ps = con.conexionBD().prepareStatement(consulta);

Could you tell me my mistake?

    
asked by ReD 01.12.2016 в 01:25
source

2 answers

2

In the MariaDBConexion class, at the end of the conexionBD() method, you execute the following statement:

return con;

but con is a global variable that is apparently in null . Most likely, you wanted to do:

return connection;

It would be good to understand why there is that global variable con that is causing confusion. You should probably remove it completely.

    
answered by 01.12.2016 в 01:33
1

Thanks to @sstan's response, I have changed from:

 Connection connection = DriverManager.getConnection(  
                url, user, pass);  

a

  con =  DriverManager.getConnection(  
                url, user, pass);  

I used to get null since the global variable con has that default value and will change its value by passing the connection parameters, but with the change that I made already works correctly for me.

    
answered by 01.12.2016 в 01:51