DataInputStream


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: DataInputStream

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    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!

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    the only thing i can see from your code that may cause a null pointer is the line:

    s.getInputStream()

    is "s" initialised?
    A kram a day keeps the doctor......guessing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center