Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-28-2008, 02:49 PM
Member
 
Join Date: Jul 2008
Posts: 3
jim478 is on a distinguished road
[SOLVED] simple server chat program, using InputStream and OutputStream. NullPointerE
Hello, I am new to java, and I am trying this simple server client program using InputStream and OutputStream.
However when I try to run it, i get this NullPointerException on the client-side.

Here is my code:

Server:
Code:
package demoClientServer; import java.io.*; import java.net.*; class server1 { public static void main(String args[]) { String data = "Toobie ornaught toobie"; try { ServerSocket srvr = new ServerSocket(1234); Socket skt = srvr.accept(); System.out.print("Server has connected!\n"); OutputStream out = skt.getOutputStream(); byte[] bt = data.toByteArray(); System.out.print("Sending string: '" + bt + "'\n"); out.write(bt); out.close(); skt.close(); srvr.close(); } catch(Exception e) { System.out.print("Whoops! It didn't work!\n"); } } }
Client:
Code:
package demoClientServer; import java.io.*; import java.net.*; class client1 { public static void main(String args[]) { try { Socket skt = new Socket("localhost", 1234); InputStream in = skt.getInputStream(); System.out.print("Received string: '"); byte[] bt = null; //in.read(bt); while (in.read(bt) > 0) { System.out.println(bt.toString()); // Read one line and output it } System.out.print("'\n"); in.close(); } catch(Exception e) { e.printStackTrace(); System.out.print("Whoops! It didn't work!\n"); } } }
Being new to Java, I may be missing something,

Thanks in advance,

Last edited by jim478 : 07-28-2008 at 04:32 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-28-2008, 05:27 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Please post the full text of the error message.
The error message shows the soruce file line number where the error occured. You could look at that line and see what object is referenced there and perhaps see a reason for it being null.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-28-2008, 09:32 PM
Member
 
Join Date: Jul 2008
Posts: 3
jim478 is on a distinguished road
Hello Norm,

Thanks for replying, I dug a bit more into the error and now its fine.

The following statement was causing it:
Quote:
byte[] bt = null;
Now its fine, thanks again!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Simple Web Server Java Tip java.net 0 04-07-2008 10:04 PM
Converting InputStream to OutputStream Java Tip Java Tips 1 01-12-2008 12:13 AM
Simple serverless chat solution goodjonx Networking 3 01-07-2008 05:25 PM
help with simple program in java katie New To Java 2 08-06-2007 10:03 PM
Java Program chat susan Advanced Java 1 07-25-2007 11:05 PM


All times are GMT +3. The time now is 02:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org