I am making a server-client project and something is wrong with my client-side when I am trying to send JSON file. Server - side is ok and works as well. Errors in Logcat say that my problem in buffered reader. Sry for my English, am only student_).
Errors
Process: com.dontknow.myapplication231, PID: 5782
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object reference
at com.dontknow.myapplication231.User.SendDataToServer.doInBackground(SendDataToServer.java:67)
at com.dontknow.myapplication231.User.SendDataToServer.<init>(SendDataToServer.java:30)
at com.dontknow.myapplication231.User.MainActivity$1.onClick(MainActivity.java:52)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
09-09 12:00:20.960 559-1769/system_process E/EGL_emulation: tid 1769:
eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
09-09 12:00:21.694 598-598/? E/EGL_emulation: tid 598: eglCreateSyncKHR(1669):
error 0x3004 (EGL_BAD_ATTRIBUTE)
09-09 12:00:22.375 1221-1276/com.android.launcher3 E/EGL_emulation: tid 1276:
eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
09-09 12:05:20.858 559-1137/system_process E/JavaBinder: !!! FAILED BINDER
TRANSACTION !!! (parcel size = 104)
09-09 12:05:20.862 559-1137/system_process E/JavaBinder: !!! FAILED BINDER
TRANSACTION !!! (parcel size = 108)
My client-using AsyncTask(this name and password i get from editName.getText.toString
and password the same
class SendDataToServer extends AsyncTask<Void,Void,Void> {
private Socket socket;
private String name;
private String password;
private static final String debug = "debug";
private static final String info = "INFO";
protected static String server_IP = "192.168.0.101";
protected static final int server_Port = 8607;
SendDataToServer(String name, String password){
this.name = name;
this.password = password;
onPreExecute();
doInBackground();
onPreExecute();
}
@Override
protected void onPreExecute() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Log.i(debug,"Attempt to connect to server");
socket = new Socket(server_IP,server_Port);
Log.i(debug,"Connection established");
}catch (IOException e){
e.printStackTrace();
}
}
});
thread.start();
}
@Override
protected Void doInBackground(Void... voids) {
JSONObject user = new JSONObject();
try {
user.put("name", name);
user.put("name",password);
} catch (JSONException e) {
e.printStackTrace();
}
try{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(String.valueOf(user));
bw.newLine();
bw.newLine();
bw.flush();
Log.i(info,"JSON has sent");
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
br.readLine();
Log.i(info,br.readLine());
}catch (IOException e) {
Log.e(debug,"Failed");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
try {
if(socket!=null){
socket.close();
}
} catch (IOException e) {
Log.e(debug,"Failed");
}
}
}
I really want to know what is this problem and how to solve it. Thanks for help.