# How to use text() command to print string array to screen

**URL:** <https://discourse.processing.org/t/how-to-use-text-command-to-print-string-array-to-screen/16574>\
**Category:** Coding Questions\
**Created:** [December 22, 2019, 3:24pm UTC](https://discourse.processing.org/t/how-to-use-text-command-to-print-string-array-to-screen/16574 "2019-12-22T15:24:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TechMaster04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/techmaster04/32/14929_2.png) [@TechMaster04](https://discourse.processing.org/u/TechMaster04)\
**Post date:** [December 22, 2019, 3:24pm UTC](https://discourse.processing.org/t/how-to-use-text-command-to-print-string-array-to-screen/16574/1 "2019-12-22T15:24:09Z")

</div>

Hello

Long story short: I’m porting my python program to processing to add a GUI. Here is my code so far:

```auto
int i = 0;

String currentName = "";

int z = i+1;

String names[] = new String[100];

void setup(){
  size(700,500);
}

void draw(){
  background(0);
  textAlign(CENTER);
  textSize(22);
  text("Welcome to the TechMaster Industries TechHelp Desk \n Please type in your name followed by ENTER to \n reserve a spot in the queue",350,50);
  textSize(32);
  text(currentName,350,160);
  textAlign(LEFT);
  text("Current Queue",10,250);
}

void keyPressed(){
   if (key == BACKSPACE){
    currentName= currentName.substring(0, max(0, currentName.length()-1)); }
   else if (key == ENTER){
     names[i] = currentName;
     currentName = "";
     println(names[i]);
     i= i+1;
   }
   else{
   currentName += key;
}
}

```

I want to use the text() command to print out the names[] array. I’ve tried putting in text(names[i],350,250); with no luck

Anyone know of a way to do this? Thanks in advance

---

<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:** [December 22, 2019, 3:47pm UTC](https://discourse.processing.org/t/how-to-use-text-command-to-print-string-array-to-screen/16574/2 "2019-12-22T15:47:22Z")

</div>

> [@TechMaster04](#):
>
> text(names[i],350,250);

```auto
for ( int i = 0 ; i < 15 ; i++ ) text( names[i],20, 20+i*30 );

```

---

<div class="post-metadata">

**Author:** ![TechMaster04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/techmaster04/32/14929_2.png) [@TechMaster04](https://discourse.processing.org/u/TechMaster04)\
**Post date:** [December 22, 2019, 4:34pm UTC](https://discourse.processing.org/t/how-to-use-text-command-to-print-string-array-to-screen/16574/4 "2019-12-22T16:34:52Z")

</div>

Issue: I’m getting NullPointerException when I put that in my code

New Code:

```auto
int i = 0;

String currentName = "";

int z = i+1;

String names[] = new String[100];

void setup(){
  size(700,500);
}

void draw(){
  background(0);
  textAlign(CENTER);
  textSize(22);
  text("Welcome to the TechMaster Industries TechHelp Desk \n Please type in your name followed by ENTER to \n reserve a spot in the queue",350,50);
  textSize(32);
  text(currentName,350,160);
  textAlign(LEFT);
  text("Current Queue",10,250);
  for(int i = 0 ; i < 15 ; i++ ) text(names[i],20, 20+i*30);
  

}

void keyPressed(){
   if (key == BACKSPACE){
    currentName= currentName.substring(0, max(0, currentName.length()-1)); }
   else if (key == ENTER){
     names[i] = currentName;
     currentName = "";
     println(names[i]);
     i= i+1;
   }
   else{
   currentName += key;
}
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 22, 2019, 5:09pm UTC](https://discourse.processing.org/t/how-to-use-text-command-to-print-string-array-to-screen/16574/5 "2019-12-22T17:09:46Z")

</div>

Hello,

You need to initialize the array otherwise it is a “null” and you get that error.

Default array values:

> **[Default array values in Java - GeeksforGeeks](https://www.geeksforgeeks.org/default-array-values-in-java/)**
>
> If we don’t assign values to array elements, and try to access them, compiler does not produce error as in case of simple variables. Instead… Read More »

```auto
  // Uncomment this to initialize array with a string:
  //String letter = "put String here";
  //for(int i = 0 ; i < 15 ; i++ ) names[i] = letter;
  
  for(int i = 0 ; i < 15 ; i++ ) 
    {
    if (names[i] != null) //Checks for a null character
      text(names[i], 20, 20+i*30);
    else                                     
      text("null", 20, 20+i*30);
    }

```

🙂

---

<div class="post-metadata">

**Author:** ![TechMaster04](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/techmaster04/32/14929_2.png) [@TechMaster04](https://discourse.processing.org/u/TechMaster04)\
**Post date:** [December 22, 2019, 5:16pm UTC](https://discourse.processing.org/t/how-to-use-text-command-to-print-string-array-to-screen/16574/6 "2019-12-22T17:16:18Z")

</div>

Thank you so much, This was exactly what I was looking for
