Reading data with arrays
in
Programming Questions
•
3 months ago
Hi all.
I load data from a csv file that I visualize afterwards - so far so good. I want then to load another csv data file and I get a Null Pointer Exception, possibly because the new data file has different data entries.
What is the best method to make sure that I don't get this exception? Shall I use try / throw, but I don't know how to create the handling.
Here is some code that I use:
- FloatTable data;
- Plot[] sensorData;
- String fileName;
- color[] colorPlot = {
- #5679C1, #950095, #F79432
- };
- float valueX;
- int currentColumn = 0;
- int columnCount;
- //String path = "/Users/Desktop/DataFeedFiles/dataFolder/";
- String[] filenames;
- ArrayList<String> fileList;
- String selectedFile = "1.csv";
- int numberOfPlots = 3;
- void setup() {
- size(800, 480);
- loadData();
- }
- void draw () {
- background(200);
- println (selectedFile);
- //Draw points according to the number of columns that are in the file. Each column
- //has one number / point
- for (int i=0; i < columnCount; i++) {
- sensorData[i].drawDataPoint();
- }
- }
- void loadData(){
- File file = new File(selectedFile);
- data = new FloatTable(selectedFile);
- columnCount = data.getColumnCount();
- sensorData = new Plot[numberOfPlots];
- for (int i=0; i<numberOfPlots; i++) {
- sensorData[i] = new Plot ();
- }
- }
- void keyPressed() {
- if (key == 'A' || key == 'a') {
- selectedFile = "1.csv";
- loadData();
- }
- if (key == 'S' || key == 's') {
- selectedFile = "2.csv";
- loadData();
- }
- }
- class FloatTable {
- //Based on Ben Fry's class
- int rowCount;
- int columnCount;
- int columnCountSensors;
- float[][] data;
- String[] rowNames;
- String[] columnNames;
- String[] columns;
- FloatTable(String filename) {
- String[] rows = loadStrings(filename);
- columns = split(rows[0], ','); //Get columns and set the array
- columnNames = subset(columns, 0); // Ignore first columns, get only sensor data
- columnCount = columnNames.length; //Get number of columns, i.e. sensor data
- rowNames = new String[rows.length-1];
- data = new float[rows.length-1][];
- for (int i = 1; i < rows.length; i++) {
- String[] pieces = split(rows[i], ',');
- rowNames[rowCount] = pieces[0]; //Get all the values from column 3, i.e. Minute
- data[rowCount] = parseFloat(subset(pieces, 1)); //getColumn except row 1 - header
- rowCount++;
- }
- data = (float[][]) subset(data, 0, rowCount);
- }
- int getRowCount() {
- return rowCount;
- }
- String getRowName(int rowIndex) {
- return rowNames[rowIndex];
- }
- String[] getRowNames() {
- return rowNames;
- }
- int getRowIndex(String name) {
- for (int i = 0; i < rowCount; i++) {
- if (rowNames[i].equals(name)) {
- return i;
- }
- }
- return -1;
- }
- int getAllColumnCount() {
- return columns.length;
- }
- int getColumnCount() {
- return columnCount;
- }
- int getColumnCountSensors() {
- columnCountSensors = columnCount;
- return columnCountSensors;
- }
- String getColumnName(int colIndex) {
- return columnNames[colIndex];
- }
- String[] getColumnNames() {
- return columnNames;
- }
- float getFloat(int rowIndex, int col) {
- if ((rowIndex < 0) || (rowIndex >= data.length)) {
- throw new RuntimeException("There is no row " + rowIndex);
- }
- if ((col < 0) || (col >= data[rowIndex].length)) {
- throw new RuntimeException("Row " + rowIndex + " does not have a column " + col);
- }
- return data[rowIndex][col];
- }
- boolean isValid(int row, int col) {
- if (row < 0) return false;
- if (row >= rowCount) return false;
- if (col >= columnCount) return false;
- if (col >= data[row].length) return false;
- if (col < 0) return false;
- return !Float.isNaN(data[row][col]);
- }
- float getTableMin(int colNum) {
- float m = Float.MAX_VALUE;
- for (int row = 0; row < rowCount; row++) {
- //for (int col = 0; col < columnCount; col++) {
- //if (isValid(row, col)) {
- if (data[row][colNum] < m) {
- m = data[row][colNum];
- //}
- //}
- }
- }
- return m;
- }
- float getTableMax(int colNum) {
- float m = -Float.MAX_VALUE;
- for (int row = 0; row < rowCount; row++) {
- // for (int col = 0; col < columnCount; col++) {
- //if (isValid(row, col)) {
- if (data[row][colNum] > m) {
- m = data[row][colNum];
- //}
- //}
- }
- }
- return m;
- }
- float getColumnMin(int col) {
- float m = Float.MAX_VALUE;
- for (int i = 0; i < rowCount; i++) {
- if (isValid(i, col)) {
- if (data[i][col] < m) {
- m = data[i][col];
- }
- }
- }
- return m;
- }
- float getColumnMax(int col) {
- float m = -Float.MAX_VALUE;
- for (int i = 0; i < rowCount; i++) {
- if (isValid(i, col)) {
- if (data[i][col] > m) {
- m = data[i][col];
- }
- }
- }
- return m;
- }
- float getRowMin(int row) {
- float m = Float.MAX_VALUE;
- for (int i = 0; i < columnCount; i++) {
- if (isValid(row, i)) {
- if (data[row][i] < m) {
- m = data[row][i];
- }
- }
- }
- return m;
- }
- float getRowMax(int row) {
- float m = -Float.MAX_VALUE;
- for (int i = 0; i < columnCount; i++) {
- if (isValid(row, i)) {
- if (data[row][i] > m) {
- m = data[row][i];
- }
- }
- }
- return m;
- }
- }
- class Plot {
- //Take csv data and create plots
- Plot () {
- }
- void drawDataPoint() {
- fill(#5679C1);
- //strokeWeight(1);
- //stroke(#5679C1);
- beginShape( );
- for (int i = 0; i < columnCount; i++) {
- float value = data.getFloat(i, 0);
- float x = map(i, data.getTableMin(0), data.getTableMax(0), 0, width);
- float y = map(value, data.getTableMin(0), data.getTableMax(0), 0, height);
- point(x, y);
- }
- endShape( );
- }
- }
1