java.lang.NullPointerException Error while serial communicating with Arduino

Console

portIdentifier: gnu.io.CommPortIdentifier@29453f44
null
java.lang.NullPointerException
at spring.mvc.smart.arduino.SerialReader.(SerialReader.java:30)
at spring.mvc.smart.arduino.Serial.connect(Serial.java:36)
at spring.mvc.smart.arduino.Main.main(Main.java:11)


Serial.java

package spring.mvc.smart.arduino;

import java.io.InputStream;
import java.io.OutputStream;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.SerialPort;

public class Serial {
public Serial() {
super();
}

void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = null;
try {
portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
System.out.println("portIdentifier: "+ portIdentifier);
}catch(NoSuchPortException e) {
System.out.println(e.getMessage());
}
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();

(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();

} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
}


Main

package spring.mvc.smart.arduino;

import org.springframework.context.ApplicationContext;

public class Main {

public static void main(String[] args) {

try {
(new Serial()).connect("COM8");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}

}

}


SerialReader
package spring.mvc.smart.arduino;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import spring.mvc.smart.dao.ReservationDao;
import spring.mvc.smart.dto.Reservation;
import spring.mvc.smart.util.JDBUtil;

/시리얼 읽기/
public class SerialReader implements Runnable{

@Autowired
private ApplicationContext applicationContext;

private JdbcTemplate jdbcTemplate;

//@Autowired
public void setTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
JDBUtil.jdbcTemplate = this.jdbcTemplate;
}

//@Autowired
private ReservationDao reservationDao = (ReservationDao) applicationContext.getBean("reservationDao");

InputStream in;

public SerialReader(InputStream in) {
this.in = in;
}

@Override
public void run() {
byte[] buffer = new byte[1024];
int len = -1;
String str = "";
String strReader = "";
String lastValue = "";
int motion = 9;
int temperature = 0;
int humidity = 0;

try {
while ((len = this.in.read(buffer)) > -1) {
System.out.println("hihi");
str = new String(buffer, 0, len);
strReader += str;
System.out.println(strReader);

if(strReader.length() > 6) {
String[] strArr = strReader.split("@");
System.out.println("strArr[0]: "+strArr[0]);
if(strArr.length > 1) {
System.out.println("strArr.length: "+strArr.length);
lastValue = strArr[strArr.length-2];
System.out.println("lastValue: "+lastValue);
motion = Integer.parseInt(lastValue.split(",")[0]);
temperature = Integer.parseInt(lastValue.split(",")[1]);
humidity = Integer.parseInt(lastValue.split(",")[2]);
System.out.println("motion: "+motion+", temperature: "+temperature+", humidity: "+humidity);
Reservation reservation = new Reservation();
reservation.setIsSeating(motion);
reservation.setReservationNo(1);

Reservation beforeReservation = reservationDao.getReservation(1);

if(beforeReservation.getIsSeating() != motion) { //기존의 착석여부와 다르면
reservationDao.updateIsSeating(reservation);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

}


Serial Writer
package spring.mvc.smart.arduino;

import java.io.IOException;
import java.io.OutputStream;

/보내는 코드/
public class SerialWriter implements Runnable {

OutputStream out;

public SerialWriter(OutputStream out) {
this.out = out;
}

@Override
public void run() {
try {
int c = 0;
while ((c = System.in.read()) > -1) {
this.out.write(c);
}
} catch (IOException e) {
e.printStackTrace();
}

}

}


That looks like a question for a Java programming Forum.

This Forum is for programming Arduinos using C/C++.

...R