Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

HiVis sketch pulling xlsx file gives NullPointerException?

$
0
0

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!!


Viewing all articles
Browse latest Browse all 2896

Trending Articles