NullPointerException
in
Programming Questions
•
2 years ago
Hi, at the momeny i am trying to create an interactive sketch that uses imported data from a .txt file. I have created an array of class Info called states, but when i try to access a method from the class, i am shown the error NullPointerException. I am wondering why the program cannot access the information and is giving me this error. The code is the following:
// Arrays to hold data
int num = 49;
Info states[] = new Info[num];
String[] data, rawText;
float[] points;
String c, z;
float x, y;
void setup() {
rectMode(CENTER);
size(700,500);
background(0);
smooth();
scale(0.5);
// Load strings
rawText = loadStrings("statedata2.txt");
for (int i=0; i<rawText.length; i=i+2) {
Info states = new Info(rawText[i]);
// Convert x-y data
points = float(split(rawText[i+1], ","));
// Draw points
fill(255, 255, 127);
stroke(255, 0,0);
strokeWeight(1);
beginShape();
for (int j=0; j<points.length; j=j+2) {
vertex(points[j], points[j+1]);
}
endShape(CLOSE);
// Draw the capital
String[] stData = split(rawText[i], ",");
float x, y;
String z;
x = float(stData[3]);
y = float(stData[4]);
z = stData[1];
stroke(0);
strokeWeight(5);
fill(0);
text(z ,x, y);
}
}
void draw(){
for(int i=0; i<states.length; i++)
states[i].showinfo();
}
class Info {
String stText;
float xpos, ypos;
String e;
float w=50.0;
float l=50.0;
float r=15.0;
Info(String stText){
String[] stData = split(stText, ",");
xpos = float(stData[3]);
ypos = float(stData[4]);
e = stData[5];
}
void showinfo(){
if ( dist(xpos, ypos, mouseX, mouseY) < 0.5*r ) {
fill(0);
rect(xpos, ypos, w, l);
text(e, xpos, ypos, w, l);
}
}
}
// Arrays to hold data
int num = 49;
Info states[] = new Info[num];
String[] data, rawText;
float[] points;
String c, z;
float x, y;
void setup() {
rectMode(CENTER);
size(700,500);
background(0);
smooth();
scale(0.5);
// Load strings
rawText = loadStrings("statedata2.txt");
for (int i=0; i<rawText.length; i=i+2) {
Info states = new Info(rawText[i]);
// Convert x-y data
points = float(split(rawText[i+1], ","));
// Draw points
fill(255, 255, 127);
stroke(255, 0,0);
strokeWeight(1);
beginShape();
for (int j=0; j<points.length; j=j+2) {
vertex(points[j], points[j+1]);
}
endShape(CLOSE);
// Draw the capital
String[] stData = split(rawText[i], ",");
float x, y;
String z;
x = float(stData[3]);
y = float(stData[4]);
z = stData[1];
stroke(0);
strokeWeight(5);
fill(0);
text(z ,x, y);
}
}
void draw(){
for(int i=0; i<states.length; i++)
states[i].showinfo();
}
class Info {
String stText;
float xpos, ypos;
String e;
float w=50.0;
float l=50.0;
float r=15.0;
Info(String stText){
String[] stData = split(stText, ",");
xpos = float(stData[3]);
ypos = float(stData[4]);
e = stData[5];
}
void showinfo(){
if ( dist(xpos, ypos, mouseX, mouseY) < 0.5*r ) {
fill(0);
rect(xpos, ypos, w, l);
text(e, xpos, ypos, w, l);
}
}
}
1