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

 Java Discussion :

les objets ObjectInputStream : Exception "NullPointerException "


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2009
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Mai 2009
    Messages : 34
    Par défaut les objets ObjectInputStream : Exception "NullPointerException "
    Bonjour,
    voila j'ai un probleme avec la manipulation des fichier sous java ,
    je suis entrain de creé un iditeur UML (pour constuir des diagramme).
    pour chaque activité il faut que le programme récupére des information et les enregistrer dans un fichier object ;
    ,et j'ai creé une botton pour modifier les information d'une activité , pour cela il faux récuperé les information déja donné et les modifié ,
    voila la classe pour récupéré les information :
    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
    169
    170
    171
    172
    173
    174
    175
    176
    177
    import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.GridLayout; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.io.Serializable; 
     
    import javax.swing.BorderFactory; 
    import javax.swing.Icon; 
    import javax.swing.ImageIcon; 
    import javax.swing.JButton; 
    import javax.swing.JDialog; 
    import javax.swing.JLabel; 
    import javax.swing.JOptionPane; 
    import javax.swing.JScrollPane; 
    import javax.swing.JTextArea; 
    import javax.swing.JTextField; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
     
    public class propo extends JDialog implements Serializable{ 
     
     
     
     
    private JLabel nomLabel, sourceLabel, capaLabel, infoLabel; 
    private transient JTextField nom; 
    private JTextArea capa,source,info; 
    private JScrollPane capascroll,sourcescroll,infoscroll; 
    private String inf=new String(); 
    private JOptionPane msg = new JOptionPane(); 
    private boolean act=false; 
     
     
     
     
    public propo(JFrame parent, String title, boolean modal){ 
     
    super(parent, title, modal); 
    this.setSize(600, 600); 
    this.setLocationRelativeTo(null); 
    this.setResizable(false); 
    this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE)*; 
    init(); 
    this.setIconImage(new ImageIcon("src/image/palm2.png" ).getImage()); 
     
    this.setVisible(true); 
     
    } 
     
    public void init(){ 
     
    JPanel panNom = new JPanel(); 
    panNom.setBackground(Color.white); 
    panNom.setPreferredSize(new Dimension(400, 60)); 
    nom = new JTextField(); 
    nom.setPreferredSize(new Dimension(220, 25)); 
    panNom.setBorder(BorderFactory.createTitledBorder("Nom d'activité")); 
    nomLabel = new JLabel("nom :"); 
    panNom.add(nomLabel); 
    panNom.add(nom); 
     
    JPanel pancapa = new JPanel(); 
    pancapa.setBackground(Color.white); 
    pancapa.setPreferredSize(new Dimension(300, 150)); 
    pancapa.setBorder(BorderFactory.createTitledBorder("capacité d'activité ")); 
    capaLabel = new JLabel("capacité : "); 
    capa = new JTextArea(); 
    capascroll = new JScrollPane(capa); 
    capascroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    capascroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    pancapa.setLayout(new GridLayout(1,1,5,5)); 
    pancapa.add(capaLabel); 
    pancapa.add(capascroll); 
     
     
     
    JPanel pansour = new JPanel(); 
    pansour.setBackground(Color.white); 
    pansour.setPreferredSize(new Dimension(300, 150)); 
    pansour.setBorder(BorderFactory.createTitledBorder("source d'activité")); 
    sourceLabel = new JLabel("source : "); 
    source = new JTextArea(); 
    sourcescroll = new JScrollPane(source); 
    sourcescroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    sourcescroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    pansour.setLayout(new GridLayout(1,1,5,5)); 
    pansour.add(sourceLabel); 
    pansour.add(sourcescroll); 
     
     
    JPanel paninfo = new JPanel(); 
    paninfo.setBackground(Color.white); 
    paninfo.setPreferredSize(new Dimension(300, 150)); 
    paninfo.setBorder(BorderFactory.createTitledBorder("autre information")); 
    infoLabel = new JLabel("information : "); 
    info = new JTextArea(); 
    infoscroll = new JScrollPane(info); 
    infoscroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    infoscroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    paninfo.setLayout(new GridLayout(1,1,5,5)); 
    paninfo.add(infoLabel); 
    paninfo.add(infoscroll); 
     
     
    JPanel content = new JPanel(); 
    content.setBackground(Color.white); 
    content.add(panNom); 
    content.add(pansour); 
    content.add(pancapa); 
    content.add(paninfo); 
     
     
     
     
    JPanel control = new JPanel(); 
    JButton okBouton = new JButton("OK"); 
     
    okBouton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent arg0) { 
    if (!nom.getText().equals("")){ 
    inf = "** Informations sovgarder avec succé ** \n" + 
    nomLabel.getText()+" \t"+ nom.getText()+" \n" + 
    capaLabel.getText()+ " \t" + capa.getText()+ " \n" + 
    sourceLabel.getText()+ " \t" + source.getText()+" \n" + 
    infoLabel.getText()+ " \t"+info.getText(); 
    setVisible(false); 
    msg.showMessageDialog(null,inf, "Activité Info", JOptionPane.INFORMATION_MESSAGE); 
    act=true; 
    } 
    else {inf ="aucun information saisi ---> voulez saisir le nom de l'activité"; 
    msg.showMessageDialog(null, inf, "Erreur", JOptionPane.ERROR_MESSAGE); 
    } 
    } 
    }); 
     
     
    JButton cancelBouton = new JButton("Annuler"); 
    cancelBouton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent arg0) { 
    inf= "aucun information saisi"; 
    setVisible(false); 
    msg.showMessageDialog(null, inf, "Erreur", JOptionPane.ERROR_MESSAGE); 
    } 
    }); 
     
    control.add(okBouton); 
    control.add(cancelBouton); 
     
     
    this.getContentPane().add(content, BorderLayout.CENTER); 
    this.getContentPane().add(control, BorderLayout.SOUTH); 
     
     
     
     
     
     
     
     
     
    } 
    public void setnom(String a){ nom.setText(a);}; 
    public void setcap(String a){ capa.setText(a);}; 
    public void setsour(String a){ source.setText(a);}; 
    public void setinfo(String a){ info.setText(a);}; 
     
     
    public String getnom(){ return nom.getText();} 
    public String getcap(){ return capa.getText();} 
    public String getsour(){ return source.getText();} 
    public String getinfo(){ return info.getText();} 
    public boolean getact(){ return act;} 
     
    public String toString(){return inf;} 
    }
    pour enregisté cette objet j'ai fait ca dans la classe main :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    ObjectOutputStream oos; 
    try { 
    oos = new ObjectOutputStream( 
    new BufferedOutputStream( 
    new FileOutputStream( 
    new File("info.txt")))); 
     
    oos.writeObject(fet); 
    oos.close(); 
    } catch (IOException ee) { 
    ee.printStackTrace(); 
    }} 
    };
    et la classe pour modifier :

    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
     
    import java.awt.event.ActionEvent; 
     
    import java.awt.event.ActionListener; 
    import java.io.BufferedInputStream; 
    import java.io.BufferedOutputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.ObjectInputStream; 
    import java.io.ObjectOutputStream; 
     
     
     
    import org.jgraph.JGraph; 
     
     
    public class modifier implements ActionListener { 
    JGraph g; 
    propo fet; 
    public modifier(JGraph graph) 
    { 
    this.g = graph; 
    } 
     
    public void actionPerformed(ActionEvent arg0) { 
    Object tt = g.getSelectionCell(); 
    String t=tt.toString(); 
    ObjectInputStream ois; 
    ObjectOutputStream oos; 
     
    try { 
    ois = new ObjectInputStream( 
    new BufferedInputStream( 
    new FileInputStream( 
    new File("info.txt")))); 
     
     
    try { 
     
    while(!((propo) ois.readObject()).getnom().equals(t)){ 
    fet = new propo(null,"information",true); 
     
    fet.setnom(((propo) ois.readObject()).getnom()); 
    fet.setcap(((propo) ois.readObject()).getcap()); 
    fet.setinfo(((propo) ois.readObject()).getinfo()); 
    fet.setsour(((propo) ois.readObject()).getsour()); 
     
    }; 
     
     
    } catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
    } 
     
    ois.close(); 
    if(fet.getact()){ 
     
     
    oos = new ObjectOutputStream( 
    new BufferedOutputStream( 
    new FileOutputStream( 
    new File("info.txt")))); 
     
    oos.writeObject(fet); 
     
    oos.close();} 
    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    }catch (IOException ee) { 
    ee.printStackTrace(); 
    } 
    } 
     
    }
    et voila l'erreur qui se pose quand je clic sur modifier:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at propo.getnom(propo.java:170)
    at modifier.actionPerformed(modifier.java:42)
    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)
    stp si quelq'un peut m'aidé n'ésite pas ...
    merci.

  2. #2
    Membre éclairé Avatar de Balbuzard
    Profil pro
    Inscrit en
    Août 2008
    Messages
    381
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 381
    Par défaut
    Apparement, tu n'as pas affecte de texte a nom. Donc ton textField est defini mais ne comportant pas de texte, un exception est declanchee lorsque tu lui appliques la methode getNom().
    Pour lire les erreurs, il suffit de prendre les premieres entrees dans la sortie console
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at propo.getnom(propo.java:170)
    at modifier.actionPerformed(modifier.java:42)
    Celles qui correspondent a ton projet et pas les autres.
    Enuite, NullPointerException indique toujours que tu appliques une methode a un objet qui n'a pas ete instancie avec un new.

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

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