please help to debug error

D

darker side

I tried to execute following client server program.
server program
import java.net.*;
import java.io.*;
public class serv
{
public static void main(String args[])throws IOException
{

ServerSocket ser=new ServerSocket(1233);
Socket cli;
System.out.println("waiting for connection...........");
cli=ser.accept();
System.out.println("connected");
DataInputStream fromsock=new DataInputStream(cli.getInputStream());
String file=new String (fromsock.readLine( ));
DataOutputStream tosock=new
DataOutputStream(cli.getOutputStream());
System.out.println("file from client" +fromsock.readLine());
fromsock.close();
tosock.close();

}
}
client
import java.net.*;
import java.io.*;
public class cli
{
public static void main(String args[]) throws IOException

{
Socket cli=new Socket(InetAddress.getByName("localhost"),1233);
System.out.println("connected by client");
DataInputStream dis=new DataInputStream(System.in);
DataOutputStream dos=new DataOutputStream(cli.getOutputStream());
String inp=dis.readLine();
dis.close();
dos.close();
}
}
it shows exceptions:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:141)
at serv.main(serv.java:14)

please help me to solve this problem and tell me reason why it happens
 
J

Joshua Cranmer

darker said:
I tried to execute following client server program.
> [ ... ]
client
import java.net.*;
import java.io.*;
public class cli
{
public static void main(String args[]) throws IOException

{
Socket cli=new Socket(InetAddress.getByName("localhost"),1233);
System.out.println("connected by client");
DataInputStream dis=new DataInputStream(System.in);
DataOutputStream dos=new DataOutputStream(cli.getOutputStream());
String inp=dis.readLine();
dis.close();
dos.close();
}
}

What do you think this method is doing? Look at it carefully, since
there is something that most people would logically expect it to do but
it doesn't do...

In addition, you should explicitly close the Socket when you are
finished using it.
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:141)

There is only one reason why the constructor of String would throw a
NullPointerException: the String being passed in is null. Now, ask
yourself why the input from the socket is null (hint: look at your
client class. What isn't it doing?).
please help me to solve this problem and tell me reason why it happens

Some other points-of-order:
@ Don't use tab's in Usenet posts. It screws up formatting.
@ Use proper English grammar, including capitalization and punctuation.
@ It generally helps to go through the Java APIs if you need help.
 
L

Lew

Nearly universal convention in Java is to name classes with an initial
upper-case letter.
{
public static void main(String args[]) throws IOException

{
Socket cli=new Socket(InetAddress.getByName("localhost"),1233);
System.out.println("connected by client");
DataInputStream dis=new DataInputStream(System.in);
DataOutputStream dos=new
DataOutputStream(cli.getOutputStream());
String inp=dis.readLine();

Are you familiar with the API docs?

Joshua said:
What do you think this method is doing? Look at it carefully, since
there is something that most people would logically expect it to do but
it doesn't do...

Hint: dos.something()?

Joshua said:
In addition, you should explicitly close the Socket when you are
finished using it.

Joshua said:
There is only one reason why the constructor of String would throw a
NullPointerException: the String being passed in is null. Now, ask
yourself why the input from the socket is null (hint: look at your
client class. What isn't it doing?). ....
Some other points-of-order:
@ Don't use tab's in Usenet posts. It screws up formatting.
@ Use proper English grammar, including capitalization and punctuation.
@ It generally helps to go through the Java APIs if you need help.

I was echoing Joshua's point, specifically about the readLine() method. The
general URL for the API docs is
<http://java.sun.com/javase/6/docs/api/>
 
R

Roedy Green

it shows exceptions:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:141)
at serv.main(serv.java:14)

Could you please indicate line 14 in serv.java
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top