Results 1 to 2 of 2
- 10-27-2012, 10:38 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
Problem Accessing ArrayList in another class
This is a simple Client-Server socket programming.
Here is my Server Class
import java.net.*;
import java.util.ArrayList;
import java.io.*;
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.
-
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
-
Need help accessing data in LinkedList
By Clerek in forum New To JavaReplies: 11Last Post: 08-02-2012, 10:39 PM -
Problem accessing a constant defined in another class
By subith86 in forum New To JavaReplies: 6Last Post: 01-26-2011, 07:49 PM -
Access LinkedList from another class
By jboy in forum New To JavaReplies: 20Last Post: 09-12-2009, 08:16 AM -
another NullPointerException in LinkedList class
By muffstuff in forum New To JavaReplies: 7Last Post: 04-10-2009, 10:51 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks