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
> I tried to execute following client server program.
> [ ... ]
[quoted text clipped - 15 lines]
> }
> }
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.

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Lew - 25 Aug 2007 19:45 GMT
darker side wrote:
>> public class cli
Nearly universal convention in Java is to name classes with an initial
upper-case letter.
<http://java.sun.com/docs/codeconv/index.html>
>> {
>> public static void main(String args[]) throws IOException
[quoted text clipped - 6 lines]
>> DataOutputStream(cli.getOutputStream());
>> String inp=dis.readLine();
Are you familiar with the API docs?
<http://java.sun.com/javase/6/docs/api/java/io/DataInputStream.html#readLine()>
Did you notice the boldface warning?
>> 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...
Hint: dos.something()?
> In addition, you should explicitly close the Socket when you are
> finished using it.
darker side wrote:
>> 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?).
...
> 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/>

Signature
Lew
>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

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com