Aphrodite 0 Newbie Poster

The code below runs successfully but once run and the user enters a message into the text field named input and clicks the button named send, I receive a null pointer expression. Within the inner action listener it calls the method sendMessage() which gets the text and assigns it to the buffer writer.

The line which I have bolded, out.print() should have a command within its parameters but I'm not sure what to now use as the command should already be processed. Anyone?

public GUI() {

		// Create the conversation area
		conversation = new JTextArea();
		
		// Create a text area and button for sending messages
		JPanel p = new JPanel(new BorderLayout());
		send = new JButton("Send");

		send.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(e.getSource() == send) {
				sendMessage();
				}
				;}
			}
		);

		input = new JTextArea();
		
		//sent input text to server!
		try {
				Socket incoming = new Socket ("java.sun.com", 80);
				//set timeout period to connect
				incoming.setSoTimeout(0);

				//InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream());
				//BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
				BufferedReader din = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
				BufferedWriter dout = new BufferedWriter(new OutputStreamWriter(incoming.getOutputStream()));

				//WRITE to the server 
				PrintWriter out = new PrintWriter (dout);		//open output stream, write to server
				out.print([b]not sure what to put here[/b]);
				out.flush();
				incoming.close();
			}

		catch(IOException e) {
			System.out.println("Error: " + e);
		}	
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater( new Runnable() {
			public void run() {
				new GUI().setVisible(true);
			}
		});
	}

	// Method to send the message in the input area to the ChatServer private void sendMessage() {
		try {
			dout.writeUTF(input.getText() + "\n" );
			System.out.println("TEXT:" + input.getText());
			input.setText("");
		}
		catch (Exception e) {
	    	System.err.println("Exception:  " + e);
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.