File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem with ArrayList .contains() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with ArrayList .contains() method" Watch "Problem with ArrayList .contains() method" New topic
Author

Problem with ArrayList .contains() method

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

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
http://www.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.


Learning and Learning!-- Java all the way!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 05, 2011
Posts: 477

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

Joined: Jan 28, 2008
Posts: 5575

sorry my above post is wrong. please ignore that!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

can you post your DomainTypeBean class?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Oh Okay!Thanks for following up.!
My DomainTypeBean class is as follows.

Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

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?

OCJP6, OCWCD5
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

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

Joined: Jan 05, 2011
Posts: 477

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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
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
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
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

Joined: Jan 05, 2011
Posts: 477

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
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
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

Joined: Jan 05, 2011
Posts: 477

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
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
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
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
I think you have forgotten to incldue "code" in the list of arguments.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

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
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
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

Joined: Jan 05, 2011
Posts: 477

Hi Campbell,
done that too , the element is contained in the list ,yet the over ridden equals method fails
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
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

Joined: Jan 05, 2011
Posts: 477

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
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
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.
venkat prathap
Greenhorn

Joined: Jun 06, 2011
Posts: 7

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




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with ArrayList .contains() method
 
Similar Threads
Comparing values using JSTL
Dropdown Selected returning unexpected value on submit.
Page not refreshing dynamic content.
List of beans returning empty
Jstl not reflecting values back to servlet