java Socket传输对象 out空指针异常 主要代码在下面 5
publicclassClientFrameextendsJFrame{/*..*/staticSocketsocket;staticObjectOutputStream...
public class ClientFrame extends JFrame{
/*..*/
static Socket socket;
static ObjectOutputStream out;
static ObjectInputStream in;
static Message message;
public ClientFrame(){
this.initNet();
//文本输入发送
inputJButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
message=new Message(inputJTextArea.getText());
System.out.println("发送"+message.getMessage());
try {
out.writeObject(message);
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
inputJTextArea.setText("");
}
});
}
//建立连接
public void initNet(){
try {
socket = new Socket("localhost", 12345);
in= new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
out = new ObjectOutputStream(socket.getOutputStream());
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args){
ClientFrame frame=new ClientFrame();
//frame的一些设置
try {
Object obj = in.readObject();
if (obj != null) {
message = (Message)obj;
System.out.println("接收"+message.getMessage());
}
} 展开
/*..*/
static Socket socket;
static ObjectOutputStream out;
static ObjectInputStream in;
static Message message;
public ClientFrame(){
this.initNet();
//文本输入发送
inputJButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
message=new Message(inputJTextArea.getText());
System.out.println("发送"+message.getMessage());
try {
out.writeObject(message);
out.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
inputJTextArea.setText("");
}
});
}
//建立连接
public void initNet(){
try {
socket = new Socket("localhost", 12345);
in= new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
out = new ObjectOutputStream(socket.getOutputStream());
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args){
ClientFrame frame=new ClientFrame();
//frame的一些设置
try {
Object obj = in.readObject();
if (obj != null) {
message = (Message)obj;
System.out.println("接收"+message.getMessage());
}
} 展开
2017-09-26
展开全部
程序中,无法知道是否已经连接成功,,,,,所以 应该在捕捉异常之后,作一下显示错误信息的处理
~
~
~
~
~
~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询