Why is serial data triggering a reset?

I'm trying to get some data into my Arduino via serial and Processing. The Arduino sketch I have works when I send data with the Arduino serial monitor. However when I send data via Processing I see the RX light flash (as it should) and then the "L" light blinks. I assume it's rebooting for some reason.

Here's my Arduino sketch. (I have a serial LCD hooked up to the TX pin so it will echo anything received to the screen.)

void setup(){

void loop(){  
    // echo any serial data back.
    if (Serial.available() > 0) {
      while (Serial.available() > 0){
        char incomingByte = Serial.read();

And here's the Processing sketch:

import processing.serial.*;
Serial myPort;
myPort = new Serial(this, Serial.list()[2], 9600);
myPort.write("Hi There, Arduino.");

Does anyone see anything obviously wrong?

Is that the whole processing sketch?

You're aware that the arduino will reset when processing opens the port - right?

The arduino will reset every time the serial port is put on line or off line. If that is all that the processing sketch does I assume that it is finishing and closing the port, thus resting the arduino.

There is also that issue that Processing has setup() and draw() functions that are analogous to the setup() and loop() functions of Arduino, and that code is supposed to be executed only inside a function.

The myPort.write statement is not in a function.

I wasn't aware that opening a serial connection resets the Arduino. Kinda thought something like that would be right at the top of the docs in big bold print. (Or maybe my eyesight is bad.)

(Yes, that's the whole Processing sketch, and no it's not required that Processing commands be inside functions. When run without functions it simply executes everything in order and quits. See for example http://processing.org/learning/gettingstarted/ )

At any rate the problem was that the data was sent when the thing was rebooting. Adding a delay makes thing happy.