从另一个Thread的类访问Thread类的公共静态arrayList

时间:2013-01-06 15:15:21

标签: java swing

我正在尝试访问

public static List<ChatThread> Chat_list of my ChatThread Class 

来自我的客户端类的run()方法,但我一直得到一个空数组(事实上它在那时抛出异常:Exception in thread "Thread-2" java.lang.NullPointerException

并且非常确定该arrayList存在且不为空(因为我在ChatThread类中对arrayList进行了测试)。看看我的代码吧。 我需要你的帮助才能做什么。

感谢。

这是包含arrayList:

的类
public class ChatThread extends Thread {


private Socket sc;
private String cherry_name;
private String passwd;
public static List<ChatThread> Chat_list = new ArrayList<ChatThread>(); //THE STATIC ARRAY LIST
private BufferedReader br;
private BufferedWriter bw;
public ChatThread(Socket sc){
    try {
        this.sc=sc;
        System.out.println(sc);
        br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
        bw = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
        String help = br.readLine();
        this.cherry_name=help.split("@")[0];
        this.passwd=help.split("@")[1];
        System.out.println(this.cherry_name);
        System.out.println(this.passwd);
        Chat_list.add(this); //This is where i add it to the arrayList
        if(Chat_list.isEmpty()) //This is where i did the test
            System.out.println("I am empty");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void run(){
//Comparaison of information with that in the database    
try{
                    bw.write("success");
                    bw.newLine();
                    bw.flush();

            while(true){

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

    }

public Socket getSc() {
    return sc;
}   

public String getCherry_name() {
    return cherry_name;
}


}
  

至于Client类:

public class Client extends Thread {
private Socket socket;
private BufferedReader br;
private BufferedWriter bw;
 private ChatThread th;
 private String cherry_name;
 public Client(String cherry_name,String passwd){
 try
 {
     socket = new Socket("127.0.0.1", 8888);
     this.cherry_name=cherry_name;
     br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
     bw.write(cherry_name+"@"+passwd);
     bw.newLine();
     bw.flush();
 }
catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Erreur lors de la lecture du socket");
            e.printStackTrace();
        }
 }
 @SuppressWarnings("deprecation")
public void run()
 { 
        try {
            String help = br.readLine();
     if(help.equals("failed")){
         this.notify();
         this.destroy();
         socket.close();
         }
     else{
         if(ChatThread.Chat_list.isEmpty()) System.out.println("Empty array!!!"); //This is where it says the array is empty whereas it wasn't the case in the ChatThread Class
         for(ChatThread ct : ChatThread.Chat_list){
                    if(cherry_name.equals(ct.getCherry_name())){
                        th=ct;
                        break;
                    }
         }
         while(true){

         }

         }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Error whilst reading from the socket");
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println("Interruption");
            e.printStackTrace();
        }

    }

public Socket getSocket() {
    return socket;
} 
}

我的服务器类:

public class Server {

public static void main(String[] args) {
    try {
    ServerSocket server =new ServerSocket(8888);
    Socket sc;
    System.out.println("Server Started");
    while(true){
            sc=server.accept();
            System.out.println("New Connection");
            new ChatThread(sc).start();
    }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
}

实例化Client类的主类:

public class help {

public static void main(String[] argv)  {

new Client("Jerry","Smith").start(); 
}

}

1 个答案:

答案 0 :(得分:2)

必须以同步方式访问对两个线程之间共享的可变对象的每次访问。不同步会导致您看到的可见性和连贯性问题。

你不应该像这样公开ArrayList(即使没有多个线程,公共静态可变对象已经是一个非常糟糕的做法)。相反,您应该将其封装在您自己的对象中,并确保每个访问都正确同步。

如果没有看到任何代码,很难给出更具体的建议。