0

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.

MHSFisher
  • 733
  • 1
  • 12
  • 34
Learning2code
  • 51
  • 1
  • 10
  • 1
    `socket` is null, obviously, and obviously this is because the connect failed. `Socket.isConnected()` can't possibly help, as you don't have a `Socket`. – user207421 May 05 '17 at 08:19
  • ah, is there a way to avoid the crash? Like after the connect failed maybe the phone can just let me know about connection failure through a toast and not let it crash when i move my seekbar? – Learning2code May 05 '17 at 08:41
  • It *did* let you know. There was a `ConnectException`. Or else you have a programming bug. Have you considered testing for `socket == null`? – user207421 May 05 '17 at 08:52
  • omg i did a if(socket!=null) and yes now my app stops crashing on failure to connect. Thanks man, looks like i misunderstood and kept using socket.isConnected() instead of checking if it is not null. Thank you so much. Maybe i should change the question title. – Learning2code May 05 '17 at 09:03
  • Well there's another aspect to this. The user shouldn't be able to press UI buttons unless there is a connection. You should disable the parts of the UI that require a connection until there is one. – user207421 May 05 '17 at 09:34
  • Oh yes! Come to think of it, that should be the way. I will modify my codes accordingly. Awesome! Thanks man – Learning2code May 05 '17 at 13:32

1 Answers1

-1

Did you accept the socket connection with the client?

socket.accept();

I think your socket.getOutputStream() might be null in setOnSeekBarChangeListener