Question

I'm new to ubuntu, and in the last days I installed eclipse and lamp for working with a project that was already running on windows7. The problem is that the same string of connection doesn't works. Here is the piece of code in witch I try to connect:

    String stringaConn = null;
        String nomeClasse = null;
        stringaConn = "jdbc:mysql://localhost/cinemapreverificasql?user=root";
        nomeClasse = "com.mysql.jdbc.Driver";
        try {
            Class.forName(nomeClasse);
            System.out.println("forName eseguito");
        } catch (ClassNotFoundException e) {
            System.out.println("Classe nomeClasse - Driver non trovata");
        }
        try {
            f.conn = DriverManager.getConnection(stringaConn);
            System.out.println("Aperta la connessione");
        } catch (SQLException e) {
            System.out.println("Stringa di connessione errata");
        }

It enters in the second catch and prints the string "Stringa di connessione errata". Here is an example of output:

forName eseguito
Stringa di connessione errata
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at gestioneDegliEventi.Gestione.windowOpened(Gestione.java:199)
    at java.awt.Window.processWindowEvent(Window.java:2048)
    at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
    at java.awt.Window.processEvent(Window.java:2009)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Searching on internet I tried the solution of changing the connection string in:

jdbc:mysql://localhost:3306/cinemapreverificasql?user=root

and give the permission with sudo ufw allow 3306, but the error risen is the same. What could be the error?

Was it helpful?

Solution

Okay, so now that you've shown the line that's actually blowing up:

stmSql = f.conn.prepareStatement("select * from film");

And f.conn is null. That's because when you assign a value to f.conn, if anything goes wrong you print out an error message and then keep going as if nothing had happened:

try {
    f.conn = DriverManager.getConnection(stringaConn);
    System.out.println("Aperta la connessione");
} catch (SQLException e) {
    System.out.println("Stringa di connessione errata");
}

Don't do that. Don't catch the exception here. You're really not handling it properly, in that after the catch statement your object is not ready to be used. You're also swallowing the exception without displaying any of the information in it, which makes it even hard to work out what's really wrong.

The method you've shown should probably just declare that it can throw SQLException, and then not try to catch anything. That way, if something goes wrong you stop immediately rather than continuing when you're bound to fail.

When you log the exception wherever you actually log it, log the whole exception - not just a message saying that something's wrong. It's entirely possible that the exception information will tell you exactly what's wrong with your connection string.

Oh, and you probably shouldn't be doing any of this in the UI thread, either - but that's a very separate issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top