Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with a method in my EJB  RSS feed

 
Johan Svensson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I'm rather new to J2EE and I am right now trying to develop a chat application for a school project. I'm having problems with one of the methods in my bean and I was wondering if someone here might be able to help me (setCurrentRoom(String currentRoom) in the following code). I get a "java.util.ConcurrentModificationException" -exception when I try to set the string currentRoom to a certain value from my jsp. The class chatRoom is defined in another application scoped bean. Does anyone know what the problem might be?



Been stuck on this project for a long time now, would really appreciate some help!

Best Regards

Johan

 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be suspicious of this:

which is in two places in the setCurrentRoom() method. That looks recursive to me.
 
Dhiraj Mahapatro
Greenhorn
Posts: 12
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess Tom is correct.
In addition to that I would suggest to use the ListIterator li wherever possible in place of directly handling the ArrayList in the code.

The basic theory beneath this is "You have your guy [iterator] to do the work for a Customer[arraylist]. you need not contact the customer[arraylist] directly. Route through the guy [iterator]"
If anyone tries to handle both the jobs, you develop a concurrency and hence java.util.ConcurrentModificationException.

The code would look like




Try this way. It should work. Please also look at the recursion.

Thanks.....
 
Johan Svensson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys!

I finally figured it out, thank you so much for the help! apparently ListIterator() does not have a method called isEmpty though, so I still used the ArrayList itself. Anyway I honestly don't know exactly what was wrong, since it worked when I used a for-loop instead of the while-loop. I did however add a global boolean variable called exists to keep track of when to exit the for-loop and when to enter the following if-statement. I'm still using the recursion, since I think its kinda neat Here is the working code:



Anyway thanks for the help! I will definitely continue using this forum when it comes to Java programming, its been a very positive experience so far


Best Regards

Johan
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
Looking at your code in setCurrentRoom() I think you should consider using a Map (Key - Room name and Value - ChatRoom object). This will highly simplify your logic and lines of code needed to do the iteration on arraylist and comparing room names individually.

Regards,
Amit
 
Johan Svensson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is.. I need to be able to iterate over the list in my JSP site. Wouldn't using a map cause problems with this? I don't wanna use Java script in my JSP, only JSTL.

//Johan
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
JSTL can handle the Map iterations as well. Sorry, I do not have any example at the moment that I could have posted here.

Regds,
Amit
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!