We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi All, I'm fairly new to using arrays and I'm not sure why this code isn't working. All I'm trying to do is read a text file, convert it to a 2D array, and then be able to read that matrix given whatever "coordinates" I put inside the square brackets. In this code I'm trying to print the variable at (2,2). The file I'm reading from is just an 8x8 matrix separated by commas. My problem is everytime I try to run this, I get a NullPointerException on the println line. I apologize if i am saying matrix when i mean 2d array.
int[][] data;
void setup() {
size(200, 200);
int[][] data = new int[7][7];
// Load text file as a String
String[] stuff = loadStrings("data.csv");
// Convert string into an array of integers using ',' as a delimiter
for(int a=0;a<7;a++) {
data[a] = int(split(stuff[a], ','));
}
}
void draw() {
println(data[2][2]);
}
Answers
https://forum.Processing.org/two/discussion/20185/how-to-read-from-a-text-file-to-arraylist-of-pvectors#Item_4
Hi, i would rather not have to go through the use of pvectors. I mostly want to know why my code is giving me a NullPointerException.
I found my problem, i needed to define my array outside of the setup function so that it could be accessed in the draw function. Thanks for the input, GoToLoop.
"data.csv":
@abotics:
Your initial error was in line 5
When you repeat the variable type it is seen as a new local variable instead of referring to line 1
When you remove int[][] from line 5 it should work
(Your Solution was different but also good)
Thanks everyone!