Constructor threw exception; nested exception is java.lang.NullPointerException

0

I am trying to connect to my DB by MySQL pe I get that error of Constructor threw exception; nested exception is java.lang.NullPointerException and do not access any parameter:

jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/facturacion?zeroDateTimeBehavior=convertToNull
jdbc.username = myuser
jdbc.password = mypassword
Caused by: java.lang.NullPointerException
	at ifac.Config.MySQL.<init>(MySQL.java:43)
	at ifac.Config.MySQL$$EnhancerBySpringCGLIB$$63df6b53.<init>(<generated>)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
	... 63 more
@Configuration
@EnableTransactionManagement
@Component
@PropertySource(value = {"classpath:application.properties"})
public class MySQL {

    @Autowired
    private Environment environment;

    private Connection conexion = null;

    public MySQL() {
        System.out.println("...........................................");
        System.out.println("...........................................");
//        System.out.println(environment.getProperty("jdbc.username"));
//        System.out.println(environment.getProperty("jdbc.password"));
//        System.out.println(environment.getProperty("jdbc.driverClassName"));
//        System.out.println(environment.getProperty("jdbc.url"));
        System.out.println("..........................................");
        System.out.println("..........................................");
        try {
            String usuario = environment.getRequiredProperty("jdbc.username");
            String password = environment.getRequiredProperty("jdbc.password");
            Class.forName(environment.getRequiredProperty("jdbc.driverClassName"));
            String url = environment.getRequiredProperty("jdbc.url");
            conexion = (Connection) DriverManager.getConnection(url, usuario, password);
        } catch (SQLException | ClassNotFoundException ex) {

        }
    }

    public Connection getConexion() {
        return conexion;
    }

    public Connection cerrarConexion() {
        try {
            conexion.close();
        } catch (SQLException ex) {
            System.out.println(ex);
        }
        conexion = null;
        return conexion;
    }
}
    
asked by user75463 20.08.2018 в 18:09
source

1 answer

0

Taken from link

NOTE: The DataSource interface, new in the JDBC 2.0 API, provides another way to connect to a data source. The use of a DataSource object is the preferred means of connecting to a data source.

As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property. This allows a user to customize the JDBC Drivers used by their applications. For example in your ~/.hotjava/properties file you might specify:

 jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver


The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

 my.sql.Driver

Look to see if a line connection is returned to you:

conexion = (Connection) DriverManager.getConnection(url, usuario, password);

And already put something in the catch so that if you get an error you find out ;-) (a System.out.println () even though)

    
answered by 21.08.2018 в 18:48