Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing lists

 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Java Gurus,

I am trying to write a method that takes two strings and compares them to existing reservations. The reservation object has a room number variable. The Suite also has a room number variable.

If the user passes in a string that matches an existing reservation, the method causes a 500 error message. If the user passes in a string that has no matches, instead of returning all the suites as available, it returns none.


I know this is very clunky code. I think it will eventually make sense to create an associate between a reservation and suite. Any thoughts on why it causes either error message would be greatly appreciated.

Thanks so much for your help,
Adam
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A 500 will almost certainly have more information in the log file, assuming you have logging turned on.

I can't really read that logic condition--consider refactoring. I'm pretty sure you don't need to check checkin == checkout *and* checkout == checkin, though.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Confino wrote:




Should not this code throw concurrentmodification Exception, if you pass some match. 500 message is of web page. That some exception was allowed to propagate back till response.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some other considerations are that your suiteList and suitesAvailable is pointing to same object, so when you say a fresh list, you should explain, what do you mean? and print suitesAvailable before and after modification in case of no match just to make sure suitesAvailable has data intially and later it is not showing anything.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Confino wrote:




this peice of code may throw nullPointerException as well, incase suitesNotAvailable is empty.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul.p Kumar wrote:
Adam Confino wrote:




this peice of code may throw nullPointerException as well, incase suitesNotAvailable is empty.


No, if suitesNotAvailable is empty the contains() method will always return false. There are two ways the code could throw a null pointer exception:
1) If suitesAvailable is null. We know suitesNotAvailable is not null because of the assignment to a new ArrayList with no possibility of re-assignment. But suitesAvailable may be null if suiteService.getSuiteList() is allowed to return null.

2) If suite was null, and suitesNotAvailable did not allow null values. As an implementation detail, suitesNotAvailable is an ArrayList and ArrayLists allow null, so this case would not happen with the provided code.

I do think the most likely source of the 500 error comes from a ConcurrentModificationException, but reading the logs is a better idea than guessing. You also might consider using suitesAvailable.removeAll(suitesNotAvailable) to achieve the task. Shorter code relying on the API implementations are usually more robust when you have the option.
 
Adam Confino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks a bunch. I'm going to try each of your suggestions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic