Appelr une methode static d'une autre classe

sarha1 Messages postés 11 Date d'inscription lundi 5 mars 2012 Statut Membre Dernière intervention 23 mai 2012 - 20 mars 2012 à 01:10
sarha1 Messages postés 11 Date d'inscription lundi 5 mars 2012 Statut Membre Dernière intervention 23 mai 2012 - 21 mars 2012 à 17:16
bonsoir, je me bloque a cett étape, en faite j'ai deux classe dans la premiére que je l'ai nommé "liste_patient", ici j'ai définit une méthode "affichage() " public qui retourne un String
jusqu’à ici pas de problème . je veux appeler cette méthode à partir d'une deuxième classe nommé commande
donc donc la classe commande j'ai :
liste_patient f= new liste_patient();

f.affichage();


voila comment j'ai définit la méthode
public String affichage()
{
..........
return String ;
}




mais lorsque j'ai compile:
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)



merci d'avance

6 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
20 mars 2012 à 08:21
Bonjour,

Si ta méthode des static, tu ne peux pas l'appeler sur un objet : f.affichage() mais il faut l'appeler en donnant le nom de la classe dans laquelle elle se trouve : liste_patient.affichage()
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
20 mars 2012 à 08:27
Et essaye de respecter les conventions de nommage en jave, une classe commence par une majuscule et les mots sont séparés par des majuscules : ClasseExemple
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
20 mars 2012 à 08:28
Tu n'utilises pas d'ide : un ide n'aurait pas laissé passer une telle erreur. Je te conseil donc de télécharger Eclipse, tu vas gagner un temps infini !
0
sarha1 Messages postés 11 Date d'inscription lundi 5 mars 2012 Statut Membre Dernière intervention 23 mai 2012
20 mars 2012 à 20:33
bonsoir Julien39,
la méthode affichage() n'est pas une méthode static normalement, je l'ai définit avec "public" mais j'ai commis une erreur en tapant le nom de sujet.
j'ai essayé avec liste_patient.affichage() mais il n'accepte pas, elle est souligné en rouge !!!!! donc quoi je faire???
oui j’utilise déjà l’Éclipse Indigno
merci comme même pour votre réponse
a++
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
21 mars 2012 à 08:12
Et quelle erreur est indiquée quand tu passes le curseur au dessus du carré rouge ?

Peux tu me donner un code un peu plus complet : celui de l'appel de la méthode affichage. Si cette méthode retourne un String, il faudrait en faire quelque chose non ? Comme un System.out.println() ?
0
sarha1 Messages postés 11 Date d'inscription lundi 5 mars 2012 Statut Membre Dernière intervention 23 mai 2012
21 mars 2012 à 17:16
**bonjour,enfin j'ai arrivé a envoyer un texte dans un textfield appartenant à une deuxième classe
mais pour mon projet je dois récupérer le texte selectionné dans un tableau dans le textfield de la deuxième classe, il n’y pas des erreur mais rien ne s'affiche, voila la methode :

public String afficher()
{
int r = tableau.getSelectedRow();
Object m=tableau.getModel().getValueAt(r,tableau.getSelectedColumn());



String converte = m.toString();
return converte;

}


*****dans la deuxième classe commande :

liste_patient d=new liste_patient();
textField.setText(d.afficher());


******voila le résultat du compilation



Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at liste_patient.afficher(liste_patient.java:411)
at commande.(commande.java:58)
at liste_patient$7.actionPerformed(liste_patient.java:291)
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)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at liste_patient.afficher(liste_patient.java:411)
at commande.(commande.java:58)
at liste_patient$7.actionPerformed(liste_patient.java:291)
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)
0
Rejoignez-nous