HiVis sketch pulling xlsx file gives NullPointerException?

edited January 2017 in Library Questions

Hi, more or less complete novice here; please point me elsewhere if this is the wrong place to ask...

Using the HiVis libraries, I'm trying to build a sketch that will pull coordinate info from a spreadsheet in my sketch's data folder and draw dots on a square at those coordinates. I've got a working sketch using selectInput that asks a user to select the xlsx file, but when I sub in the suggested line for pulling a file from the data folder, I get a nullPointerException at that line, java hangs (I have to force quit it) and I get this text:

Could not run the sketch (Target VM failed to initialize). For more information, read revisions.txt and Help → Troubleshooting.

Any idea what I'm doing wrong?

I found the suggested lines here (commented out, midway down page): https://github.com/OliverColeman/hivis/blob/latest/examples/examples/HV02_Tables/HV02_Tables.pde

Broken sketch:

import hivis.common.*;
import hivis.data.*;
import hivis.data.reader.*;
import hivis.data.view.*;
import hivis.example.*;

// Pulls coordinate info from a spreadsheet and draws the coordinates as white dots on a black 180 x 180 square. 

// Stores the data to plot.
DataTable data;

// The series containing the data we want to plot.
DataSeries xSeries;
DataSeries ySeries;

// Method containing one-off setup code.
void setup() {
  // Make a canvas that is 180 pixels wide by 180 pixels high.
  size(180, 180);
  // Pull a spreadsheet from the sketch/data folder.
  DataTable data = HV.loadSpreadSheet(new File("center_coord100_2.xlsx"));
  // The series containing the data we want to plot.
  xSeries = data.getSeries(1);
  ySeries = data.getSeries(2);
}



// Draws the plot.
void draw() {
  background(0);

  // If the data is ready to plot.
  if (ySeries != null) {

    // Gather info from each row.
    for (int row = 0; row < data.length(); row++) {

      // Get values from the series. 

      int x = xSeries.getInt(row);
      int y = ySeries.getInt(row);

      // Draw a dot.
      noStroke();
      fill(255);
      ellipse(x, y, 14, 14);
    }
  }
}

Working sketch:

import hivis.common.*;
import hivis.data.*;
import hivis.data.reader.*;
import hivis.data.view.*;
import hivis.example.*;

// Pulls coordinate info from a spreadsheet and draws the coordinates as dots on a 180 x 180 square. 

// Stores the data to plot.
DataTable data;

// The series containing the data we want to plot.
DataSeries xSeries;
DataSeries ySeries;

// Method containing one-off setup code.
void setup() {
  // Make a canvas that is 180 pixels wide by 180 pixels high.
  size(180, 180);
  // Ask the user to select a spreadsheet to visualise.
  selectInput("Select an excel file to visualise:", "fileSelected");
}


// Method that gets called when a file is selected.
void fileSelected(File selection) {
  // If no file was selected.
  if (selection == null) {
    println("No file selected.");
  } else {
    // Get data from spread sheet. 
    // The SpreadSheetReader will automatically update the DataTable it provides if the source file is changed.
    data = HV.loadSpreadSheet(selection);

    // Get the series containing the data we want to plot. 
    xSeries = data.getSeries(1);
    ySeries = data.getSeries(2);
  }
}


// Draws the plot.
void draw() {
  background(0);

  // If the data is ready to plot.
  if (ySeries != null) {

    // Gather info from each row.
    for (int row = 0; row < data.length(); row++) {

      // Get values from the series. 

      int x = xSeries.getInt(row);
      int y = ySeries.getInt(row);

   // Draw a dot.
      noStroke();
      fill(255);
      ellipse(x, y, 14, 14);
    }
  }
}

Thank you!!

Answers

  • Ok, looks like I fixed it, needed

    int sheet = 0;
    int headerRow = 0;
    int firstDataRow = 1;
    int firstDataColumn = 0;
    DataTable data = HV.loadSpreadSheet(sketchFile("center_coord100_2.xlsx"), sheet, headerRow, firstDataRow, firstDataColumn);
    

    instead of

    DataTable data = HV.loadSpreadSheet(new File("center_coord100_2.xlsx"));

    still got a nullpointerexception from this line:

    for (int row = 0; row < data.length(); row++) {

    until I changed data.length() to the actual number of the length; would be nice to know why.

    thanks in any case!

  • Did you try printing the value of data. Length(). Nothrr suggestion is checking the reference of hivis: https://olivercoleman.github.io/hivis/reference/

    Food for thought: in a table, would length return the number of rows or number of columns?

    For your first question, the new File reference should work if you provide the proper full path. I didn't test it but it is my guess. When you use selectInput, you could use println to check the generated path to ensure is the proper one is being passed. Notice also in one funtion you are passing a File object while on the other one you are passing a String object.

    Kf

  • Thanks for the suggestions; the new File reference didn't work even when I used the proper full path (pulled from the terminal window, even) - still not sure why it didn't work.

    Printing the value of data.Length() didn't work because it would hang (nullPointerException) before getting to that step. I ended up adjusting the sketch so that the data.Length() value is assigned a to an int variable in the setup section, and then used that variable later, which worked great.

    Here's the now-working code:

    import hivis.common.*;
    import hivis.data.*;
    import hivis.data.reader.*;
    import hivis.data.view.*;
    import hivis.example.*;
    
    // Pulls coordinate info from a spreadsheet and draws the coordinates as white dots on a black 180 x 180 square. 
    
    // Stores the data to plot.
    DataTable data;
    
    // The series containing the data we want to plot.
    DataSeries xSeries;
    DataSeries ySeries;
    int sslength;
    
    
    // Method containing one-off setup code.
    void setup() {
      // Make a canvas that is 180 pixels wide by 180 pixels high.
      size(180, 180);
      // Pull a spreadsheet from the sketch/data folder.
    
      int sheet = 0;
      int headerRow = 0;
      int firstDataRow = 1;
      int firstDataColumn = 0;
      DataTable data = HV.loadSpreadSheet(sketchFile("center_coord100_2.xlsx"), sheet, headerRow, firstDataRow, firstDataColumn);
      // The series containing the data we want to plot.
      xSeries = data.getSeries(1);
      ySeries = data.getSeries(2);
      sslength = data.length();
    }
    
    
    
    // Draws the plot.
    void draw() {
      background(0);
    
      // If the data is ready to plot.
      if (ySeries != null) {
    
    
        // Gather info from each row.
        for (int row = 0; row < sslength; row++) {
    
          // Get values from the series. 
    
          int x = xSeries.getInt(row);
          int y = ySeries.getInt(row);
    
          // Draw a dot.
          noStroke();
          fill(255);
          ellipse(x, y, 14, 14);
        }
      }
    }
    

    Thanks anyway for the suggestions-

Sign In or Register to comment.