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

Langage Java Discussion :

Erreur : cannot be referenced from a static context


Sujet :

Langage Java

  1. #1
    Membre actif Avatar de Belegkarnil
    Inscrit en
    Juin 2005
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 36

    Informations forums :
    Inscription : Juin 2005
    Messages : 289
    Points : 205
    Points
    205
    Par défaut Erreur : cannot be referenced from a static context
    Bonjour, pouvez-vous m'aider? Je n'arrive pas a compiler mon code a cause de cette erreur :
    TCPIP2.java:14: non-static method socket() cannot be referenced from a static context
    serverSocket = ServerSocketChannel.socket();
    ^
    1 error

    Processus terminé avec code quitter 1
    Et voici tout mon 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
    import java.net.*;
    import java.io.*;
    import java.security.*;
    import java.nio.channels.*;
    class Temp{
    	private ServerSocketChannel serverSocketChannel = null;
    	private ServerSocket serverSocket = null;
    	private Socket clientSocket = null;
    	private boolean fin = false;
    	Temp(){
    		try{
    			SocketAddress address = new InetSocketAddress(9999);
    			serverSocketChannel = ServerSocketChannel.open();
    			serverSocket = ServerSocketChannel.socket();
    			serverSocket.bind(address);
     
    			while(!fin){
    				try{
    					clientSocket = serverSocket.accept();
    					//Traitement sur le clients Socket
    				} catch(ClosedByInterruptException IException){
    					fin = true;
    				}
    			}
    		} catch(IOException IException){
    			//TODO Auto-generated catch block
    			IException.printStackTrace();
    		} finally {
    			try{
    				if(serverSocket != null) serverSocket.close();
    			} catch(Exception IException){
    				//ERREUR
    			}
    			try{
    				if(serverSocketChannel!=null) serverSocketChannel.close();
    			} catch(Exception IException){
    				//////ERREUR
    			}
    		}
    	}
    	public void setStatut(boolean setter){
    		fin = setter;
    	}
    }
    public class TCPIP2{
    	public static void main(String arg[]){
    	}
    }
    class Fonction{
    	static public String MD5(String password){
        	try{
    			MessageDigest md = MessageDigest.getInstance("MD5");
        		md.update(password.getBytes());
        		byte[] md5 = md.digest();
        		return new String(md5);
    		}catch(Exception e){
    			return "";
    		}
      	}
    }
    Merci de votre aide :o

  2. #2
    Expert éminent sénior
    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
    Points : 23 190
    Points
    23 190
    Billets dans le blog
    1
    Par défaut Re: Erreur : cannot be referenced from a static context
    Salut,


    Tout est marqué dans le message d'erreur : la méthode socket() n'est pas une méthode statique, tu dois utiliser un nom de variable et non pas le nom de la classe :


    serverSocket = serverSocketChannel.socket();
    Attention aux majuscules/minuscules

    a++

  3. #3
    Membre actif Avatar de Belegkarnil
    Inscrit en
    Juin 2005
    Messages
    289
    Détails du profil
    Informations personnelles :
    Âge : 36

    Informations forums :
    Inscription : Juin 2005
    Messages : 289
    Points : 205
    Points
    205
    Par défaut
    a ok merci beaucoup. J'ai recopier une partie de ce code pour faire uns erveur bien que je ne sache pas ce qu'est un serverSocket, socket, ...

    Merci encore

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

Discussions similaires

  1. Non-static variable cannot be referenced from a static context
    Par otylio dans le forum Débuter avec Java
    Réponses: 4
    Dernier message: 23/11/2018, 15h42
  2. Réponses: 4
    Dernier message: 12/02/2015, 10h27
  3. non-static method cannot be referenced from a static context
    Par Spawny123 dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 04/03/2010, 21h38
  4. [FOP] [ERROR] non-static variable this cannot be referenced from a static context
    Par cash3000 dans le forum API standards et tierces
    Réponses: 8
    Dernier message: 05/05/2006, 16h46
  5. Réponses: 3
    Dernier message: 05/12/2005, 10h27

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