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

Composants Java Discussion :

Problème de JTable


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Par défaut Problème de JTable
    Bonjour,
    je ne sais pas pourquoi , mais ma JFRame qui contient un panel dans lequel il y a une JTable ne fonctionne plus. Voici le code et l'erreur :

    Sous Eclipse :

    L'erreur :
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Carte.Serveurs$CompTableCellRenderer.getTableCellRendererComponent(Serveurs.java:212)
    at Carte.Serveurs$CompTableCellRenderer.getTableCellRendererComponent(Serveurs.java:1)
    Le code
    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
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    package Carte;
    import java.awt.GridLayout;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Vector;
     
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.TableCellRenderer;
     
    import Utilitaires.DataBase;
     
    public class Serveurs extends JPanel implements Runnable{
     
    	private static final long serialVersionUID = 1L;
    	String NomServeur;
    	Vector <String> LettreDisque;
    	Vector <Double> tailleMax;
    	Vector <Boolean> unite;
    	int pourcentage;
    	int x = 0;
    	int y = 20;
            JScrollPane pan;
            JTable tabler;
    	DataBase base;
            ResultSet resultat;
            CompTableCellRenderer composant = new CompTableCellRenderer();
            Thread tache;
            JPanel serv = this;
            boolean test = true;
            double taille;
           Modele dat;
     
            public void run(){
                while(test){
                    try{
                        this.lancer();
                        this.updateUI();
                        Thread.sleep(60000);
                    }
                    catch(Exception ecx){
                    //System.out.println("Erreur de sleep de SErveur"+ecx.fillInStackTrace())
                    	;}
                }
            }
     
     
            public void stopper(){
                test = false;
            }   
     
            public void lancer(){
                try{
                	String req="select id from appareils where nom = '"+NomServeur+"'";
                	//System.out.println(req);
    		base = new DataBase(req);
    		base.readBDD();
    		resultat = base.retourner();
    		if(resultat.next() && resultat.next()){
    			String id = resultat.getString("id");
                String[] nom = {"Disque","Espace Libre","Espace Libre","Etat"}; 
     
                LettreDisque = new Vector<String>();
                tailleMax = new Vector<Double>();
                unite = new Vector<Boolean>();
     
                String req1="select lettre_disque,taille from el_disques where superviser = 1 and nom = '"+NomServeur+"' order by lettre_disque";
                //System.out.println(req1);
                base.request=req1;
                base.readBDD();
                resultat = base.retourner();
                while(resultat.next()){
                    LettreDisque.addElement(resultat.getString("lettre_disque"));
                    tailleMax.addElement(resultat.getDouble("taille"));
                }
                base.fermer();
     
                Object[][] data = new Object[LettreDisque.size()][nom.length];
                dat = new Modele(data,nom);
     
     
     
                JTable table = new JTable();
                table.setRowHeight(25);
                table.setShowGrid(false);
                table.setModel(dat);
     
    //            new ModeleListener(table);
                pan = new JScrollPane(table);
                for(int i = 0;i < LettreDisque.size();i++){
                	String req2="select probleme,id from erreurs where nom = '"+NomServeur+"' and probleme = '"+LettreDisque.get(i)+"' and critique = 1 and id = '"+id+"'";
                	//System.out.println(req2);
                        base = new DataBase(req2);
                        base.readBDD();
                        resultat = base.retourner();
                        if(resultat.next()){
                            table.setValueAt(new ImageIcon("images/iconefalse.GIF"),i,3);
                        }
                        else{
                            table.setValueAt(new ImageIcon("images/iconeOK.GIF"),i,3);
                        }
                        base.fermer();
                        taille = Double.parseDouble(tailleMax.get(i).toString());
                        JProgressBar bar = new JProgressBar(0,(int)taille);
                        //System.out.println("taille barre"+taille);
                        String baf="select disques.utiliser from appareils,disques where appareils.id = disques.id and appareils.id = (select max(id) from appareils where nom = '"+NomServeur+"') and description = '"+LettreDisque.get(i).toString()+"' and disques.nom = '"+NomServeur+"' and appareils.id > '"+id+"' order by description";
                        //System.out.println(baf);
                        base = new DataBase(baf);
                        base.readBDD();
                        resultat = base.retourner();
                        if(resultat.next()){
                            double utiliser = resultat.getDouble("utiliser");
                           //System.out.println(utiliser+","+taille);
                            bar.setValue((int)(taille - utiliser));
                            bar.setString(Integer.toString(100*((int)(taille - utiliser))/(int)taille)+ " % ");
                            bar.setStringPainted(true);
                            table.setValueAt(" "+LettreDisque.get(i).toString(),i,0);
                            table.getColumnModel().getColumn(0).setCellRenderer(composant);
                            table.setValueAt(bar,i,1);
                            table.getColumnModel().getColumn(1).setCellRenderer(composant);
                            table.getColumnModel().getColumn(2).setCellRenderer(composant);
                            if(taille-utiliser < 1){
                                table.setValueAt(" " + Double.toString((taille-utiliser)*1024).substring(0,5)+" Mo/"+String.valueOf(taille*1024).substring(0, 5)+" Mo",i,2);
                                unite.addElement(false);
                            }
                            else{
                                table.setValueAt(" " + Double.toString((taille-utiliser)).substring(0,5)+" Go/"+String.valueOf(taille).substring(0, 5)+" Go",i,2);
                                unite.addElement(true);
                            }
     
    //                        for(int h=0;h<unite.size();h++)
    //                        	//System.out.println(unite.elementAt(h));
                        }
                        base.fermer(); 
                    }
    		tabler = table;
                }
    		}
                catch(SQLException sqle){
                	//System.out.println("Erreur SQL de la methode lancer");
                sqle.printStackTrace();}
            }
     
     
     
          public Serveurs(String Nom){
                this.NomServeur = Nom;
                this.lancer();
                add(pan);
                setLayout(new GridLayout(1,1));
     
     
    tabler.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    ListSelectionModel rowSM = tabler.getSelectionModel();
     
    rowSM.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            //Ignore extra messages.
            if (e.getValueIsAdjusting()) return;
     
            ListSelectionModel lsm =
                (ListSelectionModel)e.getSource();
            if (lsm.isSelectionEmpty()) {
     
            } else {
                int selectedRow = lsm.getMinSelectionIndex();
                System.out.println( selectedRow);
     
                try{                      
     
                    String lab=null;                    
                    //on créer une nouvelle fenetre pour le disques
     
     
     
               //     boolean selection= tabler.getSelectionModel().isSelectedIndex(selectedRow);
     
                    	//JProgressBar barre = (JProgressBar)tabler.getValueAt(selectedRow,1);
                           lab = tabler.getValueAt(selectedRow,0).toString();
                           lab = lab.substring(1,3);                          
                           new DessinEspaceLibre(NomServeur,lab,tailleMax.elementAt(selectedRow),selectedRow,unite.elementAt(selectedRow));   
                    		}
                    catch(Exception exsc){
                    	//System.out.println("Erreur dans nouveau DessinEspaceLibre");
                    	exsc.printStackTrace();
                    	}
            } 
     
        }});            		    	
            }
     
    class CompTableCellRenderer extends JPanel implements TableCellRenderer{ 
        	  private static final long serialVersionUID = 1L;
                private JProgressBar barre;
    	    public CompTableCellRenderer(){}  
     
     
    	    public JComponent getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column){
                    if(column == 1){
                        barre = (JProgressBar)value;
                        return barre;
                    }
                    else{
                        return new JLabel(value.toString());
                    } 
                }
          }
    }

    Le code de mon Modele :
    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
    package Carte;
     
    import javax.swing.JTable;
    import javax.swing.event.TableModelEvent;
    import javax.swing.event.TableModelListener;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableModel;
     
        class ModeleListener implements TableModelListener {
        	private static final long serialVersionUID = 1L;
        public ModeleListener(JTable table){
            table.getModel().addTableModelListener(this);
        }
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            TableModel model = (TableModel)e.getSource();
            String columnName = model.getColumnName(column);
            Object data = model.getValueAt(row, column);
        }
    }
    public class Modele extends AbstractTableModel {
    	private static final long serialVersionUID = 1L;
            private String[] nom;
            private Object[][] data;
            public Modele(Object[][] data,String[] nom){
                this.data = data;
                this.nom = nom;
            }
     
            public int getColumnCount() {
                return nom.length;
            }
     
            public int getRowCount() {
                return data.length;
            }
     
            public String getColumnName(int col) {
                return nom[col];
            }
     
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
            public Class <?> getColumnClass(int c) {
     
                return getValueAt(0, c).getClass();
            }
            public boolean isCellEditable(int row, int col) {
                return false;
            }
            public void setValueAt(Object value, int row, int col) {
                data[row][col] = value;
                fireTableCellUpdated(row, col);
            }
        }
    Avec mes encouragements, la je ne vois pas pourquoi ca plante

  2. #2
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Salut,


    Tu as tout simplement une valeur null que tu ne gère pas dans ton Renderer...

    a++

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Par défaut
    column est un int , je ne peux pas traiter le cas egale null,
    ou j'ai rien compris ?

  4. #4
    Expert éminent
    Avatar de adiGuba
    Homme Profil pro
    Développeur Java/Web
    Inscrit en
    Avril 2002
    Messages
    13 938
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java/Web
    Secteur : Transports

    Informations forums :
    Inscription : Avril 2002
    Messages : 13 938
    Billets dans le blog
    1
    Par défaut
    Je parle de value... Es-tu allé voir à la ligne indiqué dans l'exception ???

    A ton avis, que ce passe-t-il dans ce code lorsque le paramètre value est null :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
        public JComponent getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column){
            if(column == 1){
                barre = (JProgressBar)value;
                return barre;
            }
            else{
                return new JLabel(value.toString());
            } 
        }
    a++

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Par défaut
    Alors , si value est null , il ne sait pas faire le return , ok je vais essayer de la traiter

  6. #6
    Membre éclairé
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    318
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2005
    Messages : 318
    Par défaut
    J'ai mis ca
    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
    public JComponent getTableCellRendererComponent(JTable 
    table, Object value, boolean isSelected, boolean 
    hasFocus,int row, int column){
                if(value==null) return new JLabel("Erreur");
                else{
     
    	    	if(column == 1){
                        barre = (JProgressBar)value;
                        return barre;
                    }
                    else
                        return new JLabel(value.toString());
                    } 
     
                }
    et ca m'affiche maintenant le tableau avec les cellules qu'on "merder".
    Merci , ca m'avance beaucoup !

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

Discussions similaires

  1. Problème taille JTable
    Par L4BiN dans le forum Composants
    Réponses: 6
    Dernier message: 11/02/2011, 18h35
  2. Problème TableSorter Jtable
    Par leneuf dans le forum Composants
    Réponses: 2
    Dernier message: 16/11/2006, 17h54
  3. Problème sur JTable
    Par arsenik7 dans le forum Composants
    Réponses: 4
    Dernier message: 03/07/2006, 16h42
  4. Problème avec Jtable
    Par @yoyo dans le forum Composants
    Réponses: 4
    Dernier message: 22/03/2006, 15h51
  5. problème avec Jtable
    Par magic001 dans le forum Composants
    Réponses: 6
    Dernier message: 15/03/2006, 23h49

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