Results 1 to 3 of 3
  1. #1
    jim478 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default [SOLVED] simple server chat program, using InputStream and OutputStream. NullPointerE

    Hello, I am new to java, and I am trying this simple server client program using InputStream and OutputStream.
    However when I try to run it, i get this NullPointerException on the client-side.

    Here is my code:

    Server:
    Java Code:
    package demoClientServer;
    
    import java.io.*;
    import java.net.*;
    
    class server1 {
       public static void main(String args[]) {
          String data = "Toobie ornaught toobie";
          try {
             ServerSocket srvr = new ServerSocket(1234);
             Socket skt = srvr.accept();
             System.out.print("Server has connected!\n");
             OutputStream out = skt.getOutputStream();
             byte[] bt = data.toByteArray();
             System.out.print("Sending string: '" + bt + "'\n");
             out.write(bt);
             out.close();
             skt.close();
             srvr.close();
          }
          catch(Exception e) {
             System.out.print("Whoops! It didn't work!\n");
          }
       }
    }
    Client:
    Java Code:
    package demoClientServer;
    
    import java.io.*;
    import java.net.*;
    
    class client1 {
       public static void main(String args[]) {
          try {
             Socket skt = new Socket("localhost", 1234);
             InputStream in = skt.getInputStream();
             System.out.print("Received string: '");
             byte[] bt = null;
             //in.read(bt);
             while (in.read(bt) > 0) {
             System.out.println(bt.toString()); // Read one line and output it
             }
             System.out.print("'\n");
             in.close();
          }
          catch(Exception e) {
              e.printStackTrace();
             System.out.print("Whoops! It didn't work!\n");
          }
       }
    }
    Being new to Java, I may be missing something,

    Thanks in advance,
    Last edited by jim478; 07-28-2008 at 02:32 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Please post the full text of the error message.
    The error message shows the soruce file line number where the error occured. You could look at that line and see what object is referenced there and perhaps see a reason for it being null.

  3. #3
    jim478 is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Hello Norm,

    Thanks for replying, I dug a bit more into the error and now its fine.

    The following statement was causing it:
    byte[] bt = null;
    Now its fine, thanks again!

Similar Threads

  1. A Simple Web Server
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:04 PM
  2. Converting InputStream to OutputStream
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-11-2008, 10:13 PM
  3. Simple serverless chat solution
    By goodjonx in forum Networking
    Replies: 3
    Last Post: 01-07-2008, 03:25 PM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. Java Program chat
    By susan in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 09:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •