-1

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

   }


  }
Kevin Workman
  • 39,413
  • 8
  • 61
  • 94

1 Answers1

0

You declare your words array here:

word [] words;

At this point, words does not have any value. In other words, it has a null value.

Then you try to use that variable here:

words[i].display();

But remember that words is null, so you can't use it like this! How do you get the i index of a non-value? You can't!

You need to actually initialize your words array to something.

If you're following my classes tutorial from your last question, please see the Creating Many Instances section.

Side note: please try to properly format your code (the Processing editor can do it for you automatically, check the menus) and use standard naming conventions (variables start with a lower-case letter, classes start with an upper-case letter). Right now your code is very hard to read.

Kevin Workman
  • 39,413
  • 8
  • 61
  • 94
  • thank you again for putting up with my questions! T^T at least it's making a little more sense now! I just need to pass this course because I need science courses to get into my faculty... programming isn't really my thing but I'm trying hard to understand it! thank you so much again – Noob processor Jul 18 '17 at 19:11
  • I finally made the error disappear btw – Noob processor Jul 18 '17 at 19:25