Erreur "non-static variable this cannot be referenced from a static context"
Bonjour à tous,
Le but de mon code est de récupérer les données (id, nom, adresse, tel, longitude, latitude) de défibrilateurs autour d'un individu dont on connait sa longitude et latitude. L'idée est ensuite de comparer les distances entre chacun des defibrilateurs et l'individu pour finalement donner en sortie le nom du défibrilateur le plus proche.
Néanmoins, je me heurte à l'erreur suivante que je n'arrive pas à solutionner :
Erreur de compilation
error: non-static variable this cannot be referenced from a static context
defibrilateurs.put(id, new Defibrilateur(id,nom,adresse,tel,dLonRad,dLatRad));
Merci d'avance pour votre aide ! Je me remets au Java et pour le coup élucider la source de mon erreur me dépasse encore un peu.
Voici mon code complet :
Code:
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
| import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
public class Defibrilateur
{
int id;
String nom, adresse, tel;
Double dLon, dLat;
public Defibrilateur (int id, String nom, String adresse, String tel, double dLon, double dLat)
{
this.id = id;
this.nom = nom;
this.adresse = adresse;
this.tel = tel;
this.dLon = dLon;
this.dLat = dLat;
}
public double distIndividu2Defib(double lon_indiv, double lat_indiv)
{
// Calcul du x
double x = (this.dLon - lon_indiv)*Math.cos((this.dLat+lat_indiv)/2);
// Calcul du y
double y = this.dLat - lat_indiv;
// Calcul de d
double d = Math.sqrt(x*x + y*y);
return d;
}
}
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner in = new Scanner (System.in);
// Récupération de la position (en degré) de l'individu et conversion en radians
String lonIndivDeg = in.next();
Double lonIndivRad = deg2Rad(Double.parseDouble(lonIndivDeg.replace(',', '.')));
in.nextLine();
String latIndivDeg = in.next();
Double latIndivRad = deg2Rad(Double.parseDouble(latIndivDeg.replace(',', '.')));
in.nextLine();
// Récupération du nombre de défibrilateur
int N = in.nextInt();
in.nextLine();
// Création du HashMap
HashMap<Integer,Defibrilateur> defibrilateurs = new HashMap<>();
// Itération sur les N défibrilateurs et Stockage des informations dans un HashMap
for (int i=0;i<N;i++)
{
String[] defibTab = in.nextLine().split(";");
int id = Integer.parseInt(defibTab[0]);
String nom = defibTab[1];
String adresse = defibTab[2];
String tel = defibTab[3];
Double dLonRad = deg2Rad(Double.parseDouble(defibTab[4]));
Double dLatRad = deg2Rad(Double.parseDouble(defibTab[5]));
defibrilateurs.put(id, new Defibrilateur(id,nom,adresse,tel,dLonRad,dLatRad));
}
double min = -1;
int idMin = 0;
for(Defibrilateur def : defibrilateurs.values())
{
double d = def.distIndividu2Defib(lonIndivRad, latIndivRad);
if (min = -1 || d<min)
{
min = d;
idMin = def.id;
}
}
System.out.println(defibrilateurs.get(idMin).nom);
}
public static double deg2Rad(double angle)
{
return angle*Math.PI/180;
}
} |