10 Replies - 1558 Views - Last Post: 11 December 2012 - 02:36 PM Rate Topic: -----

#1 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

thread with an instance of RemoteReader.Cant seem to get it.

Posted 10 December 2012 - 02:03 PM

I need to make a client with GUI. Here is what the teacher told us to do.
Structure of your class:
//many import statements
public class  ChatClient  {
   public class  ChatClient() {
   //Initialize the GUI components and other data.
  
   }
   
   public void launchFrame()
   {
   //make the GUI
   //eastablish the socket connection
   //make a thread and start it with an instance of RemoteReader
   //create the streams from the socket connection
   //make gui visible
   }
 
   public class SendListener implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
          //get info from text field
          //send it to the server
          //clear the text field
       }
    }
   
    private class RemoteReader implements Runnable
    {
       public void run()
       {
         while (true)
         {
           //do stuff here to continously read from the server using the input stream
           //put the received text into the text area
          
         }
        
       }
     }


And here is what I did. Please help?


import java.net.*;

import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;





public class BasicChatCLient extends JFrame   {
	

	private JTextArea ta;
	private JScrollPane sbrText;
	PrintWriter writer;
	 String strClientName;
  
	private JTextField tf;
	private JLabel Label;
	private JButton btnQuit,btnSend;
	Socket clientSocket = null;
	ServerSocket serverSocket=null ;
	PrintWriter out = null;
	BufferedReader in = null;
	
	
public BasicChatCLient(Socket clientSocket) {
	
	super("BasicChatCLient");
	this.clientSocket = clientSocket;


  tf = new JTextField(50);
  ta = new JTextArea(10,50);
 sbrText= new JScrollPane(ta);
 Label = new JLabel("Chat Program", JLabel.CENTER);
 btnQuit = new JButton("Quit");
 btnSend = new JButton("Send ");

}
public BasicChatCLient() {
	
	

  tf = new JTextField(50);
  ta = new JTextArea(10,50);
 sbrText= new JScrollPane(ta);
 Label = new JLabel("Chat Program", JLabel.CENTER);
 btnQuit = new JButton("Quit");
 btnSend = new JButton("Send ");

}
public void launchFrame()

{
	Thread t1 = new Thread(RemoteReader(tf));
	t1.start();

	btnSend.addActionListener(new SendButtonListener());
	btnQuit.addActionListener(new ExitButtonListener());
	this.setLayout(new BorderLayout(1,1));
	
 sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 sbrText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

Label.setFont(new Font("Serif", Font.BOLD, 18));
Label.setOpaque(true);
Label.setBackground(Color.white);
Label.setOpaque(true);


ta.setEditable(false);
tf.setBackground(Color.white);
ta.setBackground(Color.white);
JPanel p3= new JPanel();
p3.setLayout(new BorderLayout());
p3.add(Label,BorderLayout.NORTH);

JPanel p1 = new JPanel();

p1.setLayout(new BorderLayout());
p1.add(sbrText, BorderLayout.CENTER);
p1.add(tf, BorderLayout.SOUTH);





JPanel p2 = new JPanel(new GridLayout(2,1));

p2.add(btnSend,BorderLayout.NORTH);
p2.add(btnQuit,BorderLayout.SOUTH);
this.add(p1,BorderLayout.WEST);

this.add(p2, BorderLayout.EAST);
this.add(p3, BorderLayout.NORTH);
this.setTitle("Chat Client");




 

			


}	
public class ExitButtonListener implements ActionListener 
{ public void actionPerformed(ActionEvent ev) { 
	 System.exit(0); 
	 } 
} 

public class SendButtonListener implements ActionListener 
{ 
	
	
	public void actionPerformed(ActionEvent ev)
{ 
	
	
	try { 
	 writer.println(tf.getText());
	 writer.flush(); } catch(Exception ex) 
	 { ex.printStackTrace(); } tf.setText("");
	 tf.requestFocus();  }
}


private class RemoteReader implements Runnable
{
   public void run()
   {
	   try {
           InputStream is = clientSocket.getInputStream();
           ObjectInputStream ois = new ObjectInputStream(is);
           while (true) {
               String strMsg = (String) ois.readObject();
               ta.append("\n" + strClientName + ":" + strMsg);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
  

}


public static void main (String []args) throws IOException
{		
	

	Socket kkSocket = null;
	PrintWriter out = null;
	BufferedReader in = null;

	try {
		kkSocket = new Socket("127.0.0.1", 4444);
		out = new PrintWriter(kkSocket.getOutputStream(), true);
		in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
	} catch (UnknownHostException e) {
		System.err.println("Could not connect to 127.0.0.1.");
		System.exit(1);
	} catch (IOException e) {
		System.err.println("Couldn't get I/O for the connection to 127.0.0.1.");
		System.exit(1);
	}

	
BasicChatCLient gui = new BasicChatCLient();

gui.launchFrame();
gui.setVisible(true);
gui.pack();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

}



Is This A Good Question/Topic? 0
  • +

Replies To: thread with an instance of RemoteReader.Cant seem to get it.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2832
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 02:40 AM

Quote

And here is what I did. Please help?
With what problem?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 01:22 PM

It is a good start. However I don't see how a ChatClient constructor may received a Socket as parameter to its constructor.

A ChatClient GUI should start a GUI, then it can have a "Connect" button whicht will allow it to connect to a Server. THEN the server will return a Socket to it. SO you can't create the Client with the Socket unless you establish the connection to the server in the main() method (without using any GUI interface) and when the connection with the Server is established you pass the socket as parameter to the ClientGUI constructor.
Was This Post Helpful? 0
  • +
  • -

#4 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 01:35 PM

View Postpbl, on 11 December 2012 - 01:22 PM, said:

It is a good start. However I don't see how a ChatClient constructor may received a Socket as parameter to its constructor.

A ChatClient GUI should start a GUI, then it can have a "Connect" button whicht will allow it to connect to a Server. THEN the server will return a Socket to it. SO you can't create the Client with the Socket unless you establish the connection to the server in the main() method (without using any GUI interface) and when the connection with the Server is established you pass the socket as parameter to the ClientGUI constructor.


I re did it a bit. Now the server actually interacts with the client but I get the "Message not sent error". Now this I cant seem to figure out. D:
    import java.net.*;

    import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;





    public class BasicChatCLient extends JFrame   {
       

        private JTextArea ta;
        private JScrollPane sbrText;
        PrintWriter writer;
       
     
        private JTextField tf;
        private JLabel Label;
        private JButton btnQuit,btnSend;
        Socket clientSocket = null;
       

        PrintWriter out = null;
        BufferedReader in = null;
       
       
    public BasicChatCLient() {
       
        super("BasicChatCLient");
        


      tf = new JTextField(50);
      ta = new JTextArea(10,50);
     sbrText= new JScrollPane(ta);
     Label = new JLabel("Chat Program", JLabel.CENTER);
     btnQuit = new JButton("Quit");
     btnSend = new JButton("Send ");

    }
   
    public void launchFrame()

    {
       
    /* launchFrame is supposed to connect to the chat server.

looks like this: try { create client socket, create bufferedreader, create printstream }

it's also supposed to start a thread. looks like this:
    RemoteReader reader1 = new RemoteReader();

        (new Thread(reader1)).start();

*/try
{  clientSocket = new Socket("127.0.0.1", 2000);
InputStreamReader streamreader = new InputStreamReader(clientSocket.getInputStream());
in = new BufferedReader(streamreader);
out = new PrintWriter(clientSocket.getOutputStream());
	
}catch (Exception ex) {
  ta.append("Cannot Connect! Try Again. \n");
  
}
    	
    	Thread RemoteReader = new Thread(new RemoteReader());
       RemoteReader.start();
    	

        btnSend.addActionListener(new SendButtonListener());
        btnQuit.addActionListener(new ExitButtonListener());
        this.setLayout(new BorderLayout(1,1));
       
     sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
     sbrText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    Label.setFont(new Font("Serif", Font.BOLD, 18));
    Label.setOpaque(true);
    Label.setBackground(Color.white);
    Label.setOpaque(true);


    ta.setEditable(false);
    tf.setBackground(Color.white);
    ta.setBackground(Color.white);
    JPanel p3= new JPanel();
    p3.setLayout(new BorderLayout());
    p3.add(Label,BorderLayout.NORTH);

    JPanel p1 = new JPanel();

    p1.setLayout(new BorderLayout());
    p1.add(sbrText, BorderLayout.CENTER);
    p1.add(tf, BorderLayout.SOUTH);





    JPanel p2 = new JPanel(new GridLayout(2,1));

    p2.add(btnSend,BorderLayout.NORTH);
    p2.add(btnQuit,BorderLayout.SOUTH);
    this.add(p1,BorderLayout.WEST);

    this.add(p2, BorderLayout.EAST);
    this.add(p3, BorderLayout.NORTH);
    this.setTitle("Chat Client");




     

               


    }   
    public class ExitButtonListener implements ActionListener
    { public void actionPerformed(ActionEvent ev) {
         System.exit(0);
         }
    }

    public class SendButtonListener implements ActionListener
    {
       
    	  public void actionPerformed ( ActionEvent event )

          {
    		  if ((tf.getText()).equals("")) {
    	            tf.setText("");
    	            tf.requestFocus();
    	        }
    		  else{
    			  try
    			  {
    				  writer.println(tf.getText());
    	               writer.flush(); 
    				  
    			  } 
    			  catch (Exception ex) {
    	                ta.append("\nMessage was not sent. \n");
    	            }
    			  tf.setText("");
    	            tf.requestFocus();
    		  }
    			  
    		  
    		  
                       
          }
 
       
    
       
    }
        
    
    

    private class RemoteReader implements Runnable
    {
    	public void run()
    	   {
    		
    	       try {
    	          
    	        
    	    	   while (true) {
    	               String stream = (String) in.readLine();
    	               ta.append("\n"+ stream);
    	           }
    	       } catch (Exception e) {
    	           e.printStackTrace();
    	       }
    	   }
    	  
          
        	
        	   }
           
    
    
    public static void main (String []args) throws IOException
    {       
       

       

     
       
    BasicChatCLient gui = new BasicChatCLient();

    gui.launchFrame();
    gui.setVisible(true);
    gui.pack();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }

    }


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 01:50 PM

First replace
ta.append("\nMessage was not sent. \n");
by
ta.append("\nMessage was not sent. Error: " + ex + "\n");


Then are You sure the server is still running ?
post your Server class
Was This Post Helpful? 0
  • +
  • -

#6 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 01:57 PM

It doesnt permit me to upload the class file and connection file. I only have the class files. But here is what it says:Connection made from Host:local Host. Everybody say Hello! Welcome null the passphrase is Kobe. Message was not sent.error.java.lang.NullPointerException.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 02:09 PM

nul pointer exception !!!!
You see why you should always print (and stack trace) the Exception

Obviously here or your JTextField or the writer are null
I would bet on the writer :)
Was This Post Helpful? 0
  • +
  • -

#8 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 02:17 PM

View Postpbl, on 11 December 2012 - 02:09 PM, said:

nul pointer exception !!!!
You see why you should always print (and stack trace) the Exception

Obviously here or your JTextField or the writer are null
I would bet on the writer :)/>

Nope still no dice. I got rid of all the nulls and same thing.

