Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In below code, looks like your <code>columns</code> array length is 1. So, it can not access [1]. It can only access [0]</p> <pre><code> String[] columns = line.split(FIELD_SEP); String code = columns[0]; **String description = columns[1];** </code></pre> <p>Check the length of <code>columns</code> array, by doing system out:</p> <pre><code>System.out.println("columns length : " + columns.length) </code></pre> <p><strong>Answer Updated after Question updation:</strong></p> <p>After your recent comment and new error point** to <code>this.getProducts</code>, I am assuming that your issue lies in <code>productFile</code> content.</p> <p><strong>Do this:</strong></p> <pre><code>BufferedReader in = new BufferedReader(new FileReader(productsFile)); in.readLine(); </code></pre> <p>The way you are reading it is not right. Do what I have suggested. Then do split after reading the line.</p> <p><strong>Note:</strong> <code>readUTF</code> is for reading characters which have been encoded using <em>formatted</em> UTF-8. This simply means that this reads the data in the form of java primitive types. So that when you read, you can convert them directly back to java primitive type. Not recommend for reading normal UTF-8 string.</p> <p><strong>Note:</strong> <code>readUTF</code> should only used when you have written in the form of java primitive types(<strong>ie:</strong> <code>writeUTF</code>). <strong>Reading/Writing</strong> in the form of java primitive types.</p> <p><strong>Answer Updated (As OP wants to read it as java primitive types):</strong></p> <p>The way you are reading and getting the result is perfectly fine. Because <code>readUTF</code> is supposed to read java primitive types. Which is why you are getting <em>java</em>(check text file) in <code>line</code> variable. Check the way you are writing it. I don't know why you are doing string split when you are directly reading java primitive types. You need to keep doing <code>readUTF</code> and create the <code>Product</code> object out of it.</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