DataInputStream
Hi all, I am trying to create a DataOutput Stream that passes 2 variables to a socket in a different class. The socket and DataOutputStream are created below in the first class.
try {
//create socket object, pass file, counter parameters
Socket s = new Socket (server_host,server_port);
DataOutputStream data_op_stream = new DataOutputStream (s.getOutputStream());
data_op_stream.writeInt (counter);
data_op_stream.writeUTF (fileName);
data_op_stream.close (); //Close the DataOutputStream
s.close (); //Close the opened socket
}//End try
Then i want to read the sent values with a DataInputStream as below in the second class. Both compile Ok and the sockets work fine but I get a null pointer exception on the first line of the try below, starting DataInputStream...
try{
DataInputStream data_in_stream = new DataInputStream (s.getInputStream());
String fileName = data_in_stream.readUTF ();
int counter = data_in_stream.readInt ();
System.out.println(fileName);
System.out.println(counter);
}
Have I not sent the data correctly or am i not fetching it correctly??
After that i need to edit the counter and write some data to a txt file.
thanks. dh
sometimes i love it, the rest of the time i really hate it!
Bookmarks