  import java.net.*;

    import java.io.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;





    public class BasicChatCLient extends JFrame   {
       

        private JTextArea ta;
        private JScrollPane sbrText;
        PrintWriter writer;
       
     
        private JTextField tf;
        private JLabel Label;
        private JButton btnQuit,btnSend;
        Socket clientSocket ;
       

        PrintWriter out ;
        BufferedReader in ;
       
       
    public BasicChatCLient() {
       
        super("BasicChatCLient");
        


      tf = new JTextField(50);
      ta = new JTextArea(10,50);
     sbrText= new JScrollPane(ta);
     Label = new JLabel("Chat Program", JLabel.CENTER);
     btnQuit = new JButton("Quit");
     btnSend = new JButton("Send ");

    }
   
    public void launchFrame()

    {
       
    /* launchFrame is supposed to connect to the chat server.

looks like this: try { create client socket, create bufferedreader, create printstream }

it's also supposed to start a thread. looks like this:
    RemoteReader reader1 = new RemoteReader();

        (new Thread(reader1)).start();

*/try
{  clientSocket = new Socket("127.0.0.1", 2000);
InputStreamReader streamreader = new InputStreamReader(clientSocket.getInputStream());
in = new BufferedReader(streamreader);
out = new PrintWriter(clientSocket.getOutputStream());
	
}catch (Exception ex) {
  ta.append("Cannot Connect! Try Again. \n");
  
}
    	
    	Thread RemoteReader = new Thread(new RemoteReader());
       RemoteReader.start();
    	

        btnSend.addActionListener(new SendButtonListener());
        btnQuit.addActionListener(new ExitButtonListener());
        this.setLayout(new BorderLayout(1,1));
       
     sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
     sbrText.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    Label.setFont(new Font("Serif", Font.BOLD, 18));
    Label.setOpaque(true);
    Label.setBackground(Color.white);
    Label.setOpaque(true);


    ta.setEditable(false);
    tf.setBackground(Color.white);
    ta.setBackground(Color.white);
    JPanel p3= new JPanel();
    p3.setLayout(new BorderLayout());
    p3.add(Label,BorderLayout.NORTH);

    JPanel p1 = new JPanel();

    p1.setLayout(new BorderLayout());
    p1.add(sbrText, BorderLayout.CENTER);
    p1.add(tf, BorderLayout.SOUTH);





    JPanel p2 = new JPanel(new GridLayout(2,1));

    p2.add(btnSend,BorderLayout.NORTH);
    p2.add(btnQuit,BorderLayout.SOUTH);
    this.add(p1,BorderLayout.WEST);

    this.add(p2, BorderLayout.EAST);
    this.add(p3, BorderLayout.NORTH);
    this.setTitle("Chat Client");




     

               


    }   
    public class ExitButtonListener implements ActionListener
    { public void actionPerformed(ActionEvent ev) {
         System.exit(0);
         }
    }

