• Post Reply Bookmark Topic Watch Topic
  • New Topic

sockets throw null pointer exception

 
sunil choudhary
Ranch Hand
Posts: 144
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HOw can the code at the javasoft website can be wrong?
I tried t manipulate teh sockets and a bit of coding but its no use.
THe server preogram runs fine.When I try to run the Client program then the browser opens a dial up screen and asks for connection.Well the problem arises after this when I try to send the message then there is a Null Pointer exception at the console.
I hae tried to give 127.0.0.0 as socket name for the local loopback but itis not working.
Please help me find out what can be wrong?

the programs are up at sun site.
Essentials of the JavaTM Programming Language, Part 2
Lesson 1: Socket Communications
for server : http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketServer.java
for client http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava2/Code/SocketClient.java
thnaks for help
sunil

------------------
 
sunil choudhary
Ranch Hand
Posts: 144
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arr...Well.....
Sorry to write again I resolved erlier problem myself(actually computer name has to be changed to your local computer ame by using getLocalHost and getHostName methods.
However.. a new thing has come up .
the client hangs after teh send buton is presssed after typiung a message..
and also no activity is seen in hte server window
Can you help me with this one instead......

------------------
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunil,
Why you want to use getLocalHost and getHostName methods?
Thanks
Angela
 
sunil choudhary
Ranch Hand
Posts: 144
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why you want to use getLocalHost and getHostName methods?

This is to get the name of my own computer at which I am running it .other wise the computer tried to look the computer at teh net.
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunil,
Thanks for ur response.
As you know I am learning all concepts of Client-Server communication.I saw the code from the link you had above. I don't know in which file & how I have to implement getLocalHost and getInetAddress?
Here is Client program:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

class SocketClient extends JFrame implements ActionListener {
JLabel text, clicked;
JButton button;
JPanel panel;
JTextField textField;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
SocketClient(){
//Begin Constructor
text = new JLabel("Text to send over socket:");
textField = new JTextField(20);
button = new JButton("Click Me");
button.addActionListener(this);
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
getContentPane().add(panel);
panel.add("North", text);
panel.add("Center", textField);
panel.add("South", button);
} //End Constructor
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if(source == button){
//Send data over socket
String text = textField.getText();
out.println(text);
textField.setText(new String(""));
//Receive text from server
try{
String line = in.readLine();
System.out.println("Text received :" + line);
}
catch (IOException e)
{
System.out.println("Read failed");
System.exit(1);
}
}
}
public void listenSocket(){
//Create socket connection
try{
socket = new Socket("10.3.158.154", 10001);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println("Unknown host: 10.3.158.154");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}

public static void main(String[] args){
SocketClient frame = new SocketClient();
frame.setTitle("Client Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}
Here is server program:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

class SocketServer extends JFrame implements ActionListener {
JButton button;
JLabel label = new JLabel("Text received over socket:");
JPanel panel;
JTextArea textArea = new JTextArea();
ServerSocket server = null;
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
String line;
SocketServer(){
//Begin Constructor
button = new JButton("Click Me");
button.addActionListener(this);
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
getContentPane().add(panel);
panel.add("North", label);
panel.add("Center", textArea);
panel.add("South", button);
} //End Constructor
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if(source == button){
textArea.setText(line);
}
}
public void listenSocket(){
try
{
server = new ServerSocket(10001);
}
catch (IOException e)
{
System.out.println("Could not listen on port 10001");
System.exit(-1);
}
try
{
client = server.accept();
}
catch (IOException e)
{
System.out.println("Accept failed: 10001");
System.exit(-1);
}
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
}
catch (IOException e)
{
System.out.println("Accept failed: 10001");
System.exit(-1);
}
while(true)
{
try{
line = in.readLine();
//Send data back to client
out.println(line);
}
catch (IOException e)
{
System.out.println("Read failed");
System.exit(-1);
}
}
}
protected void finalize(){
//Clean up
try{
in.close();
out.close();
server.close();
}
catch (IOException e)
{
System.out.println("Could not close.");
System.exit(-1);
}
}
public static void main(String[] args){
SocketServer frame = new SocketServer();
frame.setTitle("Server Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}

Would you please add these getLocalHost and getInetAddress at proper place in my code? My server IP address is 10.3.158.154 and port number is 10001.That way I can totally understand the concept. And also I want to clarify that I have to run Client class file from Client machine and Server class file in Server machine? May I right? If so, How I can check that server listen to client and also give response back to client.
I would really appreciate your help,
If you please let me know ASAP
Thanks a lot in advance,
Angela
 
sunil choudhary
Ranch Hand
Posts: 144
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela
Add your IP addres to place where I have written suresh.As far
as port no is concerned you specify it at run time.
BTW please reply me directly at my mail Id for which books and resources you are using.
I would also like to know about you if it is comfortable to you .
Sunil
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunil,
I have these following confusion?
(1)What getLocalHost and getInetAddress?
(2)where i can put values of these in my code?
Do u have any messanger? I am using hotmail. U want to communicate through messanger? If yes, let me know timing schedule
Thanks in advance,
angela
Originally posted by sunil choudhary:
Hi Angela
Add your IP addres to place where I have written suresh.As far
as port no is concerned you specify it at run time.
BTW please reply me directly at my mail Id for which books and resources you are using.
I would also like to know about you if it is comfortable to you .
Sunil

 
sunil choudhary
Ranch Hand
Posts: 144
Java Redhat Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry No messenger.
sunil
Do u have any messanger? I am using hotmail. U want to communicate through messanger? If yes, let me know timing schedule
[/B]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!