Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

please help to debug error

Thread view: 
darker side - 25 Aug 2007 19:05 GMT
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
Joshua Cranmer - 25 Aug 2007 19:19 GMT
> 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

Roedy Green - 26 Aug 2007 10:56 GMT
>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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.