Erreur java.lang.ArrayIndexOutOfBoundsException

Résolu
mayma - Modifié par BunoCS le 19/07/2016 à 11:30
BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 - 19 juil. 2016 à 12:59
Bonjour,
Voilà mon erreur :java.lang.ArrayIndexOutOfBoundsException: 4, j'arrive pas à le résoudre. Merci bien pour votre aide. Voici mon code:

public class CoordinateMatrix {

 private static GeometryFactory geomFact = new GeometryFactory();

 private Map<Integer, Node> multiMap = new HashMap<>();


 public CoordinateMatrix(File f) throws IOException {
  fillMatrix(f);
 }

 public void fillMatrix(File f) throws IOException {

  // We create a BufferReader
  InputStream ips = new FileInputStream(f);
  InputStreamReader ipsr = new InputStreamReader(ips);
  BufferedReader br = new BufferedReader(ipsr);

  String Line = br.readLine();

  // On récupère le tableau des identifiants

  int id = 0;
  int indexX = 2;
  int indexY = 3;
  int cat= 4;// catégorie :point de collecte ou dépot de stockage.
  // On parcourt chaque ligne
  while ((Line = br.readLine()) != null) {

   // On parse la ligne courante
   String[] tabLineTemp = Line.split(";");

   System.out.println(Arrays.toString(tabLineTemp));
   
  
   //On fait un test => si c'est 0 alors on crée un Point de Collecte
   //Si c'est un on crée un centre de stockage
   //On ajoute à la hasmap
   
if(Integer.parseInt(tabLineTemp[cat])==0){
 
 DepotDeStockage d= new DepotDeStockage (Integer.parseInt(tabLineTemp[id]), indexX, indexY, 0);
 
 
}


  else if(Integer.parseInt(tabLineTemp[cat])==1){
  PointDeCollecte p= new  PointDeCollecte  (Integer.parseInt(tabLineTemp[id]), indexX, indexY, 1);
 
   
  }


  
   
 Node n = new Node(Integer.parseInt(tabLineTemp[id]),
     

    Double.parseDouble(tabLineTemp[indexX])

   
   , Double.parseDouble(tabLineTemp[indexY])
   ,  Double.parseDouble(tabLineTemp[cat]))                              ;

  multiMap.put(Integer.parseInt(tabLineTemp[id]), n);

  
  }
   


  br.close();
 }

 public Set<Integer> getListId() {
  return multiMap.keySet();
 }



 public com.vividsolutions.jts.geom.Point getPoint(int id) {

   
  Node n = multiMap.get(id);
  
  if(n == null){
   return null;
  }
  

  return geomFact.createPoint(new Coordinate(n.getX(), n.getY()));

 }
 
 public Collection<Node> getListNodes(){
  return multiMap.values();
 }

 public static void main(String[] args) throws IOException {

  File f = new File(CoordinateMatrix.class.getResource("/data/idf/com_idf_pt_xy.csv").getPath());
  CoordinateMatrix cM = new CoordinateMatrix(f);

  
    Set<Integer> Lid = cM.getListId();
    System.out.println("---------------------------------"); for (int i =
    0; i < Lid.size(); i++) {
    
    System.out.print(Lid.toArray()[i] + ";");
    
    } System.out.println("");
    System.out.println("---------------------------------");
    
    com.vividsolutions.jts.geom.Point p = cM.getPoint(77283);
    
    System.out.println("Les coordnnées du point qui a l'id 77283 sont : " + p);
   

 }

}


EDIT : Ajout des balises de code (la coloration syntaxique).
Explications disponibles ICI

Merci d'y penser dans tes prochains messages.

4 réponses

BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 103
19 juil. 2016 à 11:30
Hello,

ArrayOutOfBoundsException te dit que tu essaies d'accéder à une case de ton tableau qui n'existe pas...

Au niveau de tes ID, tu les déclares comme suit:
int id = 0;
int indexX = 2;
int indexY = 3;
int cat= 4;// catégorie :point de collecte ou dépot de stockage.

Et tu les utilises comme index de tableau. Or:
- il n'y a pas de valeur "1"
- un index de tableau va de "0" à "taille-1"

Note: j'ai édité ton message. Voir encadré.
0
Rejoignez-nous