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 :

Erreur : non-static variable this cannot be referenced from a static context


Sujet :

avec Java

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2016
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2016
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Erreur : non-static variable this cannot be referenced from a static context
    Bonjour pour un exercice j'ai du crée un jeu assez simple et y ajouter un temps limité avec Timer et TimerTask cependant je tombe sur cette erreur " non-static variable this cannot be referenced from a static context " et je ne sais pas comment la résoudre.
    Quelqu'un pourrait t-il m'aider à corriger ce problème ? Merci d'avance !
    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
               import java.util.Scanner;
                    import java.util.Random;
                    import java.util.Timer;
                    import java.util.TimerTask;
                    public class NewMain {
     
                        /**
                         * @param args the command line arguments
                         */
                        public  void main(String[] args) {
                    Timer temps = new Timer("Printer");
                    Mytask t = new Mytask();
                    temps.schedule(t,0,2000);
     
                    int a = 0;        
     
     
                    final String oui = "oui";
                    final String non = "non";
                    System.out.print("Bienvenue dans Pick'n'lock\n");
                    System.out.print("Êtes vous prêt ? ");
                    System.out.print("oui/non ?\n");
                    Scanner daniet = new Scanner(System.in);
                    String c = daniet.nextLine();
     
     
     
     
     
     
     
                    switch(c){
                        case oui :
                        System.out.print("Hello World\n");
                        Random rand = new Random();
                        int combi1 = rand.nextInt(100+1);
                        int combi2 = rand.nextInt(100+1);
                        int combi3 = rand.nextInt(100+1);
                        System.out.println(""+combi1);
                        System.out.println(""+combi2);
                        System.out.println(""+combi3);
                        int ok1=0;
                        int ok2=0;
                        int ok3=0;
     
                        Scanner trouve = new Scanner(System.in);
                        do{
                            System.out.print("Entrez premier nombre\n");
                        int trouve1 = trouve.nextInt();
     
                        if (trouve1 < combi1){
                        System.out.print("C'est plus grand\n");
                        }else if (trouve1 > combi1) {
                        System.out.print("C'est plus petit\n");
                        } else {
                        System.out.print("Nombre 1 trouvé !\n");
                        ok1++;
     
                        }
                        }while(ok1==0);
                        do{
                        System.out.print("Entrez deuxième nombre\n");
                            int trouve2 = trouve.nextInt();
                             if (trouve2 < combi2){
                        System.out.print("C'est plus grand\n");
                        }else if (trouve2 > combi2) {
                        System.out.print("C'est plus petit\n");
                        } else {
                        System.out.print("Nombre 2 trouvé !\n");
                        ok2++;
                        }
                        }while (ok2==0);
     
                        do{
                        System.out.print("Entrez troisième nombre\n");
                            int trouve3 = trouve.nextInt();
                             if (trouve3 < combi3){
                        System.out.print("C'est plus grand\n");
                        }else if (trouve3 > combi3) {
                        System.out.print("C'est plus petit\n");
                        } else {
                        System.out.print("Nombre 3 trouvé !\n");
                        ok3++;
                        }
                        }while (ok3==0);
                       System.out.print("Prenez les documents secrets et filez vite la police arrive !");
     
     
     
     
                        break;
                        case non:
                        System.out.print("Good Bye World");
                        break;
     
     
                    }
                        }
                       class Mytask extends TimerTask {
                           private  int times = 0;
                           @Override
                           public void run(){
                               times++;
                               if (times <= 5){
                                   System.out.print("La police arrive");
                               }else{
                                   System.out.print("Trop tard !");
                                   this.cancel();
                               }
                           }
     
                       }
                    }
      0  0

  2. #2
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 310
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 310
    Points : 9 522
    Points
    9 522
    Billets dans le blog
    1
    Par défaut
    Ce serait sympa de mettre un code lisible, ça aiderait bien déjà...
    De mon point de vue, tu as oublié le "static" dans la signature de la méthode 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
    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
    import java.util.Scanner;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class NewMain {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            Timer temps = new Timer("Printer");
            Mytask t = new Mytask();
            temps.schedule(t,0,2000);
    
            int a = 0;        
    
            final String oui = "oui";
            final String non = "non";
            System.out.print("Bienvenue dans Pick'n'lock\n");
            System.out.print("Êtes vous prêt ? ");
            System.out.print("oui/non ?\n");
            Scanner daniet = new Scanner(System.in);
            String c = daniet.nextLine();
    
            switch(c)
            {
            case oui :
                System.out.print("Hello World\n");
                Random rand = new Random();
                int combi1 = rand.nextInt(100+1);
                int combi2 = rand.nextInt(100+1);
                int combi3 = rand.nextInt(100+1);
                System.out.println(""+combi1);
                System.out.println(""+combi2);
                System.out.println(""+combi3);
                int ok1=0;
                int ok2=0;
                int ok3=0;
    
                Scanner trouve = new Scanner(System.in);
                do
                {
                    System.out.print("Entrez premier nombre\n");
                    int trouve1 = trouve.nextInt();
    
                    if (trouve1 < combi1)
                    {
                        System.out.print("C'est plus grand\n");
                    }
                    else if (trouve1 > combi1) 
                    {
                        System.out.print("C'est plus petit\n");
                    } 
                    else 
                    {
                        System.out.print("Nombre 1 trouvé !\n");
                        ok1++;
                    }
                } while(ok1==0);
                
                do
                {
                    System.out.print("Entrez deuxième nombre\n");
                    int trouve2 = trouve.nextInt();
                    if (trouve2 < combi2)
                    {
                        System.out.print("C'est plus grand\n");
                    }
                    else if (trouve2 > combi2) 
                    {
                        System.out.print("C'est plus petit\n");
                    } 
                    else 
                    {
                        System.out.print("Nombre 2 trouvé !\n");
                        ok2++;
                    }
                } while (ok2==0);
    
                do
                {
                    System.out.print("Entrez troisième nombre\n");
                    int trouve3 = trouve.nextInt();
                    if (trouve3 < combi3)
                    {
                        System.out.print("C'est plus grand\n");
                    }
                    else if (trouve3 > combi3) 
                    {
                        System.out.print("C'est plus petit\n");
                    } 
                    else 
                    {
                        System.out.print("Nombre 3 trouvé !\n");
                        ok3++;
                    }
                } while (ok3==0);
                
                System.out.print("Prenez les documents secrets et filez vite la police arrive !");
                break;
                
            case non:
                System.out.print("Good Bye World");
                break;
            }
        }
    
        class Mytask extends TimerTask 
        {
            private  int times = 0;
            @Override
            public void run()
            {
                times++;
                if (times <= 5)
                {
                    System.out.print("La police arrive");
                }
                else
                {
                   System.out.print("Trop tard !");
                   this.cancel();
                }
            }
        }
    }
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
      0  0

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: 39
    Dernier message: 04/08/2015, 22h09
  3. Réponses: 3
    Dernier message: 23/07/2015, 14h59
  4. Réponses: 4
    Dernier message: 12/02/2015, 10h27
  5. [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

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