How to populate a String?

Hi there,

I’m braking down a bigger code in to small parts.
Now i’m stuck with what I guess is a basic things.

I would like to simply display words I have in a text file .txt.
I written a code but i get the error “null pointer exception”, I can’t understand how to populate a string.
Can some one explain me please, what I am doing wrong?

PS: i’ve already saw tones of video/documentation and so on… but i’m still stuck… :frowning:

PFont font;
String [] lines;

void setup() {
  size(900, 500);
  font = loadFont("MaisonNeue-Demi-96.vlw");
  String[] lines = loadStrings("myText.txt"); 
  printArray(lines); // I visualize the content of the file in the console 

void draw() {
  for (int i = 0; i < lines.length; i++) { // error, I guess lines is empty --> how can i populate this string?
    text(lines[i], 300, 250);
  • -a- confirm that the file is not empty
    you see the lines printed?
  • -b- you try to print same words to screen
    • in a continuous loop but with no background();
    • print all words to the same position

Hey, thanks for the answer.
I load a screenshot to confirm that i see the lines.

I set the background in the setup function, is that not enough?
I was expecting that the words would have been displayed all in the same position but I was thinking to find a solution after I could manage to display them

The reason is that you are creating a new variable by using String[] in your setup. So your variable is not global anymore. Just delete that and it should be fine.


oh I see now! thank you ver much!

with this it is possible to create a image of your word cloud
using random color and position

you make it at setup


and in normal draw loop just call the picture

PGraphics wordcloud;

void make_wordcloud() {
  wordcloud = createGraphics(width-10, height-10);
  for (String l : lines) {
    wordcloud.fill(random(0, 255), random(0, 255), 200);
    wordcloud.text(l, random(0, 430), random(40, 480));

and if later the word list changes make the picture again

1 Like

Hey, thank you a lot for your help, it is very kind from you!
I make a test with the function make_wordcloud you suggested me including the PGraphics.
Then more I have the feeling that i’m getting closer to my final goal then more bigger problems that i could not think about comes out!

Now i display the words and every time a new word comes in I redraw and it display it.
a: the words already been displayed they change position with the new draw
b: words are sometimes one over the other
c: the words are random size

Puff :slight_smile: I still have to go trough lots of problems… I’ll keep working on it and try to break down the problems in to smaller ones… I’ll keep you update

so far thank you a lot

1 Like