2 andy muge Andy_muge 于 2015.05.26 19:20 提问

java 编程中出现下面的错误

package Chat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class chatFrame extends JFrame implements ActionListener,Runnable{
LoginFrame frame=new LoginFrame();
Thread thread;
JPanel contentPane;
JScrollPane jScrollPane1=new JScrollPane();
JScrollPane jScrollPane2=new JScrollPane();
JButton jBtnSend=new JButton("发送");
JButton jBtnClear=new JButton("清空");
JTextArea jTextArea1=new JTextArea();
JTextArea jTextArea2=new JTextArea();
JScrollPane jScrollPane3=new JScrollPane();
JList jList1=new JList();
Socket socket;
int port=6000;
DataInputStream in;
DataOutputStream out;
Vector vector=new Vector();
String chatstr,str;
public chatFrame(){
setTitle("聊天室");
setSize(602,515);
this.setResizable(false);
this.setVisible(false);
try{
setDefaultCloseOperation(EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try{
out.writeUTF("下线了:"+frame.name+"/"+frame.sex);
out.close();
in.close();
socket.close();
}catch(Exception ee){ }
finally{
frame.dispose();
dispose();
}
}
}
);
jbInit();
}catch(Exception exception){
exception.printStackTrace();
}
}
void jbInit()throws Exception{
frame.setVisible(true);
jScrollPane1.setBounds(2,2,445,320);
jScrollPane2.setBounds(1,333,448,95);
jScrollPane3.setBounds(458,6,139,424);
jBtnSend.setBounds(93,441,78,31);
jBtnClear.setBounds(185,441,76,31);
jTextArea1.setEditable(false);
contentPane=(JPanel)getContentPane();
contentPane.setLayout(null);
contentPane.add(jScrollPane2);
contentPane.add(jScrollPane1,null);
contentPane.add(jScrollPane3);
contentPane.add(jBtnSend);
contentPane.add(jBtnClear);
jScrollPane1.getViewport().add(jTextArea1);
jScrollPane2.getViewport().add(jTextArea2);
jScrollPane3.getViewport().add(jList1);
jBtnClear.addActionListener(this);
jBtnSend.addActionListener(this);
try{
//InetAddress address=InetAddress.getLocalHost();
socket=new Socket("127.0.0.1",port);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}catch(Exception e){
setVisible(true);
jTextArea2.setText("无法连接服务器...");
}
if(thread==null){
thread=new Thread(this);
thread.start();
}
frame.setClection(socket,in,out);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jBtnSend){
sendStr();
}
if(e.getSource()==jBtnClear){
jTextArea1.setText("");
}
}
public void sendStr(){
chatstr=jTextArea2.getText();
if(chatstr.equals("")){
jTextArea2.setText("please say a word:");
return;
}
try{
out.writeUTF(frame.name+":"+chatstr);
jTextArea2.setText("");
}catch(Exception ee){ }
}
public void run(){
String sl=null;
String ss=null;
String s=null;
while(true){
try{
sl=in.readUTF();
}catch(Exception ee){
vector.removeAllElements();
jList1.setListData(vector);
jTextArea2.setText("已经与服务器断开...");
return;
}
if(sl.startsWith("可以注册")){
frame.setVisible(true);
setTitle("昵称..."+frame.getName());
continue;
}
else if(sl.startsWith("该用户已经注册")){
frame.jTextField1.setText("请重新输入昵称");
continue;
}
else if(sl.startsWith("新用户")){
StringTokenizer st=new StringTokenizer(sl,":");
ss=null;
s=null;
ss=st.nextToken();
s=st.nextToken();
vector.addElement(s);
jList1.setListData(vector);
continue;
}
else if(sl.startsWith("下线了")){
StringTokenizer st=new StringTokenizer(sl,":");
ss=null;s=null;
ss=st.nextToken();
s=st.nextToken();
vector.removeElement(s);
jList1.setListData(vector);
continue;

        }
        jTextArea1.append(sl+"\n");
    }
}

}


Exception in thread "Thread-2" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at Chat.chatFrame.run(chatFrame.java:133)
at java.lang.Thread.run(Unknown Source)

3个回答

diaoliwei2
diaoliwei2   Rxr 2015.05.26 19:29

是不是有多线程操作同一个东西了??

caozhy
caozhy   Ds   Rxr 2015.05.26 19:31

http://www.cnblogs.com/newsouls/p/4038273.html
两个线程操作同一个集合必须同步。

DZL_ACE
DZL_ACE   2015.05.26 19:42

同时操作线程控制一个东西会出现写冲突,把代码贴出来看看

Csdn user default icon
上传中...
上传图片
插入图片