0

I just started tinkering with java and now am trying to send a JSON object over the socket. I tried to implement Serializable but it did not work so I've put that to hold. Now I m just trying to send a JSON object in the form of a string.

JSONObject jsontype = new JSONObject;
String stringJSON = jsontype.toString();
    return stringJSON;

this.clientOut = socket.getOutputStream();
this.clientOut.write((tosend+"\n").getBytes()); // had to add \n 
//otherwise it doesn't work 

I have tested the receiving side of it by sending a plain string with \n and it works! but if i try to send a json object it throws a nullpointerexception error. My json object is not null as I have tested it. Is there any other way to convert a json into a regular string? I am using the org.json library.

sakib11
  • 496
  • 1
  • 5
  • 20
  • The code you posted wouldn't compile. And even if it compiled, it would not be the cause of the NullPointerException you're seeing. And you haven't posted the stack trace of the exception either. So, without the code causeing the exception, and without its stack trace, there is absolutely no way we can possibly help you. – JB Nizet Mar 09 '19 at 11:29
  • this is just a fraction of the code – sakib11 Mar 09 '19 at 15:04
  • anyway thanks for your reply. I have solved it. – sakib11 Mar 09 '19 at 15:11

0 Answers0