Adding to Array from JavaSpace problem
I'm now stuck on such a simple task I have no idea what to do. I just want to take from the space all "chatRooms" then extract the chat room name which i will keep in an array, then write to the space supplying the extracted chat room name and "chatRooms". I have tried writing the object to array but no luck as it showed an object but couldn't extract name. I keep getting the following error from the code:
Error: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
Code :
public void threadchatRetrieval()
{
space = SpaceUtils.getSpace();
if (space == null){
System.err.println("Failed to find the javaspace");
System.exit(1);}
int count = 0;
Username template = new Username(null,"chatRooms");
do{
try {
temp = (Username)space.take(template, null,6000);
System.out.println("output: " + temp.Name);
if(temp.Name.equals(null)){
System.out.print("null entry");
} else{
entry.add(0, temp.Name);
count++;
System.out.print("added at: " + count);
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnusableEntryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransactionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e){
System.out.println("done");
}
//entry.add(temp.Name);
}while(temp != null);
System.out.println("output size: " + entry.size());
for (int i=0; i<= entry.size();i++) {
System.out.println( "for: " + entry.get(i));
try {
String Username = entry.get(i);
Username template_new = new Username(Username,"chatRooms");
System.out.println( "write: " + Username);
space.write(template_new, null,8000);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransactionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Re: Adding to Array from JavaSpace problem
Here is your problem:
Code :
for (int i=0; i<= entry.size();i++) {
System.out.println( "for: " + entry.get(i));
Your ArrayList objects size is 1, but you put you string at the fixed index 0 here:
Your for loop runs through 2 times, one time where i = 0, and one where i = 1 (since size is 1)
First time, get(0) will resolve and get you the string.
Second time, get(1) will not resolve, and give your error message.
Might be easier to see when compacted
Code :
ArrayList<String> myList = new ArrayList<String>();
myList.add(0, "stringZero");
for (int i = 0; i <= myList.size(); i++){
System.out.println(myList.get(i));
}
tl;dr
Solution, make i sharply smaller than size(); i < size();
Please use the code brackets for you code, and also include a line number with the error message. Its much easier to read and find an error then :-).
Re: Adding to Array from JavaSpace problem
thankyou for the reply. I have been reading alot on JavaSpaces, turns out to treat objects as if they were in an array you need to write a head and tail object to space to keep track, i thought this was a hack way of doing things and surley there was a better way but this is what is suggested and it works great.
Re: Adding to Array from JavaSpace problem
That is called a Linked list:
Each element doesn't know where they are in the world, but they know who their neighbors are. The end elements obviously know that they are the end elements because they only have one (or none) neighbor.
Arrays are kind of the opposite: They know where they are in the world, but could care less who their neighbors are. That's why inserting to arrays is so inefficient: you have to tell a lot of people they need to move so someone can fit into a certain spot.