Note that there are some explanatory texts on larger screens.

plurals
  1. POI am getting a Array Index out of bounds yet I can't find the what is causing it?
    text
    copied!<p>In this class I am just working with a binary file but every time I compile I get an array index out of bounds all I needed to do was enhance this code so instead of it reading and writing from a text file it does it using binary, my array list seems to be fine to me? any help would be appreciated</p> <pre><code>import java.util.*; import java.io.*; import java.nio.file.*; public final class ProductBinaryFile implements ProductDAO { private ArrayList&lt;Product&gt; products = null; private Path productsPath = null; private File productsFile = null; private final String FIELD_SEP = "\t"; public ProductBinaryFile() { productsPath = Paths.get("products.bin"); productsFile = productsPath.toFile(); **products = this.getProducts();** } public ArrayList&lt;Product&gt; getProducts() { // if the products file has already been read, don't read it again if (products != null) return products; products = new ArrayList&lt;&gt;(); if (Files.exists(productsPath)) // prevent the FileNotFoundException { try (DataInputStream in = new DataInputStream( new BufferedInputStream( new FileInputStream(productsFile)))) { // read all products stored in the file // into the array list String line = in.readUTF(); while(line != null) { String[] columns = line.split(FIELD_SEP); String code = columns[0]; **String description = columns[1];** String price = columns[2]; Product p = new Product( code, description, Double.parseDouble(price)); products.add(p); line = in.readUTF(); } } catch(IOException e) { System.out.println(e); return null; } } return products; } public Product getProduct(String code) { for (Product p : products) { if (p.getCode().equals(code)) return p; } return null; } public boolean addProduct(Product p) { products.add(p); return this.saveProducts(); } public boolean deleteProduct(Product p) { products.remove(p); return this.saveProducts(); } public boolean updateProduct(Product newProduct) { // get the old product and remove it Product oldProduct = this.getProduct(newProduct.getCode()); int i = products.indexOf(oldProduct); products.remove(i); // add the updated product products.add(i, newProduct); return this.saveProducts(); } private boolean saveProducts() { try (DataOutputStream out = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(productsFile)))) { // write all products in the array list // to the file for (Product p : products) { out.writeUTF(p.getCode() + FIELD_SEP); out.writeUTF(p.getDescription() + FIELD_SEP); out.writeDouble(p.getPrice()); } } catch(IOException e) { System.out.println(e); return false; } return true; } } </code></pre> <p>my text file am trying to read from: <img src="https://i.stack.imgur.com/GJpd8.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload