My whole source code is supposed to be a multi-chat created in Java that uses RSA to keep the messages encrypted until the clients receive their message on their side it gets decrypted.
The problem is that I am getting an error in my Thread block. The reason why I am getting an error is because my RSA object named "secret" is null (keep in mind secret is a public object). Which is weird because I initiated my secret object when the user decides to send the message.
Here is my code when I initiate my RSA object called secret:
`
public void sendMessage(){
try{
bufferedWriter.write(username); //whatever we send here the client handle
bufferedWriter.newLine();
bufferedWriter.flush();
Scanner scanner = new Scanner(System.in);
while(socket.isConnected()){
String messageToSend = scanner.nextLine(); // this reads the user text
//insert RSA here and huffman
this.secert = new RSA(messageToSend); //this encrypts the message
//bufferedWriter.write(username + ": " + messageToSend);
bufferedWriter.write(username + ": " + secert.getEncrpytMessageString());
bufferedWriter.newLine();
bufferedWriter.flush();
}
}catch(IOException e){
closeEverything(socket, bufferedReader, bufferedWriter);
}
}
`
Here is my code when I decrypt the RSA code which gives me the error. I wrote a comment so it is easy to spot.
`
public void listenForMessage(){
new Thread(new Runnable() {
@Override
public void run() {
String msgFromGroupChat;
try{
while(socket.isConnected()){
//msgFromGroupChat = bufferedReader.readLine();
msgFromGroupChat = secert.decrypt(bufferedReader.readLine()); //reads broadcast message ERROR HERE
//once it reaches the client it should decrypt
//msgFromGroupChat = secert.decrypt(msgFromGroupChat);
System.out.println(msgFromGroupChat);
}
}
catch(IOException e){
closeEverything(socket,bufferedReader,bufferedWriter);
}
}
}).start();
}
`
This is the error message I received:
Exception in thread "Thread-0" java.lang.NullPointerException at Client$1.run(Client.java:78) at java.base/java.lang.Thread.run(Thread.java:834)
I expected that my message would be decrypted and it would be able to print out into the console of the other user that is in this local server.
One thing I is tried initializing my RSA object named secret out of the while(socket.isConnected()) block in the sendMessage method. This did not help because I ended up getting the same error message. So, I think there is an issue in the thread block in the method called listenForMessage.