Note that there are some explanatory texts on larger screens.

plurals
  1. POfilling an bidimensionnal array in java
    primarykey
    data
    text
    <p>I'm having some issues filling a bidimensionnal double-array out of a multi-dimensional object-array. My problem is that null value are causing a <code>java.lang.NullPointerException at IOControl.ReadCSV$1.compare(ReadCSV.java:328) at IOControl.ReadCSV$1.compare(ReadCSV.java:1) at java.util.TimSort.countRunAndMakeAscending(Unknown Source) at java.util.TimSort.sort(Unknown Source) at java.util.TimSort.sort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at IOControl.ReadCSV.run(ReadCSV.java:324) at en.window.Main.main(Main.java:46)</code> , and i really don't understand why.</p> <p><strong>What am i doing ?</strong> I'm reading a CSV - file, i'm storing the input line by line, in my array of object <code>Data</code> called <code>content</code>. Then i'm filling a <code>double</code> bidimensional array called <code>sortedOutput</code> in which i would like to store <code>LeftSpeed</code> and <code>NormAvgPowOutput</code> which are double value stored in my <code>Data</code> array.</p> <p><strong>What i want :</strong> To have my <code>sortedOutput</code> bidimensionnal array sorted from the smallest to the biggest value on dimension 0 : for variable <code>LeftSpeed</code>.</p> <p>So how can i avoid the null values? Because when i try to spot them while filling my second array, the compilator says i can't compare a double to a <code>null</code> value.</p> <p>Sorry for the long post, hope you guys can help me out :) Here my code : </p> <pre><code>public void run(String path, int length) { /* * Main function of ReadCSV class. * Open / reads / closes the file. * Fills the object. */ BufferedReader br = null; String input = ""; String cvsSplitBy = ","; int[] pos = new int[200]; Data[] content = new Data[length]; Double[][] sortedOutput = new Double[length][4]; int i = 0; int j = 0; int k = 0; try { br = new BufferedReader(new FileReader(path)); while ((input = br.readLine()) != null) { // use comma as separator String[] lines = input.split(cvsSplitBy); content[i] = new Data(); if (i == 0) { //not relevant here } else { j = 0; content[i].setTime("TIME", getTime(pos[j++], lines)); content[i].setData("DATA", getContent(pos[j++], lines)); //etc } // gets rid of non coherent or useless values, e.g negative power, ... if (content[i].lhWdStdev &gt; 0 &amp;&amp; content[i].rhWdStdev &gt; 0) { normalizeData(content[i]); // not relevant content[k].setLeftSpeed(); sortedOutput[k][0] = content[k].getLeftSpeed(); sortedOutput[k][2] = content[k].getNormAvgPowOutput(); Arrays.sort(sortedOutput, new java.util.Comparator&lt;Double[]&gt;() { public int compare(Double[]a, Double[]b) { return Double.compare(a[0], b[0]); } }); } if (sortedOutput[k][0] == null) { System.out.println("FAIL"); } System.out.println("Output = " +sortedOutput[k][0]); i++; k++; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // Again it's not relevant } </code></pre> <p>EDIT : So finally i made 2 huge misstakes. First i tried to sort the array in the loop, that means BEFORE it was fully filled (thanks @Ted Hopp). Secondly i didn't handle the <code>null</code> values correctly. I should have checked if (<code>a != null</code>, <code>b != null</code>, <code>a[0] != null</code>, <code>b[0] != null</code> ) and then return the new order. (thanks @jboi ).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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