Serial Connection to Arduino throws nullPointerException error - best answer gets a poem =)

edited January 2014 in Arduino

I'm getting a "nullPointerException" error in Processing when reading the serial port with data from an Arduino. It successfully pulls the content of the serial port but then throws the error after a few times. When the error occurs processing highlights the line in bold below(val = new....) The first chunk of code is from Processing. I've included the arduino code below as well. Best answer gets a custom poem(subject of their choice).

What's the goal? - I'm taking readings from the photocell via arduino and passing the values to processing to display. <

Processing Code

    import processing.serial.*;


    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port


    void setup()
    {

    String portName = Serial.list()[7]; //change the 0 to a 1 or 2 etc. to match your port
    myPort = new Serial(this, portName, 9600); 

    }

    void draw()
    {
      if ( myPort.available() > 0) 
      {  // If data is available,
     ** val = new String(myPort.readBytesUntil('\n'));**
      println(val); //print it out in the console
      } 


    }

Arduino Code

    /* Photocell simple testing sketch. 
     
    Connect one end of the photocell to 5V, the other end to Analog 0.
    Then connect one end of a 10K resistor from Analog 0 to ground 
     
    int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
    int photocellReading;     // the analog reading from the sensor divider
 
    int horn = 12;
    int marks = 0;
    int total = 0;
    void setup(void) {
      // We'll send debugging information via the Serial monitor
      Serial.begin(9600); 
    pinMode(horn, OUTPUT);  
    }
     
    void loop(void) {
      photocellReading = analogRead(photocellPin);  
     //writes values that the photocell is reading
      //Serial.print("Analog reading = ");
     // Serial.println(photocellReading);     // the raw analog reading
     if(photocellReading < 80){
       digitalWrite(horn, HIGH);
       marks++;
      //Serial.println(marks); 
      }else{
        digitalWrite(horn, LOW);
        total = total+marks;
        marks = 0;
      }
      Serial.println(total); 
      
      delay(100);
}

Answers

Sign In or Register to comment.