Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Chat client, string array. what am i doing wrong?

$
0
0

Can anyone please tell me what am i doing wrong? I want to create a chat client and i made a string array to write all the data that i send to the server but i'm getting trouble with the string array. at this point i want to write on a window something like:

nick: nickx

nickx: dataOut[0]

nickx: dataOut[1]

nickx: dataOut[2]

nickx: dataOut[3]

nickx: dataOut[4]

nickx: dataOut[5] (...)

here's my code. i'm getting a lot of "null pointer exception" and i don't know why. if anybody can tell me ...

// PROGRAMA CLIENTE

import processing.net.*;

Client myClient;

PFont myFont;

int i=0;

int y =50;

int x=0;

String nick="";

String dataIn="";

String [] dataOut;

String ip [];

void setup() {

  background(255);

  size(500,500);

  myFont = loadFont("ACaslonPro-Regular-20.vlw");

  textFont(myFont, 20);

  fill(0);

  textAlign(LEFT);

  myClient = new Client(this, "localhost", 10002);

}

void draw() {

  background(255);

  text("nick: ",10,20);

  text (nick,60,20);

  if(i>0){

    text (nick + ": " + dataOut[x], 10,y);

    if (myClient.available() > 0) {

    nick = myClient.readString();

    dataOut[x] = myClient.readString();

    }

  }

}

void keyPressed() {

  if(i>0){

    if (key== ENTER && dataOut[x]!= ""){

    myClient.write(dataOut[x]);

    x=x+1;

    dataOut[x]="";

    y = y+20;

    }

    if (keyCode == BACKSPACE) {

      if (dataOut[x].length() > 0) {

      dataOut[x] = dataOut[x].substring(0, dataOut[x].length()-1);

      }

    } else if (keyCode == DELETE) {

      dataOut[x] = "";

      } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT && keyCode!= ENTER) {

        dataOut[x] = dataOut[x] + key;

        }

  }

    if(i==0){

      if (key==ENTER && nick != ""){

          myClient.write(nick);

          i=1;
      }

      if (keyCode == BACKSPACE) {

         if (nick.length() > 0) {

         nick = nick.substring(0, nick.length()-1);

         }
      } else if (keyCode == DELETE) {

           nick = "";

         }else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT && keyCode!= ENTER) {

              nick = nick + key;

          }

    }

}

Viewing all articles
Browse latest Browse all 2896

Trending Articles