IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException


Sujet :

JDBC Java

  1. #1
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    bonjour tout le monde,

    j'eesaye de créer une interface graphique pour me connecter avec mon login et mon mot de passe.
    Seulement, j'éprouve des difficultés pour créer une requete qui pourra juste consulter les données de ma table connexion pour voir si le login et le password existe.
    Je sais que c'est tout bete mais je me rapel plus comment traiter la consultation.

    voici ma classe ADO:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
     
     
    public class ADO {
     
    	private static final String DRIVER = "org.apache.derby.jdbc.ClientDriver";
    	private static final String URL = "jdbc:derby://localhost:1527/Diabetomedical";
    	private static final String USER = ";user=derby";
    	private static final String PWD = ";password=derby";
    	private Connection connection;
    	private String login;
    	private String password;
     
     
    	public void connexion(){
    		try{
    		Class.forName(DRIVER);
    		connection = DriverManager.getConnection(URL+USER+PWD);
     
    		}catch(ClassNotFoundException e){e.printStackTrace();
    		}catch(SQLException ex){ex.printStackTrace();}
     
    	}
    	public void deconnexion(){
    		try {
    			connection.close();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	public void consultation(){
    		request = connection.
     
     
    	}
     
     
     
    	}
    merci d'avance

  2. #2
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    Tu pourais faire un truc de ce genre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
     
    public void consultation(){
     
    		Statement stmt;
                    ResultSet result;
    		try {
                          stmt = connection.
                          result = stmt.executeQuery("SELECT COUNT(*) FROM connection where login='" + login + "' and password='" + password + "'");
                          if (result.getInt(1) >0)
                                  //connection OK
                          else
                                  //mauvais login et mdp
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
                    finally
                    {
                          try {
                          if (result != null)
                               result.close();
                          if (stmt!= null)
                               stmt.close();
                          } catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
                   }
     
     
    	}
    autant l'hiver éclate que l'hétéroclite
    le vrai geek c'est celui qui croit qu'il y a 1024 mètres dans un kilomètre

  3. #3
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    salut,
    je préfèrai plutot quelque chose de ce genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
     
    public void consultation(){
    		//Statement stmt;
            //ResultSet result;
            try {
            	Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery( "SELECT * FROM connexion" );
                    while ( rs.next() ) {
                        //int numColumns = rs.getMetaData().getColumnCount();
                        //peut causer probleme car il peut rechercher dans toutes les colonnes des ligne de connexion les infos fournies
                        while(rs.getObject("login").equals(fen.getLogin()) && rs.getObject("password").equals(fen.getPassword())){
                        	eco = new Ecouteur(3);
                        }
                       /* for ( int i = 1; i <= numColumns; i++ ) {
                           //Column numbers start at 1.
                           //Also there are many methods on the result set to return
                           // the column as a particular type. Refer to the Sun documentation
                           // for the list of valid conversions.
                           System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
                        }*/
                    }
     
            } catch(SQLException e) {
                e.printStackTrace();
            }
     
     
    	}
    merci quand meme

  4. #4
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    zut j'ai une erreur de ce type:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at modeles.Fenetre.actionPerformed(Fenetre.java:162)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

    voici ma classe qui pose pb:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    package modeles;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    import ado.ADO;
    
    
    
    @SuppressWarnings("serial")
    public class Fenetre extends JPanel implements ActionListener{
    
    	private JPanel jpa1, jpa2;
    	private JLabel jla1, jla2;
    	private JTextField login;
    	private JPasswordField password;
    	private JTabbedPane jtp;
    	private JButton bvalider;
    	private ImageIcon icon;
    	ADO ado;
    
    	public Fenetre(JPanel jpa1, JPanel jpa2, JLabel jla1, JLabel jla2,
    			JTextField login, JPasswordField password, JTabbedPane jtp,
    			JButton bvalider, ImageIcon icon, ADO ado) {
    		super();
    		this.jpa1 = jpa1;
    		this.jpa2 = jpa2;
    		this.jla1 = jla1;
    		this.jla2 = jla2;
    		this.login = login;
    		this.password = password;
    		this.jtp = jtp;
    		this.bvalider = bvalider;
    		this.icon = icon;
    		this.ado = ado;
    	}
    	
    	public Fenetre() {
    		super(new BorderLayout());
    		
    		jtp = new JTabbedPane(JTabbedPane.TOP);
    		icon = createImage("/connect.png");
    		
    		//JComponent p1 = makeTextPanel("Connexion");
    		//jtp.setMnemonicAt(1, KeyEvent.VK_1);
    		
    		jpa1 = new JPanel();
    		jpa1.setLayout(new GridLayout(2,2));
    		jpa2 = new JPanel();
    		jpa2.setLayout(new FlowLayout());
    		
    		jla1 = new JLabel("Login: ");
    		jla2 = new JLabel("Password: ");
    		login = new JTextField(10);
    		password = new JPasswordField(10);
    		bvalider = new JButton("Submit");
    		jpa1.add(jla1);
    		jpa1.add(login);
    		jpa1.add(jla2);
    		jpa1.add(password);
    		jpa2.add(jpa1);
    		jpa2.add(bvalider);
    		
    		//p1.add(jpa2);
    		jtp.addTab("Connexion", icon,jpa2);
    		
    		bvalider.addActionListener(this);
    		//login.addActionListener(new Ecouteur(2));
    		//password.addActionListener(new Ecouteur(2));
    		
    		
    		//add the tabbedpane to this panel
    		add(jtp);
    		
    		//the following line enables to use scrolling tabs
    		jtp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    		
    	}
    	protected JComponent makeTextPanel(String text) {
    		JPanel panel = new JPanel(false);
    		JLabel filler = new JLabel(text);
    		filler.setHorizontalAlignment(JLabel.CENTER);
    		panel.setLayout(new GridLayout(1, 1));
    		panel.add(filler);
    		return panel;
    	}
    	/** Returns an ImageIcon or null if the path was invalid.*/
    	protected static ImageIcon createImage(String path) {
    		java.net.URL imgURL = Fenetre.class.getResource(path);
    		if(imgURL != null){
    			return new ImageIcon(imgURL);
    		}else{
    		System.err.println("couldn't find file: "+path);
    		return null;
    	}
    	}
    	/**Create the GUI and show it. For thread safety.
    	 * this method should be invoked from
    	 * the event dispatch thread.
    	 */
    	private static void createAndShowGUI(){
    		//create and set up the window
    		JFrame frame = new JFrame("DiabetoMédical");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//Add content ti the window.
    		frame.add(new Fenetre(), BorderLayout.CENTER);
    		
    		//Display the window
    		frame.pack();
    		frame.setVisible(true);
    	}
    	public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    //Turn off metal's use of bold fonts
    		UIManager.put("swing.boldMetal", Boolean.FALSE);
    		createAndShowGUI();
                }
            });
        }
    	
    	public String getLogin(){
    		return login.getSelectedText();
    	}
    	public String getPassword(){
    		return password.getSelectedText();
    	}
    	public void OngletAccueil(){
    		JComponent p2 = makeTextPanel("Accueil");
    		jtp.addTab("Accueil", p2);
    	}
    	public void OngletAjoutPatient(){
    		JComponent p3 = makeTextPanel("Ajout d'un patient");
    		jtp.addTab("Ajout d'un patient", p3);
    	}
    	public void OngletAjoutSuiviPatient(){
    		JComponent p4 = makeTextPanel("Suivi Patient");
    		jtp.addTab("Suivi Patient", p4);
    	}
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		Object source = e.getSource();
    		
    		if(bvalider ==source){
    			ado.connexion();
    			ado.consultation();
    			ado.deconnexion();
    		}	
    	}
    		
    }
    Je sais pas pourquoi j'ai cette erreur.
    Quelqu'un sait-il ce qui ne va pas ?
    merci.

  5. #5
    Membre chevronné Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Points : 2 232
    Points
    2 232
    Par défaut
    La ligne qui pose problème c'est
    tu as une erreur parce que ado n'a pas été initialisé dans ton constructeur Fenetre().
    autant l'hiver éclate que l'hétéroclite
    le vrai geek c'est celui qui croit qu'il y a 1024 mètres dans un kilomètre

  6. #6
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    ah oui merci beaucoup !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 4
    Dernier message: 08/02/2010, 08h23
  2. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException ?
    Par freezerhm dans le forum Concurrence et multi-thread
    Réponses: 5
    Dernier message: 04/12/2007, 09h26
  3. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par Trint dans le forum Interfaces Graphiques en Java
    Réponses: 6
    Dernier message: 27/02/2007, 11h28
  4. Réponses: 8
    Dernier message: 11/05/2006, 19h32
  5. [JDIC]Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Par kedare dans le forum Concurrence et multi-thread
    Réponses: 4
    Dernier message: 06/05/2006, 22h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo