java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object reference
Before you mark down this, I am very new to this, I have tried to search on ways to solve this before posting but I can't seem to find a solution. I hope you understand.
I get this error when i run the on change listener.
seekbarbrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekbarbrightness, int progress, boolean b) {
if(b==true) {
tblumens.setText(String.valueOf(progress) + " Lumens");
try {
socket.getOutputStream().write(String.valueOf(progress).getBytes());
} catch (IOException e) {
}
}
}
});
I run the below code in the onCreate
method.
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
check = "fail";
socket = new Socket(serverAddr, SERVERPORT);
check = "success";
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
What is the problem? Is it because my socket connection is not initialized properly? (Also, i have not got my server running yet, however, the catch would show an error won't it?)
I've tried to use a if socket.isConnected()
function then do the try and catch, but it makes no difference..
Please help! I've searched for many tutorials and arrived at these codes for the socket client connection.