null pointer exception

Hey guys, so i'm very new to this and I was attempting to create a Simon says program for my ardruino. The arduino program works fine but when I try to communicate with the serial port from processing I keep getting the "null pointer exception" message. I have looked online for help but nothing makes sense in relation to my specific problem. Could someone please help me out, I would greatly appreciate it. Here is my processing code:

void setup() {
  println(Serial.list()); 


}
void draw(){
if (key == 'a'){
  port.write(1);
}
if (key == 'w'){
  port.write(2);
}
if (key == 'd'){
  port.write(3);
}
if (key == 's'){
  port.write(4);
}
}

...when I try to communicate with the serial port from processing I keep getting the "null pointer exception" message...

Processing is raising that exception?

Yes i'm using processing 2.2.1

Forgot to mention, the error specifically highlights the portwrite() functions.

Without the Processing code you are unlikely to get help. Please use
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags.

I'm trying to understand what you are referring too. That is the entire processing code. Ill give it again with the brackets:

void setup() {
  println(Serial.list()); 


}
void draw(){
if (key == 'a'){
  port.write(1);
}
if (key == 'w'){
  port.write(2);
}
if (key == 'd'){
  port.write(3);
}
if (key == 's'){
  port.write(4);
}
}

Here is the adruino code as well...

int duration2[] = {100, 200, 100, 200, 100, 400, 100, 100, 100, 100, 200, 100, 500};
int duration[] = {100, 100, 100, 300, 100, 300};
boolean ledpin[] = {9, 10, 11, 12};  // LED pins
int turn = 0;  // turn counter
int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far)
int inputArray[100];  
int incomingByte;
int keystate = 0;

void setup() 
{
  Serial.begin(9600);

  for(int x=0; x<4; x++)  // LED pins are outputs
  {
    pinMode(ledpin[x], OUTPUT);
  }

  randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function
  
}
void loop() 
{  
 if (Serial.available() > 0) {
  incomingByte = Serial.read();
 } 
  for (int y=0; y<=99; y++)
  {
    //function for generating the array to be matched by the player
    digitalWrite(ledpin[0], HIGH);
    digitalWrite(ledpin[1], HIGH);
    digitalWrite(ledpin[2], HIGH);
    digitalWrite(ledpin[3], HIGH);
  
     delay(25);
    }
    
    digitalWrite(ledpin[0], LOW);
    digitalWrite(ledpin[1], LOW);
    digitalWrite(ledpin[2], LOW);
    digitalWrite(ledpin[3], LOW);
    delay(1000);
  
    for (int y=turn; y <= turn; y++)
    { //Limited by the turn variable
      //Serial.println(""); //Some serial output to follow along
      //Serial.print("Turn: ");
      //Serial.print(y);
      //Serial.println("");
      randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
      for (int x=0; x <= turn; x++)
      {
        Serial.print(randomArray[x]);
      
        for(int y=0; y<4; y++)
        {
      
          if (randomArray[x] == 1 && ledpin[y] == 9) 
          {  //if statements to display the stored values in the array
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }

          if (randomArray[x] == 2 && ledpin[y] == 10) 
          {
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }
  
          if (randomArray[x] == 3 && ledpin[y] == 11) 
          {
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }

          if (randomArray[x] == 4 && ledpin[y] == 12) 
          {
            digitalWrite(ledpin[y], HIGH);
            delay(400);
            digitalWrite(ledpin[y], LOW);
            delay(100);
          }
        }
      }
    }
    input();
  }
 
void input() { //Function for allowing user input and checking input against the generated array

  for (int x=0; x <= turn;)
  { //Statement controlled by turn count

    for(int y=0; y<4; y++)
    {
      
      if (Serial.available() > 0) {
       // keystate = 1 - keystate;
      }
    
      if (keystate == LOW && incomingByte == 1)
      { //Checking for button push
        digitalWrite(ledpin[0], HIGH);
        delay(200);
        digitalWrite(ledpin[0], LOW);
        inputArray[x] = 1;
        delay(250);
        //Serial.print(" ");
        //Serial.print(1);
        if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
          fail();                              //the value in the same spot on the generated array
        }                                      //The fail function is called if it does not match
        x++;
      }
       if (keystate == LOW && incomingByte == 2)
      {
        digitalWrite(ledpin[1], HIGH);
        delay(200);
        digitalWrite(ledpin[1], LOW);
        inputArray[x] = 2;
        delay(250);
        //Serial.print(" ");
        //Serial.print(2);
        if (inputArray[x] != randomArray[x]) {
          fail();
        }
        x++;
      }

      if (keystate == LOW && incomingByte == 3)
      {
        digitalWrite(ledpin[2], HIGH);
        delay(200);
        digitalWrite(ledpin[2], LOW);
        inputArray[x] = 3;
        delay(250);
        //Serial.print(" ");
        //Serial.print(3);
        if (inputArray[x] != randomArray[x]) {
          fail();
        }
        x++;
      }

      if (keystate == LOW && incomingByte == 4)
      {
        digitalWrite(ledpin[3], HIGH);
        delay(200);
        digitalWrite(ledpin[3], LOW);
        inputArray[x] = 4;
        delay(250);
        //Serial.print(" ");
        //Serial.print(4);
        if (inputArray[x] != randomArray[x]) 
        {
          fail();
        }
        x++;
      }
    }
  }
  delay(500);
  turn++; //Increments the turn count, also the last action before starting the output function over again
}

void fail() { //Function used if the player fails to match the sequence
 
  for (int y=0; y<=2; y++)
  { //Flashes lights for failure
    
    digitalWrite(ledpin[0], HIGH);
    digitalWrite(ledpin[1], HIGH);
    digitalWrite(ledpin[2], HIGH);
    digitalWrite(ledpin[3], HIGH);
    delay(200);
    digitalWrite(ledpin[0], LOW);
    digitalWrite(ledpin[1], LOW);
    digitalWrite(ledpin[2], LOW);
    digitalWrite(ledpin[3], LOW);
    delay(200);
  }
  delay(500);
  turn = -1; //Resets turn value so the game starts over without need for a reset button
}

This is the first time I am posting anything so please forgive me for any misunderstanding.
Let me know if I am missing anything else. Thank you.

Where, in the Processing code, have you assigned a value to port? Where have you even declared port?

I did, for some reason it did not copy over.
Here is the processing code again:

import processing.serial.*;
Serial port;

void setup() {
  println(Serial.list()); 
  port = new Serial(this, Serial.list()[0], 9600);

}
void draw(){
if (key == 'a'){
  port.write('a');
}
if (key == 'w'){
  port.write('w');
}
if (key == 'd'){
  port.write('d');
}
if (key == 's'){
  port.write('s');
}
}
  port = new Serial(this, Serial.list()[0], 9600);

Why do you assume that Serial.list() returns an array with any elements in it? Have you tried:

println("Com ports the Arduino might be connected to:");
println(Serial.list());
println("================");

to see if there is anything in the list?

I'm trying to understand what you are referring too.

So am I. My apologies.

#include <stdio.h>

int main ()
{
int var = 20; /* actual variable declaration */
int ip; / pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );

/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );

return 0;
}

Software Company in Lucknow