I'm trying to send a variable to another java application every 5 seconds, but I'm getting "Exception in thread "Timer-0" java.lang.NullPointerException" and I can't figure out which part is messing up.
Timer/main:
SendCommunication sendComm = new SendCommunication();
sendComm.init("localhost", 8081);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override public void run() {
sendComm.packData();
sendComm.sendPackage();
}
}, 0L, 5000L);
packData:
public void packData() {
DatabaseOperation dbOperation = DatabaseOperation.getInstance();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
String tempGroupDist = Integer.toString(dbOperation.getGroupDistance());
try {
outputStream.write(tempGroupDist.getBytes());
this.sendData = outputStream.toByteArray();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
sendPackage:
public void sendPackage() {
try {
System.out.println("Sending data to " + this.sendData.length + " bytes to server.");
DatagramPacket sendPacket = new DatagramPacket(this.sendData, this.sendData.length, this.ipAddress, this.serverPort);
this.clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(this.receiveData, this.receiveData.length);
System.out.println ("Waiting for return packet");
this.clientSocket.setSoTimeout(10000);
try {
this.clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
InetAddress returnIPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
System.out.println ("From server at: " + returnIPAddress + ":" + port);
System.out.println("Message: " + modifiedSentence);
}
catch (SocketTimeoutException ste) {
System.out.println ("Timeout Occurred: Packet assumed lost");
}
this.clientSocket.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
getGroupDistance:
public int getGroupDistance() {
try {
sqd = con.prepareStatement("SELECT SUM(DISTANCE) FROM Patients");
rs = sqd.executeQuery();
if (rs.next()) {
int sum = rs.getInt(1);
if (groupDistance == 0) {
this.groupDistance = sum;
} else {
this.groupDistance = this.groupDistance + sum;
}
sqd.close(); // lukker statemenet
stmt.close();
rs.close(); // lukker resultSet
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM Patients";
rs = stmt.executeQuery(sql);
rs.next();
}
} catch (SQLException bananFisk) {
bananFisk.printStackTrace();
}
return groupDistance;
}
My confusion stem from the following: packData + sendPackage work on their own. getGroupDistance works on its own. I'm using an identical timer somewhere else in the code and that works fine.
This combination however doesn't work and I can't figure out why..
EDIT: I fixed it even though I still don't know what the problem was. But if I move the timer to the initialization it runs fine and I get the expected result. The link to the NullPointerException might hold the answer to why it didn't work before but I couldn't seem to find the error, seeing as all elements worked on their own.