java - Serialized object has null values after sending via udp -
i have set udp client/server model can send string messages each other.
i've created custom class loginrequest serialized , sent server containing username , password. when getters these variables called return null values though check variables before loginrequest sent.
here code serializing , sending datagram client:
private void login(string name, char[] pass) throws sqlexception { try { loginrequest login = new loginrequest(name, pass); bytearrayoutputstream baos = new bytearrayoutputstream(); objectoutputstream oos = new objectoutputstream(baos); oos.writeobject(login); byte[] buffer= baos.tobytearray(); oos.close(); baos.close(); datagrampacket packet = new datagrampacket(buffer, buffer.length, inetaddress.getbyname(server), 10110); datagramsocket ssocket = new datagramsocket(); ssocket.send(packet); login.getusername(); } catch (exception e) { e.printstacktrace(); } }
here code de-serializing loginrequest , reading username:
private void readmessage() { try { bytearrayinputstream bais = new bytearrayinputstream(cpacket.getdata()); objectinputstream ois = new objectinputstream(bais); object obj = ois.readobject(); bais.close(); ois.close(); if(obj instanceof loginrequest) { system.out.println("login request"); loginrequest login = (loginrequest) obj; login.getusername(); } else { system.out.println("not login request"); } } catch (exception e) { e.printstacktrace(); } }
here code loginrequest:
public class loginrequest implements serializable { /** * */ private static final long serialversionuid = -1379800611469143228l; private static string username; private static char[] password; public loginrequest(string name, char[] pass) { username = name; password = pass; } public string getusername() { system.out.println("username: " + username); return username; } public char[] getpassword() { string p = password.tostring(); system.out.println("password: " + p); return password; } }
when attempt read username or password after deserialization, nullpointerexception. extremely happy if can point me in right direction.
drop static
on attribute usename , password!
Comments
Post a Comment