Adding to Array from JavaSpace problem
Not sure if this is advanced or not but here goes:
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
Well, first of all. The IndexOutOfBoundsException is being thrown because your array contains only 1 value and the index is going further than 0 i.e. the index is out of range, hence the 'out of bounds' part of the exception. I'm not 100% certain I follow what your trying to do but here goes my interpretation:
Extract all chatroom names from space;
Extract each chatroom name, store individual names in a string array;
Access string array[n] and use this value and all chatroom names to write to space;
Please let me know if this is correct.
Re: Adding to Array from JavaSpace problem
Quote:
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"
Can you rewrite your requirements in java programming terms?
Your question is all jargon about your app. Translate that to variables, classes and methods.