Keep getting error and don't know how to fix...

ok, I keep getting this nullPointerException error.. and I have no clue what I've done wrong.... I believe its on the client side of it... because when I run just the client.. the try catch should make it be like "can't find host" but instead.. it just sits there.. the cursor blinking.. and Idk why.. so if you could please help me!!!!!!! heres the server and client code

import java.net.*;

import java.io.*;

publicclass SocketServerextends Thread

{

privatestatic Socket socket =null;

staticboolean forever =true;

static RTS2 rts;

publicstaticvoid main(String[] args)throws Exception

{

ServerSocket serverSocket =null;

try{

serverSocket =new ServerSocket(5454);

}catch (IOException e){

System.err.println("Could not listen on port.");

System.exit(1);

}

Socket clientSocket =null;

try{

clientSocket = serverSocket.accept();

}catch (IOException e){

System.err.println("Accept failed.");

System.exit(1);

}

while(forever)

{

try{

ObjectOutputStream oos =

new ObjectOutputStream(socket.getOutputStream());

oos.writeObject(rts.getCompany());

ObjectInputStream ois =

new ObjectInputStream(socket.getInputStream());

rts.setEnemy((Men)ois.readObject());

socket.close();

}catch (Exception e){

System.out.println(e);

e.printStackTrace();

}

}

}

}

import java.net.*;

import java.io.*;

publicclass SocketClient

{

staticboolean forever=true;

static RTS2 rts;

static Socket socket =null;

static ObjectInputStream ois =null;

static ObjectOutputStream oos =null;

publicstaticvoid main(String[] args)throws Exception

{

try{

socket =new Socket("josh", 5455);

ois =new ObjectInputStream(socket.getInputStream());

oos =new ObjectOutputStream(socket.getOutputStream());

}catch (UnknownHostException e){

System.err.println("Don't know about host");

System.exit(1);

}catch (IOException e){

System.err.println("Couldn't get I/O for the connection");

System.exit(1);

}

while(forever)

{

rts.setEnemy((Men)ois.readObject());

System.out.println("hello");

oos.writeObject(rts.getCompany());

oos.flush();

}

}

}

[5135 byte] By [JackBoba] at [2007-10-3 5:22:43]
# 1

Looks like you never create a RTS2 instance.

So your rts variable is null.

So a NullPointerException is thrown when you try to call any of their methods (e.g. setEnemy())

(Note that the exception's stack trace would have pointed you to the line of code where the exception is thrown, making it easier to debug)

TimTheEnchantora at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 2

ok so that was a silly mistake I made... yet it didn't fix the problem... whenever I run just the client, shouldn't it not find a host and not just sit there blinking? but when I do run the server then run the client I get this error from the server side

java.lang.NullPointerException

at SocketServer.main(SocketServer.java:32)

which is when it starts the ObjectOutputStream and such... so I can only assume the problems on the client side, I bet its something easy too that i'm missing.. lol

JackBoba at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 3
Yep. You're calling socket.getOutputStream(), but you've never set socket to anything other than null.
L.A.Reeda at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 4
yeah I have.... i set the socket here in the try catch socket = new Socket("josh", 5455); then I use sock.getOutputStream() on the next line....
JackBoba at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 5
It's in the server. You are declaring and setting 'clientSocket' but you're using 'socket' which is null.Also none of these Sockets should be static and in the server they shouldn't be declared at class scope, as you will have one per client thread.
ejpa at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 6
ok, so I fixed the nullPointerException error.... but now... its not doing anything... they both just sit there... and they aren't sending the objects like they should be.... any reason why?
JackBoba at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 7
Yes. Create and flush the ObjectOutputStream before the ObjectInputStream at both client and server.
ejpa at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 8

yay it works now.... but the only bad thing is... it runs slow as shit.... like its bogged down with information...

so is there a way to make it go faster? I've heard somewhere about multicasting sockets or making another socket to receive and such.. any ideas?

and you've been a big help ejp.. thanks a lot

JackBoba at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 9
Why are you closing the socket in the read loop?If you want to exchange more than one object, create the ObjectXXStreams outside both loops and just do readObject ad/or writeObject inside the loops.
ejpa at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 10

I took out closing the socket and put all the stuff inside the loop... so that code up there.. is like really old lol... but its all working fine and dandy.. and I'm only sending one object as of now.. but its making it run slow... maybe because its sending to much info? or maybe because I'm running 2 versions of my game along with the server and client all on the same comp? lol

JackBoba at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 11
> create the ObjectXXStreams outside both loops still good advice
ejpa at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 12
hmm ok, but I ask a small favor... any chance you could give me an example of how to use it? just write out like 5 lines of code to show me how to use it? that'd be very helpful
JackBoba at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 13
Your existing code is OK with the change I mentioned.
ejpa at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 14
what in the world are ObjectXXStream... or is that short for ObjectOutputStream and ObjectInputStream?
JackBoba at 2007-7-14 23:29:47 > top of Java-index,Core,Core APIs...
# 15
yeah,Ejb told you so clear.ObjectXXStream means ObjectOutputStream "and" ObjectInputStream
lvguangchuana at 2007-7-21 11:01:49 > top of Java-index,Core,Core APIs...