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

avec Java Discussion :

Exception in Thread


Sujet :

avec Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 4
    Par défaut Exception in Thread
    Bonjour,


    Je suis en train de crée un programme qui me permet de visualiser les photos en utilisant une base de donnés Mysql.

    J'ai crée un classe connexion qui contient des méthodes pour communiquer avec la base de données :

    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
    package ImagesBrowser;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
     
    public class Connexion {
     
    private Connection conn = null;
    private Statement smt = null;
    public static int retour;
     
     
    static {
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (InstantiationException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }
     
     
    public Connexion(final String host, final String login, final String pass, final String base) {
    try {
    conn = DriverManager.getConnection("jdbc:mysql://" + host + "/"
    + base + "?" + "user=" + login + "&password=" + pass);
    smt = conn.createStatement();
    retour=1;
    System.out.println("Ok");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
     
    public static int getRetour(){
    return retour;
    }
     
     
    public boolean executeInsert(String query) {
    try {
    return smt.execute(query);
    } catch (SQLException e) {
    System.err.println(query);
    e.printStackTrace();
    return false;
    }
    }
     
    public int executeUpdate(String query) {
    try {
    return smt.executeUpdate(query);
    } catch (SQLException e) {
    e.printStackTrace();
    System.err.println(e.getSQLState());
    System.err.println(e.getMessage());
    return -1;
    }
    }
     
    public ResultSet executeSelect(String query) {
    try {
    return smt.executeQuery(query);
    } catch (SQLException e) {
    e.printStackTrace();
    return null;
    }
    }
     
    public void printResultSet(ResultSet r) {
    try {
    r.beforeFirst();
    ResultSetMetaData meta = r.getMetaData();
    int nbCol = meta.getColumnCount();
    for (int i = 1; i < nbCol + 1; ++i) {
    if (i > 1)
    System.out.print(", ");
    System.out.print(meta.getColumnLabel(i));
    }
    System.out.println("");
    while (r.next()) {
    for (int i = 1; i < nbCol + 1; ++i) {
    if (i > 1)
    System.out.print(", ");
    System.out.print(r.getString(i));
    }
    System.out.println("");
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    public ResultSet executeSelect1(String query) {
    try {
    return smt.executeQuery(query);
    } catch (SQLException e) {
    e.printStackTrace();
    return null;
    }
    }
     
    public void printResultSet1(ResultSet r) {
    try {
    r.beforeFirst();
    ResultSetMetaData meta = r.getMetaData();
    int nbCol = meta.getColumnCount();
    for (int i = 1; i < nbCol + 1; ++i) {
    if (i > 1)
    System.out.print(", ");
    System.out.print(meta.getColumnLabel(i));
    }
    System.out.println("");
    while (r.next()) {
    for (int i = 1; i < nbCol + 1; ++i) {
    if (i > 1)
    System.out.print(", ");
    System.out.print(r.getString(i));
    }
    System.out.println("");
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
     
    public List<String> getSColumnFromResultSet(ResultSet r, String col) {
    List<String> l = new ArrayList<String>();
    try {
    r.beforeFirst();
    while (r.next())
    l.add(r.getString(col));
    }
    catch (SQLException e) {
    e.printStackTrace();
    }
    return (l);
    }
    }

    j'ai une autre classe AskAlbumPanel qui crée une fenêtre pour la demande du nom de l'album :


    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
    package ImagesBrowser;
     
    import java.sql.Statement;
     
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
     
     
    public class AskAlbumNamePanel extends JFrame implements ActionListener{
    private static final long serialVersionUID = 2763707468537479660L;
    private JButton boutonEnvoyer = new JButton("Envoyer");
    private JTextField NomAlbum = new JTextField();
    private JLabel EntrerNomAlbum = new JLabel("Entrer le nom de l'album :");
    private int largeur =400;
    private int hauteur = 200;
    private Connexion execute = null;
    public Statement stm = null;
     
     
    public AskAlbumNamePanel(){
    setTitle("Nom de l'Album");
    setSize(largeur,hauteur);
    JPanel p = new JPanel();
    JPanel p1 = new JPanel();
    p1.setBorder(BorderFactory.createEmptyBorder(10,0,30,0));
    JPanel p2 = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(NomAlbum,BorderLayout.CENTER);
    p.add(EntrerNomAlbum,BorderLayout.WEST);
    p.add(p1,BorderLayout.NORTH);
    p.add(p2,BorderLayout.SOUTH);
    JPanel pSud=new JPanel();
    p2.setBorder(BorderFactory.createEmptyBorder(10,0,40,0));
    pSud.add(p2,BorderLayout.NORTH);
    pSud.add(boutonEnvoyer,BorderLayout.SOUTH);
    p.add(pSud,BorderLayout.SOUTH);
     
    boutonEnvoyer.addActionListener(this);
     
    Container contentPane = getContentPane();
    contentPane.add(p);
    setResizable(false);
    setVisible(true);
    setLocationRelativeTo(null);
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
     
    public void actionPerformed(ActionEvent actionEvent) {
    if (actionEvent.getSource() == boutonEnvoyer) {
    final String albumName = NomAlbum.getText();
    if (albumName.length()==0){
    System.out.println("Pas de texte");
    setTitle("Erreur : Entrer un nom d'album !");
     
    }
    else{
    System.out.println(albumName);
    getTexte(albumName);
    String queryString = "INSERT INTO `Album` VALUES (NULL,`albumName`)";
    execute.executeInsert(queryString);
    setTitle("Nom de l'album envoyer !");
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    setTitle("Chargement ");
    try {
    Thread.sleep(300);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    setTitle("Chargement . ");
    try {
    Thread.sleep(300);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    setTitle("Chargement . . ");
    try {
    Thread.sleep(300);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    setTitle("Chargement . . . ");
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    dispose();
    }
     
    }
    }
     
    public String getTexte(String Texte){
    return Texte;
    }
     
    }
    Le problème c'est lorsque j'essaie d'envoyer le nom de l'album à partir de JTextField, on obtiens cette erreur :

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ImagesBrowser.AskAlbumNamePanel.actionPerformed(AskAlbumNamePanel.java:70)
    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)


    Pouvez vous m'éclaircir sur ce problème ?

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    at ImagesBrowser.AskAlbumNamePanel.actionPerformed(AskAlbumNamePanel.java:70)
    Tu a une erreur à la ligne 70 de ton code. En l'occurence ici, il semble que ton champ execute soit null, impossible donc d'appeler une méthode dessus.

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 4
    Par défaut
    donc en faite faut que j'appelle la classe Connexion (Connexion C = new Connexion(); ?
    Le problème c'est que le la classe Connexion() prend plusieurs argument (pour se connecter à la base de données), or ici je n'ai besoin que de la méthode :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public boolean executeInsert(String query) {
            try {
                return smt.execute(query);
            } catch (SQLException e) {
                System.err.println(query);
                e.printStackTrace();
                return false;
            }
        }

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Etonnament tu ne saura pas faire grand chose si tu n'est pas au préalable connecté à une base de données. Pour te connecter à un base de données, tu trouvera des FAQs ici:
    http://java.developpez.com/faq/jdbc/

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 4
    Par défaut
    Je suis déjà connecté à la base de données grâce à une méthode qui fait appel à un JButton (en retour, j'obtiens true).

  6. #6
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    tu dois stocker l'objet de connection dans ton champ connection. "true" ne veux rien dire et ne sert à rien, ta connection renvoyée par jdbc est un objet connection (voir la FAQ mentionnée)

Discussions similaires

  1. Réponses: 8
    Dernier message: 11/05/2006, 19h32
  2. [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
  3. [Classpath]Exception in thread "main" NoClassDefFound
    Par let_me_in dans le forum Général Java
    Réponses: 24
    Dernier message: 24/01/2006, 20h28
  4. Exception in thread "main" java.lang.ArrayIndexOut
    Par Poseidon62 dans le forum Concurrence et multi-thread
    Réponses: 6
    Dernier message: 04/11/2005, 01h38
  5. Réponses: 5
    Dernier message: 12/06/2002, 15h12

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