日期:2014-05-20  浏览次数:20931 次

DefaultListModel 出现空指针异常问题
package com.hyn;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Talklogin extends JFrame{
/**

*/
private static final long serialVersionUID = 1L;
private ObjectOutputStream oos;
private JList user_list;
private JTextArea show_jta;
private JTextField send_jtf;
private JTextField jtf_username;
private boolean loginFlag = false;
public void createsocketlogin(){
try {
Socket socket=new Socket("localhost", 8765);
oos=new ObjectOutputStream(socket.getOutputStream());
new loginthread(socket).start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

class loginthread extends Thread{
Socket socket;
public loginthread(Socket socket){
this.socket=socket;
}

public void run(){
try {
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
DefaultListModel dcbm=(DefaultListModel)user_list.getModel();
while(true){
String getout=br.readLine().trim();
if(!getout.startsWith("MEG:")){
if(getout.startsWith("退出:")){
dcbm.removeElement(getout.substring(3));
}else{
@SuppressWarnings("unused")
boolean flag=false;
for(int i=0;i<dcbm.getSize();i++){
if(getout.equals((String) dcbm.getElementAt(i))){
flag=true;
break;
}
}
if(flag=false){
dcbm.addElement(getout);
}
}
}else{
DateFormat df=DateFormat.getDateInstance();
String date=df.format(new Date());
df = DateFormat.getTimeInstance(DateFormat.MEDIUM);
  String time = df.format(new Date()); 
  String senduser=getout.substring(4, getout.indexOf(":发送给:"));
  String receive=getout.substring(getout.indexOf(":的消息是:")+6);
  show_jta.append(" "+senduser+" "+date+" "+time+"\n "+receive+"\n");
  show_jta.setSelectionStart(show_jta.getText().length()-1);
  show_jta.setSelectionEnd(show_jta.getText().length());
  send_jtf.requestFocus();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void send(){
String loginusername=jtf_username.getText().trim();
String getout=send_jtf.getText();
if(getout.equals("")){
return;
}
ArrayList<String> al=new ArrayList<String>();
Object[] receiveusername=user_list.getSelectedValues();
if(receiveusername.length<=0){
return;
}
for(int i=0;i<receiveusername.length;i++){