Results 1 to 2 of 2
  1. #1
    MemberZero is offline Member
    Join Date
    Oct 2012
    Rep Power

    Default Problem Accessing ArrayList in another class

    This is a simple Client-Server socket programming.
    Here is my Server Class

    import java.util.ArrayList;

    public class Server {
    public static ArrayList<MyThread> clients = new ArrayList<MyThread>();

    public static void main(String args[]) throws IOException {
    ServerSocket s = new ServerSocket(7777);
    try {
    while (true) {
    Socket s1 = s.accept();
    MyThread t = new MyThread(s1);
    clients.add(t); // adds threads to an ArrayList
    System.out.println(clients); // prints the ArrayList(It works and print all threads)
    t.start(); //start the thread

    } catch (SocketException e) {
    System.out.println("Error: " + e);

    This is a MouseClickeed method in my UI class where I want to access the ArrayList in Server Class

    public void mouseClicked(MouseEvent arg0) {
    try {

    String s = textField.getText();
    Client.ClientName = s; \\ gets the string entered and set it as Client Name
    System.out.println(Server.clients); \\It's supposed to print Client ArrayList but instead it prints an empty List ([])


    catch (NullPointerException e1) {
    System.out.println("Error: " + e1);

    I need help and need to understand why isn't the ArrayList accessible in the UI class !
    Last edited by MemberZero; 10-27-2012 at 10:45 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Problem Accessing ArrayList in another class

    It looks like the ArrayList is in fact accessible since you're not getting a compilation error. Perhaps you need to figure out instead why the ArrayList is empty.

    Also please click on my link below to see how to use code tags so we can be better able to read your code.

Similar Threads

  1. Need help accessing data in LinkedList
    By Clerek in forum New To Java
    Replies: 11
    Last Post: 08-02-2012, 10:39 PM
  2. Replies: 6
    Last Post: 01-26-2011, 07:49 PM
  3. Access LinkedList from another class
    By jboy in forum New To Java
    Replies: 20
    Last Post: 09-12-2009, 08:16 AM
  4. another NullPointerException in LinkedList class
    By muffstuff in forum New To Java
    Replies: 7
    Last Post: 04-10-2009, 10:51 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts