//==========================================JUI的服务器============================================import java.awt.Dimension;
import java.awt.Insets;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;public  class Serverl extends JFrame implements Runnable,ActionListener {
Socket s = null;
JButton st;
JButton fs;
TextArea to;
Thread th2;
TextArea sr;
Serverl s1;
public void f(){
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - 330)/2);
int y = (int) ((dimension.getHeight() - 210)/2);
this.setLocation(x,y);

((JPanel)this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("2.jpg");
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());

this.setResizable(false);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(348, 386);
this.setVisible(true);

JPanel jp1 = (JPanel) this.getContentPane();
jp1.setLayout(null);

to = new TextArea();
to.setBounds(9, 60, 334, 168);
this.add(to);
// ta.setEnabled(false);


sr = new TextArea();
sr.setBounds(10, 240, 331, 80);
this.add(sr);

st=new JButton("开启服务器");
st.setBounds(10, 325, 80, 30);
st.setMargin(new Insets(0,0,0,0));
st.addActionListener(this);
this.add(st);

fs=new JButton("发送消息");
fs.setBounds(248, 325, 80, 30);
fs.setMargin(new Insets(0,0,0,0));
fs.addActionListener(this);
this.add(fs);

}
public void run() {
ServerSocket b = null;
try {
b = new ServerSocket(8000);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(true){
s=b.accept();
InputStream in=s.getInputStream();
BufferedReader ber=new BufferedReader(new InputStreamReader(in));
String str=ber.readLine();
to.append("客户端:"+str+"\n");
// System.out.println("客户端:"+str+"\n");
}

} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "已断开连接!!");
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==st){
Serverl dx=new Serverl();
Thread th=new Thread(dx);
th.start();
}

}
public static void main(String[] args) {
new Serverl().f();
}
}
//=========================================客户端==================================================import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Client {
Socket s;
public void connectClient(){

try {
s = new Socket("localhost",8000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//接收客户端发送的数据
Thread th1 = new Thread(){
public void run(){
while(true){
try {
InputStream is = s.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = null;
while((str = br.readLine()) != null){
System.out.println("服务器端说:" +str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
};
th1.start();

Thread th2 = new Thread(){
public void run(){
while(true){
try {
OutputStream os = s.getOutputStream();
PrintStream ps = new PrintStream(os,true);
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
ps.println(str);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
};
th2.start();


} /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
new Client().connectClient();
}}为什么我接受客户端to.append("客户端:"+str+"\n");老报空指针····换来其他的都没问题