[DUPLICATE] Unsure what happened : "Error inside Serial.serialEvent()"

Am learning to access an SD card and then displaying the contents to an LCD.
I have a Nano-compatible connected to the LCDisplay via I2C and SD card module via SPI.
Both modules work using them one at a time but now the difficulties start when i start combining them together.

The code actually works, but sometimes the LCDisplay doesn't lcd.clear() - i thought it was the code but discovered that it works after i closed and then opened the Serial Monitor (for debugging).

And now i encounter an error message which i have no idea about - is this hardware related ? (or bad setup??)

Here's the error message;

Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
	at gnu.io.RXTXPort.nativeavailable(Native Method)
	at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
	at processing.app.Serial.serialEvent(Serial.java:258)
	at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
	at gnu.io.RXTXPort.eventLoop(Native Method)
	at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Error inside Serial.serialEvent()
java.io.IOException: Bad file descriptor in nativeavailable
	at gnu.io.RXTXPort.nativeavailable(Native Method)
	at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
	at processing.app.Serial.serialEvent(Serial.java:258)
	at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
	at gnu.io.RXTXPort.eventLoop(Native Method)
	at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Error inside Serial.serialEvent()

i also notice a red LED is lit up on the Nano (Iteaduino Nano) which is visible in the pic below.

this is the code itself;

/*
** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
** by Edward Comer
** uses F Malpartida's NewLiquidCrystal library.

 */
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x20 //
#define BACKLIGHT_PIN     7
// using MJKDZ board
#define En_pin  4    
#define Rw_pin  5   
#define Rs_pin  6   
#define D4_pin  0   
#define D5_pin  1   
#define D6_pin  2   
#define D7_pin  3   

LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

/*
 SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10
                           created  24 Nov 2010
                           modified 9 Apr 2012       by Tom Igoe
 
 This example code is in the public domain.
 */
#include <SD.h>
// Note that even if it's not used as the CS pin, the hardware CS pin
// must be left as an output or the SD library functions will not work.
const int chipSelect = 10;


void setup()
{
  lcd.begin (16,2);
  //Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN,NEGATIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();
  lcd.print("LCD connected");  

  Serial.begin(9600);

  Serial.print("Initializing SD card...");
  // make sure default chip select pin is set to output
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  
} // end setup()

String readChar;
int posChar;

void loop()
{
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("what2prn.txt");  // default as FILE_READ
  lcd.clear();
  
//  lcd.autoscroll();
  
if (dataFile) {
    while(dataFile.available())  {
    // it's THIS SIMPLE - display byte-by-byte, NO need String var
    lcd.write(dataFile.read());
//    lcd.autoscroll();
    }
//
  posChar = 0;
  lcd.setCursor(posChar,1);
  lcd.write("End of file reached");
  Serial.println("End of file reached");
  delay(500);
  Serial.println("Clearing LCDisplay");
  lcd.clear();  
  delay(500);
 //
  }
}
#define D4_pin  0   
#define D5_pin  1

You appear to be connecting your LCD to the hardware serial pins. Not a recommended practice when you want to use them to communicate with the PC.

It's really hard to tell from that picture if that is indeed the case.

PaulS:

#define D4_pin  0   

#define D5_pin  1



You appear to be connecting your LCD to the hardware serial pins. Not a recommended practice when you want to use them to communicate with the PC.

It's really hard to tell from that picture if that is indeed the case.

those pins are for the 16 pins of the LCDisplay which are accessed via I2C through the LCD display library being used.
(i hope i've used the terms correctly)

are you suggesting there might be 'clashes' between the two libraries ? (SD & LCD)

those pins are for the 16 pins of the LCDisplay which are accessed via I2C through the LCD display library being used.

An I2C device has two pins.

Do those numbers refer to pins on the Arduino or do they refer to pins on the LCD?

PaulS:

those pins are for the 16 pins of the LCDisplay which are accessed via I2C through the LCD display library being used.

An I2C device has two pins.

Do those numbers refer to pins on the Arduino or do they refer to pins on the LCD?

yes, to the pins on the LCDisplay - i am only using 2 pins for the I2C connection, the "standard" A4 & A5.
anyway, i did a search (which i should've done first !) and found these two related threads which seem to indicate it's not a programming error - will repost this thread in the General Electronics section now.

Trouble with arduino nano serial resetting - Arduino Forum
forum.arduino.cc/index.php?topic=145981.0

Error inside Serial.serialEvent() - Arduino Forum
forum.arduino.cc/index.php?topic=49261.0

Is the problem you describe here the same problem you describe there...
http://forum.arduino.cc/index.php?topic=213256.0

yes it is, i believe we can lock this thread.