我如何修复java.lang.ArrayIndexOutOfBoundsException:索引1超出长度1错误? [重复]

问题描述 投票:0回答:1
因此,我已经阅读了文档和大量教程,但是仍然没有想到如何修复“

线程” main“中的异常java.lang.ArrayIndexOutOfBoundsException:索引1超出长度1 “代码中的错误,因为我一生无法看到任何错误。

public HashMap<String, product> getAllProducts() { HashMap<String, product> productMap = new HashMap<String, product>(); //create a file object from the external file File productFile = new File("src/main/resources/stock.sample.csv"); // Catch Exception when reading the file using try-catch block try { // Read an external file: stock.sample.csv via Scanner Scanner scanner = new Scanner(productFile); while (scanner.hasNext()){ String fileRecord = scanner.nextLine(); String[] fileRecordSplit = fileRecord.split(","); product product = new product(); product.setId(fileRecordSplit[0]); product.setName(fileRecordSplit[1]); // Throws the java.lang.ArrayIndexOutOfBoundsException: // Index 1 out of bounds for length 1 product.setCategory(fileRecordSplit[2]); Price price = new Price(); price.setAmount(Double.parseDouble(fileRecordSplit[3])); product.setPrice(price); productMap.put(product.getId(), product); } } catch (FileNotFoundException e){ // TODO Auto-generated catch block e.printStackTrace(); } return productMap; } }
java indexoutofboundsexception
1个回答
0
投票
second
© www.soinside.com 2019 - 2024. All rights reserved.