Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175

    Errore inspiegabile (entra nel catch, nn so perchè)...

    Scusatemi il titolo, ma non saprei cosa scrivere
    Ho una classe che si chiama MyDBConn.java, al suo interno ho un metodo che si chiama GetResultFromPazienti();


    File: MyDBConn.java:
    codice:
    package cc;
    
    import java.sql.*;
    
    public class MyDBConn {
        private Connection myConnection;
        private java.sql.Statement stmt;
    
    // ...
    
    public ResultSet getResultFromPazienti2(String query) {
            ResultSet rs=null;
            try{
                rs=stmt.executeQuery(query);
            }
            catch(Exception e){
                alerts.showErr("---- ERRORE ----");
            }
            return rs;
        }
    Inoltre ho una classe chiamata Paziente.java, da questa classe vorrei richiamare il metodo getResultFromPazienti2(), questo è il codice della classe Paziente:

    File: Paziente.java
    codice:
    package cc;
    import java.sql.*;
    
    public class Paziente extends javax.swing.JFrame {
            private MyDBConn mdbc;
            private java.sql.Statement stmt;
    
    // ...
    // ...
    
        private void formWindowOpened(java.awt.event.WindowEvent evt)
    {
            ResultSet rs=mdbc.getResultFromPazienti2("select ... from ...
    where ...");
    
            try {
                rs.next();
                     // ...
                     txtNome.setText(rs.getString("NOME"));
                     // ...
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    Al momento della compilazione nn sembra esserci alcun problema infatti il package viene compilato senza errori. Nel momento in cui faccio partire il main project e viene eseguito il metodo formWindowOpened() viene mostrato il messaggio: "Exception in thread "AWT-
    EventQueue-0" java.lang.NullPointerException", oltre questo viene mostrato anche il messaggio "---- ERRORE ----", quindi la funzione getResultFromPazienti2 viene richiamata, ma qual'è l'errore che mi fà entrare nella sezione catch del metodo getResultFromPazienti2???

  2. #2
    Non avendo postato tutto il codice sembrerebbe che la variabile stmt in MtDBConn non sia inizializzata, pertanto è null e quando tenti di eseguire executeQuery scatta l'eccezione. Se non è così posta il codice completo e magari anche lo stackTrace dell'eccezione.
    Al mio segnale... scatenate l'inferno!

  3. #3
    In effetti, così è un po' difficile capire da che cosa possa derivare l'eccezione.
    Aggiungi questa riga prima dell'alert attuale:
    codice:
    System.out.println( e.printStackTrace() );
    E vedi che cosa viene stampato nella console.

    Ciao,

  4. #4
    Originariamente inviato da interarete
    In effetti, così è un po' difficile capire da che cosa possa derivare l'eccezione.
    Aggiungi questa riga prima dell'alert attuale:
    codice:
    System.out.println( e.printStackTrace() );
    E vedi che cosa viene stampato nella console.

    Ciao,
    Senza System.out... solo e.printStackTrace() ;-)
    Al mio segnale... scatenate l'inferno!

  5. #5
    Originariamente inviato da R@ve M@ster
    Senza System.out... solo e.printStackTrace() ;-)
    E sì, mi sa che vista l'ora stavo già dormendo... :-)

  6. #6
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175
    Ecco il codice completo del file MyDBConn.java (ditemi se serve anche il file che richiama la funzione):
    codice:
    package cc;
    
    import java.sql.*;
    
    public class MyDBConn {
        private Connection myConnection;
        private java.sql.Statement stmt;
        
            
        /** Creates a new instance of MyDBConn */
        public MyDBConn() {
        }
        
        public void init(){
        
           try{
            Class.forName("Driver");
            myConnection=DriverManager.getConnection(
                    "URL", "user", "pass"
                    );
            }
            catch(Exception es){
                alerts.showErr(es.getMessage());
                
            }
        }
        
    
        public ResultSet getResultFromPazienti2(String query) {
            ResultSet rs=null;
            try{
                rs=stmt.executeQuery(query);
            }
            catch(Exception e){
                e.printStackTrace();
            }
            return rs;
        }
    
        ///////////// RESULT
        
        
        public Connection getMyConnection(){
            return myConnection;
        }
         
        public void close(ResultSet rs){
            
            if(rs !=null){
                try{
                   rs.close();
                }
                catch(Exception e){}
            
            }
        }
        
         public void close(java.sql.Statement stmt){
            
            if(stmt !=null){
                try{
                   stmt.close();
                }
                catch(SQLException e){}
                System.out.println("ERRORE");
            }
        }
         
      public void destroy(){
      
        if(myConnection !=null){
        
             try{
                   myConnection.close();
                }
                catch(Exception e){}
            
            
        }
      }   
    }
    E questo è l'output:
    codice:
    java.lang.NullPointerException
            at cc.MyDBConn.getResultFromPazienti2(MyDBConn.java:51) (corrisponde alla riga rossa del file sopra)
            at cc.Paziente.formWindowOpened(Paziente.java:522)
            at cc.Paziente.access$000(Paziente.java:14)
            at cc.Paziente$2.windowOpened(Paziente.java:108)
            at java.awt.Window.processWindowEvent(Window.java:1187)
            at javax.swing.JFrame.processWindowEvent(JFrame.java:266)
            at java.awt.Window.processEvent(Window.java:1148)
            at java.awt.Component.dispatchEventImpl(Component.java:3955)
            at java.awt.Container.dispatchEventImpl(Container.java:2024)
            at java.awt.Window.dispatchEventImpl(Window.java:1778)
            at java.awt.Component.dispatchEvent(Component.java:3803)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
            at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at cc.Paziente.formWindowOpened(Paziente.java:525)
            at cc.Paziente.access$000(Paziente.java:14)
            at cc.Paziente$2.windowOpened(Paziente.java:108)
            at java.awt.Window.processWindowEvent(Window.java:1187)
            at javax.swing.JFrame.processWindowEvent(JFrame.java:266)
            at java.awt.Window.processEvent(Window.java:1148)
            at java.awt.Component.dispatchEventImpl(Component.java:3955)
            at java.awt.Container.dispatchEventImpl(Container.java:2024)
            at java.awt.Window.dispatchEventImpl(Window.java:1778)
            at java.awt.Component.dispatchEvent(Component.java:3803)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
            at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

  7. #7
    L'eccezione che ottieni (NullPointerException) indica che stai tentando di chiamare un metodo di un oggetto che in quel momento è null. Nella riga in cui si verifica l'eccezione l'unico oggetto su cui invochi un metodo è stmt; e infatti stmt è null, perché non lo istanzi da nessuna parte.

    Devi istanziarlo così:
    codice:
    stmt = myConnection.createStatement();
    Ciao,

  8. #8
    Utente di HTML.it
    Registrato dal
    Sep 2000
    Messaggi
    1,175
    Grazie mille a tutti, ora funziona perfettamente!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.