public class Test { public static void main(String[] args) 

ServerSocket server=null; 
try 

server=new ServerSocket(5432); 

catch(IOException e ) 
{ } 
OutputStream os; 
InputStream is; 
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
DataOutputStream dos; 
DataInputStream dis; 
Socket s; 
InetAddress address; 
String receiveMsg=null; 
String sendMsg=null; 
while(true) 

try 

s=server.accept(); 
address=s.getInetAddress(); 
System.out.println(address+"linked..."); 
os=s.getOutputStream(); 
is=s.getInputStream(); 
dos=new DataOutputStream(os); 
dis=new DataInputStream(is); 
receiveMsg=dis.readUTF(); 
System.out.println("The message from client is:"+receiveMsg); 
System.out.println("What is the message that we will send to the client:"); 
sendMsg=br.readLine(); 
dos.writeUTF(sendMsg); 
dos.flush(); 

catch(IOException e) 

e.printStackTrace(); 
} } 

} 总是出现异常, s=server.accept();这句有异常,NullPointer异常。怎么回事啊

解决方案 »

  1.   

    我这里测没有出现空指针啊,只是运行到那句话的时候锁住了,是不是因为client端没有发起连接,所以server端一直处在等待client发起链接的状态,因此下面的代码不走下去,我猜想的,回答的不对,请见谅.
      

  2.   

    你ServerSocket server看看是null么?
      

  3.   

    没抛异常啊 DEBUG走到s = server.accept();停掉。
      

  4.   

    lient端没有发起连接,所以server端一直处在等待client发起链接的状态,因此下面的代码不走下去,
    不是这样吧。。
      

  5.   

    连接上客户端后,receiveMsg=dis.readUTF();出现异常,时SOCKET异常。具体如下:java.net.SocketException: Connection reset。怎么回事
      

  6.   

    ServerSocket server=null; 
    try 

    server=new ServerSocket(5432); 

    catch(IOException e ) 
    { } 看这里,创建socket的时候可能有异常,而你什么也没做,还让程序继续了下去!
    如果出现异常了server并没有被创建的,仍然为null,后面调用就出现空指针异常了
      

  7.   

    楼上的说法不太有可能,应为上面的人说,debug程序的时候是在s = server.accept();的时候停掉的。