# How to populate a String?

**URL:** <https://discourse.processing.org/t/how-to-populate-a-string/6029>\
**Category:** Coding Questions\
**Created:** [November 28, 2018, 2:13pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029 "2018-11-28T14:13:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![polispace](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@polispace](https://discourse.processing.org/u/polispace)\
**Post date:** [November 28, 2018, 2:13pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029/1 "2018-11-28T14:13:27Z")

</div>

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?  
Thanks

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

```auto
PFont font;
String [] lines;

void setup() {
  size(900, 500);
  background(0);
  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?
    textFont(font);
    fill(255);
    text(lines[i], 300, 250);
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 28, 2018, 2:33pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029/2 "2018-11-28T14:33:37Z")

</div>

- -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

 ![snap-023](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/62b2d37b8a3c641f1a4853cc0a20daeccfc12faf.png)

---

<div class="post-metadata">

**Author:** ![polispace](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@polispace](https://discourse.processing.org/u/polispace)\
**Post date:** [November 28, 2018, 2:46pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029/3 "2018-11-28T14:46:27Z")

</div>

![41](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/28ff5fe0b16b49a069214cf9b89275311c943417.jpeg)

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

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 28, 2018, 2:47pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029/4 "2018-11-28T14:47:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![polispace](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@polispace](https://discourse.processing.org/u/polispace)\
**Post date:** [November 28, 2018, 2:57pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029/5 "2018-11-28T14:57:09Z")

</div>

oh I see now! thank you ver much!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 28, 2018, 4:24pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029/6 "2018-11-28T16:24:47Z")

</div>

check  
[https://processing.org/reference/createGraphics\_.html](https://processing.org/reference/createGraphics_.html)  
with this it is possible to create a image of your word cloud  
using random color and position

you make it at **setup**

```auto
  make_wordcloud();

```

and in normal **draw** loop just call the picture

```auto
image(wordcloud,0,0); 

```

> **test**
>
> ```auto
> PGraphics wordcloud;
> 
> void make_wordcloud() {
> wordcloud = createGraphics(width-10, height-10);
> wordcloud.beginDraw();
> wordcloud.textFont(font);
> for (String l : lines) {
> wordcloud.fill(random(0, 255), random(0, 255), 200);
> wordcloud.textSize(random(15,85));
> wordcloud.text(l, random(0, 430), random(40, 480));
> }
> wordcloud.endDraw();
> }
> 
> ```

and if later the word list changes make the picture again

---

<div class="post-metadata">

**Author:** ![polispace](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@polispace](https://discourse.processing.org/u/polispace)\
**Post date:** [November 28, 2018, 8:42pm UTC](https://discourse.processing.org/t/how-to-populate-a-string/6029/7 "2018-11-28T20:42:14Z")

</div>

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 🙂 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