    public class SendButtonListener implements ActionListener
    {
       
    	  public void actionPerformed ( ActionEvent event )

          {
    		  if ((tf.getText()).equals("")) {
    	            tf.setText("");
    	            tf.requestFocus();
    	        }
    		  else{
    			  try
    			  {
    				  writer.println(tf.getText());
    	               writer.flush(); 
    				  
    			  } 
    			  catch (Exception ex) {
    	                ta.append("\nMessage was not sent. \n");
    	            }
    			  tf.setText("");
    	            tf.requestFocus();
    		  }
    			  
    		  
    		  
                       
          }
 
       
    
       
    }
        
    
    

    private class RemoteReader implements Runnable
    {
    	public void run()
    	   {
    		
    	       try {
    	          
    	        
    	    	   while (true) {
    	               String stream = (String) in.readLine();
    	               ta.append("\n"+ stream);
    	           }
    	       } catch (Exception e) {
    	           e.printStackTrace();
    	       }
    	   }
    	  
          
        	
        	   }
           
    
    
    public static void main (String []args) throws IOException
    {       
       

       

     
       
    BasicChatCLient gui = new BasicChatCLient();

    gui.launchFrame();
    gui.setVisible(true);
    gui.pack();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }

    }


Was This Post Helpful? 0
  • +
  • -

#9 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 02:27 PM

I can email you the server and what not. Send me an email and I will replie with the server. I think it would help you help me if you had the server. Danielkostinskiy@gmail.com
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,912
  • Joined: 06-March 08

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 02:30 PM

What do you mean "same thing" ???
If you get ride of all your null pointer exception it would crash on something else. How do you know on what it crashed ?

There was an Exception telling you that you were having an Null Pointer Exception.
It is not by hidding it (removing it from the print statement) that you will fix your problem !!!

I am sure You still crash on a null pointer exception, you just don't display that fact anymore. and what is null in the writer.. where did you do writer = new PrintWriter(... ???

So put back the ex in your catch() and add a
ex.printStackTrace() while you are at it.

This post has been edited by pbl: 11 December 2012 - 02:37 PM

Was This Post Helpful? 0
  • +
  • -

#11 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

Re: thread with an instance of RemoteReader.Cant seem to get it.

Posted 11 December 2012 - 02:36 PM

OMG THANK YOU!!!!!!!!!!!!

It was the writer thing... I named the writer as
out = new PrintWriter(clientSocket.getOutputStream()); 

But then ... I used
writer.println(tf.getText());
    	              writer.flush();  


Thank you so much :D. IT WORKSSS. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1