0

I am trying to access the

public static List<ChatThread> Chat_list of my ChatThread Class 

from the run() method of my Client Class but i keep getting an empty array(Infact it throws an exception at that point : Exception in thread "Thread-2" java.lang.NullPointerException)

and am very certain that that arrayList exists and is not empty(Because i did a test on the arrayList in my ChatThread Class). Just take a look at my code. Please I need your help on what to do.

Thanks.

This is the class containing the 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;
}


}

As for the Client class :

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;
} 
}

And my server class :

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();
        }
}
}

A main class to instantiate the Client class :

public class help {

public static void main(String[] argv)  {

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

}
4

1 回答 1

2

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

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

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

于 2013-01-06T15:24:11.177 回答