如何修复 java.lang.ArrayIndexOutOfBoundsException 错误:索引 1 超出长度 1 的界限?

huangapple 未分类评论58阅读模式
标题翻译

How do I fix java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 error?

问题

我已经阅读了文档以及关于它的一系列教程,但是我仍然不能理解如何修复我的代码中的“Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1”错误,因为我无论如何都看不出有什么问题。

 public HashMap<String, product> getAllProducts() {
            HashMap<String, product> productMap = new HashMap<String, product>();
    
            //从外部文件创建文件对象
            File productFile = new File("src/main/resources/stock.sample.csv");
    
            // 使用 try-catch 块在读取文件时捕获异常
            try {
                // 通过 Scanner 读取外部文件:stock.sample.csv
                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]); // 抛出 java.lang.ArrayIndexOutOfBoundsException:
                    // Index 1 超出长度 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: 处理异常
                e.printStackTrace();
            }
            return productMap;
        }
    }
英文翻译

So I already read the documentation and a bunch of tutorials on it but it's still not clicking in my head how to fix the "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1" error in my code since I can't for the life of me see anything wrong with it.

 public  HashMap&lt;String, product&gt; getAllProducts() {
            HashMap&lt;String, product&gt; productMap = new HashMap&lt;String, product&gt;();
    
            //create a file object from the external file
            File productFile = new File(&quot;src/main/resources/stock.sample.csv&quot;);
    
            // 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(&quot;,&quot;);
    
                    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;
        }
    }

huangapple
  • 本文由 发表于 2020年3月4日 03:46:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/60514454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定