• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with ArrayList .contains() method

 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks ,
Im facing a problem with an arrayList that im using to store a list of values obtained from a list of beans. Im trying to store the values of a particular property of the beans into an arrayList named chkList and then use it to set a variable .The input is obtained through an jsp form and processed in a servlet.Here's my code
https://coderanch.com/forums/posts/list/517209#2447416
However, no matter what value I pass to Dtype , the value of setDtID is always being set to -1. Why is this happening ?
I already feel its a mistake due to syntax goof up . Any pointers in solving it would be helpful.
Thank you for your time.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to override equal method(considering the attribute of OBJECTID) of the bean/DomainTypeBean. contains uses equals method of the object which inside the List.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:you need to override equal method(considering the attribute of OBJECTID) of the bean/DomainTypeBean. contains uses equals method of the object which inside the List.


I didnt quite get you
are you suggesting something like

As I did try that and it didnt work .
Thanks for replying.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry my above post is wrong. please ignore that!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you post your DomainTypeBean class?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Okay!Thanks for following up.!
My DomainTypeBean class is as follows.

 
Ranch Hand
Posts: 144
Oracle Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should be a put try/catch around the parseInt call to prevent an exception from crashing your thread.

Is this code inside a Servlet?
Have you verified that Dtype is being set?
How did you declare chkClist? It is a List<Integer> ?
Is chkList a local, instance, or class variable?
Have you verified that values are being added to the ChkList?
Is there any omitted code that you think might be causing a problem?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Zal wrote:There should be a put try/catch around the parseInt call to prevent an exception from crashing your thread.


Will try that.

Mike Zal wrote:Is this code inside a Servlet?


Yes its a servlet

Mike Zal wrote:Have you verified that Dtype is being set?


Yes it is being set.

Mike Zal wrote:How did you declare chkClist? It is a List<Integer> ?


Its an arraylist.

Mike Zal wrote:Is chkList a local, instance, or class variable?


class variable

Mike Zal wrote:Have you verified that values are being added to the ChkList?


yes they are being added.

Mike Zal wrote:Is there any omitted code that you think might be causing a problem?


Thats the link to the entire servlet code.Please do have a look.
http://pastebin.com/embed_iframe.php?i=4zxbbjn0
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike ,
I have tried putting a try catch statement and also tried casting the List<Integer> as well , however it still doesnt work. Any further suggestions?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that the whole of your bean class? The advice you were given earlier, to override the equals method (and hashCode) was correct.You will have to check my amendments for spelling errors and whether the () match. In the case of the hash code, you multiply the result repeatedly by prime odd numbers, so as to maximise the variablilty in the lower-order bits.
In the case of equals, you check whether they are the same object; if not, the null checks must be done in a particular order to avoid Exceptions.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to have missed out your dtTypeOld from those methods. I am sure you can work out how to correct that.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,
Thanks a lot for replying.
Im sorry. I still dont understand the logic, could you explain in simpler terms ? Im just a newbie to java .
And I did try your code , it still throws the same error.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are going to have to simplify things. Create a class to correspond to your Bean. Put all 5 fields into the hashCode and equals methods. Delete "import java.lang.*;" which is never necessary. You should now have this:I have also added a toString method.

You will find more about the equals and hashCode methods from this post.

When I copied your code, I found another error. Look at the names of the set and get methods, especially that for the Oid field. Your methods aren't named correctly to match the field name; I have (I think) corrected that in this post. I don't know whether that is causing your particular problem, but creating Beans from XML data requires those naming conventions.
And use spaces, not tabs for indentation.

Now, run a simple command line application. Forget about the jsp and the servlet. Run a main method which adds the values to yrou Bean class, and adds them to the List later. Then see what you are getting.That will give you a start; you can verify that things are actually getting into your Bean. Then gradually work back to the full-size application.
And check what I posted carefully; it is bound to be full of spelling errors.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,
Thanks a lot for the reply . ! Im sorry for the delay in replying back. I did try your code and faced the following error when i ran the bean class through the test class.
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 'd'
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at com.jspcrud.bean.DomainTypeBean.toString(DomainTypeBean.java:72)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)

The line where this occurs is. while over riding the toString () method

Any ideas as to where i could be going wrong?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name of the Exception tells you. The number of % tags and the number of arguments after the closing " and , must match. You appear to ahve six % tags (not counting the %n) and five arguments.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have forgotten to incldue "code" in the list of arguments.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell ,
Thanks for replying , I did make the change and the bean code worked well , however , the contains method within my servlet still fails , why is that?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. Find the Lists you are using and print them out. You should get a printing of all their contained elements, so try to keep the list small, so it will all fit onto one screen.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,
done that too , the element is contained in the list ,yet the over ridden equals method fails
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get the element out of the List by using something like myList.get(3).
Compare it with what you think it is equal with this sort of thing:If that doesn't work, show us the overridden equals() method. If your bean equals null, you will suffer a nice NullPointerException.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,
Sorry for the delay. I tried your code snippet after removing %b'
and got an error while running the snippet. as below.
inside loadDomainType
DomainType code=MARITALSTATUS, objid=1, description=MaritalStatus
ChkList val1
[1]
Please enter index you are looking for, remembering 1st index is 0: 1
My bean equals myList.get(1):
Jun 6, 2011 10:06:48 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.jspcrud.dao.DAO.getDtList(DAO.java:98).
Any clues where i could be going wrong?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which case you need to check the size of that List. What happens when you try element 0? If you get a similar Exception for 0, it probably means you have a List with no elements in.
 
Greenhorn
Posts: 7
Eclipse IDE Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

give the below trial

if Dtype is not Integer object, use give the below trial.

Integer tempObj = new Integer(Integer.parseInt(Dtype));
chkList.contains(tempObj)


when Dtype is Integer , go with below code.
chkList.contains(Dtype)


Thanks,
MVVPR




 
reply
    Bookmark Topic Watch Topic
  • New Topic