newbie here,
probably a logical error so, I started with a set of codes without class
and then I tried to redo it by using it with the same outcome, but now I keep getting a NullPointerException
error on words[i].display();
what am I doing wrong in my codes? below are my before and after codes... thank you in advance for anyone who can help!
also, I tried doing it with a normal string or without loading an external file and it works fine! why do I get it when I start using a loadstrings
is there any difference?
BEFORE:
String [] allWords;
int index = 0 ;
float x;
float y;
void setup () {
size (500,500);
background (255); //background : white
String [] lines = loadStrings ("alice_just_text.txt"); //imports the
external file
String text = join(lines, " "); //make into one long string
allWords = splitTokens (text, ",.?!:-;:()03 "); //splits it by word
x = 100; //where they start
y = 150;
}
void draw() {
background (255);
for (int i = 0; i < 50; i++) { //produces 50 words
x = x + random (-3,3); //makes the words move or shake
y = y + random (-3,3); //makes the words move or shake
int index = int(random(allWords.length)); //random selector of words
textSize (random(10,80)); //random font sizes
fill (0); //font color: black
textAlign (CENTER,CENTER);
text (allWords[index], x, y, width/2, height/2);
println(allWords[index]);
index++ ;
}
}
and the AFTER:
String [] allWords;
word [] words;
int index = 0 ;
void setup () {
size (500,500);
background (255); //background : white
textSize (random(10,80)); //random font size
String [] lines = loadStrings ("alice_just_text.txt");
String text = join(lines, " "); //make into one long string
allWords = splitTokens (text, ",.?!:-;:()03 "); //splits it by word
}
void draw() {
background (255);
for (int i = 0; i < 50; i++) { //produces 50 words
words[i].display();
}
}
class word {
float x;
float y;
word(float x, float y) {
this.x = x;
this.y = y;
}
void move() {
x = 120 + random (-3,3); //variables sets random positions
y = 130 + random (-3,3); //variables sets random positions
}
void display() {
int index = int(random(allWords.length));
fill (0); //font color: black
textAlign (CENTER,CENTER); //should make it start at the center
text (allWords[index], x, y, width/2, height/2); //positions
}
}