Results 1 to 5 of 5
  1. #1
    vinci is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default NullPointerException with PrintStream write

    I am writting a file transfer system with java
    but it occuer NullPointerException error when i use PrintStream write function
    can anyone help....

    The system server can be multi-user, it opens thread as follow
    Java Code:
      private static int port=4443, maxConnections=0;
      // Listen for incoming connections and handle them
      public static void main(String[] args) {
        int i=0;
        try{
          ServerSocket listener = new ServerSocket(port);
          Socket server;
    
          while((i++ < maxConnections) || (maxConnections == 0)){
            // doComms connection;
            server = listener.accept();
            IOdoComms conn_c= new IOdoComms(server);
            Thread t = new Thread(conn_c);
            t.start();
          }
        } catch (IOException ioe) {
          System.out.println("IOException on socket listen: " + ioe);
          ioe.printStackTrace();
        }
      }
    }
    then the opened thread conect to the specfic client
    Java Code:
      
    class IOdoComms implements Runnable {
        private Socket server;
        private String line,input;
        private Socket client;
        private String msg;
        private OutputStream Clientout;
        private int iniated=0;
        private PrintStream clientmsg;
    
        IOdoComms(Socket server) {
          this.server=server;
        }
      
    IOdoComms() { }
    
            public void connectClient (String server,  int  port)  {
    	         try   {
    
    	             if  (server.equals( " localhost " ))  {
    	                client  =   new  Socket(InetAddress.getLocalHost(), port);
    	            }   else   {
    	                client  =   new  Socket(InetAddress.getByName(server), port);
    	            }
    	           clientmsg = new PrintStream(client.getOutputStream());
    
         System.out.println("Open connection: " + client.getLocalAddress());
    
    	        }   catch  (UnknownHostException e)  {
    	            e.printStackTrace();
    	        }   catch  (IOException e)  {
    	            e.printStackTrace();
    	        }
        }
    
         public void sendmessage (String msg)
    	 {   clientmsg.println (msg) ;
    	 }
    it throw NullPointerException for the line
    clientmsg.write(mybytearray, 0, mybytearray.length);

    these codes are copied from the net and I have try the origan code is runnable
    can anyone tell me what the problem is...?

    Java Code:
        public void run () {
    
          input="";
         System.out.println("Open connection: " + server.getLocalAddress());
          try {
            // Set input  stream to the computation
            DataInputStream in = new DataInputStream (server.getInputStream());
            IOdoComms con  =  new  IOdoComms ();
    
            while(true)
            {  	if(iniated==0)
          		  {	line = in.readLine();
    				    // Set connect to the IO
    
    					String[] array =line.split("/");
    					con.connectClient(array[0] ,  4442 );
    					//send initial object
    
    					iniated=1;
    			 }
    
                 else
    			{line = in.readLine();}
    
              System.out.println("IO got from " + server.getLocalAddress() + ":" + line);
              if(line.equals("."))
               break;
    
          // ****************file transfer ***********
    
             File myFile = new File ("obj\\1.txt");
             byte [] mybytearray  = new byte [(int)myFile.length()];
             FileInputStream fis = new FileInputStream(myFile);
             BufferedInputStream bis = new BufferedInputStream(fis);
             System.out.println("len: " + (int)myFile.length());
             bis.read(mybytearray,0,mybytearray.length);
            con.sendmessage(Integer.toString(mybytearray.length));
            System.out.println("len: " + mybytearray.length);
    
    
             clientmsg.write(mybytearray, 0, mybytearray.length);
             clientmsg.flush();
    
            }
            server.close();
          } catch (IOException ioe) {
            System.out.println("IOException on socket listen: " + ioe);
            ioe.printStackTrace();
          }
        }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    clientMsg is null.
    This is because you never call connectClient() on the same IOdoComms object that you call run() on.
    Java Code:
        public void run () {
    ... snip
          try {
            // Set input  stream to the computation
            DataInputStream in = new DataInputStream (server.getInputStream());
            IOdoComms con  =  new  IOdoComms ();  // this is a new con.
    ... snip
    
            while(true)
            {
    ... snip
    					con.connectClient(array[0] ,  4442 );  // Which you call connectClient() on
    // At this point the clientmsg in this is still null.
    ... snip
    
             clientmsg.write(mybytearray, 0, mybytearray.length);  // null clientmsg.
             clientmsg.flush();
    
            }

  3. #3
    vinci is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    oh...many thanks
    i put the line
    IOdoComms con = new IOdoComms (); // this is a new con.
    outside the try and solve the problem

    Java Code:
        public void run () {
    ... snip
            IOdoComms con  =  new  IOdoComms ();  // this is a new con.
          try {
            // Set input  stream to the computation
            DataInputStream in = new DataInputStream (server.getInputStream());
    
    ... snip

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    It does?
    How does clientmsg get set?

  5. #5
    vinci is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    i just put the line
    IOdoComms con = new IOdoComms (); // this is a new con.
    outside the try

    it seems that reference is better to do outside the try block

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2010, 10:18 AM
  2. Replies: 6
    Last Post: 05-19-2009, 12:22 PM
  3. Do uncaught errors appear in the PrintStream?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 08:56 AM
  4. scanner vs. BufferedReader? printstream vs. FileWriter?
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 01-09-2009, 01:58 AM
  5. text areas, printstream, and "\n"
    By diggitydoggz in forum New To Java
    Replies: 10
    Last Post: 12-26-2008, 04:03 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
  •