1

I'm fairly new to programming and trying to connect to a DB using HTML, I'm using Wildfly, Apache NB and Derby in this case, hosted locally, the thing is that the browser gives me the following error after I try to log in with the credentials that I have created in the DB: [Login screen1

Error

java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because the return value of "model.Conexion.getConexion()" is null

So I went to NB Conexion.java to check the getter but I cannot seem to find what is the issue here is the code.

Also, I have tried this with different versions of JDK, JRE more specifically u144 and u271 tried with GlassFish5, Payara and Wildfly and still no luck.

I would really appreciate any help on this as my semester depends a lot on this

package model;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexion {

    private static Conexion conexion;
    private static final String BDURL= "jdbc:derby://localhost:1527/proyectohtml;user=ROOT;password=1234";
    private static Connection conn;

    private Conexion(){
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver").getDeclaredConstructor().newInstance();
            conn=DriverManager.getConnection(BDURL);
        } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }

    public static synchronized Connection getConexion() {
        if (conexion==null) {
            conexion=new Conexion();
        }
        return conn;
    }
}
acm
  • 2,086
  • 3
  • 16
  • 34
RobC
  • 11
  • 2
  • By the way, unless this is for a specific assignment I highly recommend not managing low-level details like this yourself. Spring Boot, for example, takes your connection string as a line in a config file (or an environment variable)... and then you're done. It's all handled for you, including connection pooling, driver management, and so on. – chrylis -cautiouslyoptimistic- Nov 19 '20 at 22:16
  • hi thanks! yeah at the moment im being asked to connect using these details, i knew there was an easier way but if I do it like that im going to get penalized – RobC Nov 19 '20 at 22:25
  • I will suggest a hint in your debugging: What are you initializing in your method, and what are you returning? – chrylis -cautiouslyoptimistic- Nov 19 '20 at 22:26
  • i am initializing conn and BDURL String to send it to the DB, im not sure what part of my code is wrong, as for the server the creds are the same I made the server and all – RobC Nov 19 '20 at 22:30
  • You're initializing `conn`? Where? (Note, by the way, that this `Class.forName` business has been obsolete for 13 years. It's entirely a magic spell that is unnecessary with `DriverManager`.) – chrylis -cautiouslyoptimistic- Nov 19 '20 at 22:47
  • I think here conn=DriverManager.getConnection(BDURL); I know im using old stuff this school program sucks – RobC Nov 19 '20 at 22:54
  • And if that doesn't work, what do you do? – chrylis -cautiouslyoptimistic- Nov 19 '20 at 22:56
  • im not sure, im lost, im banging my head on the keyboard – RobC Nov 19 '20 at 23:01
  • https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – chrylis -cautiouslyoptimistic- Nov 19 '20 at 23:03

0 Answers0