• 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

Not able to remove objects from the List

 
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added objects to the List based on some conditions but some how some null objects added to the list.when i print the elements of the list i am getting the following output.

Layout Name is nullProperty Name is nullStart is 0 Length is 0



So i want to remove these kind of objects from the list so i written code like below:



but still when i print the elements of list i am getting those null elements.What's wrong in the above code.Please help me.

Regards
Sudhakar
[ December 16, 2008: Message edited by: Karnati Sudhakar ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if (propertyStore1 == propertyStore2) {

This will only work if the variables propertyStore1 and propertyStore2 refer to the exact same PropertyStore object, which is not the case (and what is most likely not what you meant).

Use .equals(...) to compare two objects, and make sure that your PropertyStore class imlements an equals(...) method.
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Use .equals(...) to compare two objects, and make sure that your PropertyStore class imlements an equals(...) method.


As per your comment i changed my code like below.





But i am getting this exception:

Exception in thread "main" java.lang.NullPointerException



what did i do wrong?

Regards
Sudhakar
[ December 16, 2008: Message edited by: Karnati Sudhakar ]
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The stack trace should tell you exactly what line this is occurring on, can you see which one?
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The stack trace should tell you exactly what line this is occurring on, can you see which one?




Martijn Verburg Thanks for your reply.

Here is the stack trace :

Exception in thread "main" java.lang.NullPointerException
at xmlparser.PropertyStore.equals(PropertyStore.java:16)
at xmlparser.Main.main(Main.java:110)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)



Please help me..

Regards
Sudhakar
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because of your implementation of equals
For every propertyStore1 which have LayoutName or PropertyName equals to null, your equals implementation will throw a NullPointerException
this.LayoutName.equals(p1.LayoutName) --> this.LayoutName = null
this.PropertyName.equals(p1.PropertyName) --> this.PropertyName = null

Hence you need a null check to compare any objects (in your case String)
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karnati Sudhakar:


You know, you can do the same using the methods in List and Collection in just one line:

This may look odd, so I'll explain:

Collection.remove(Object) and List.remove(Object) use equals to check if objects match, and return whether or not the object was removed. The loop will make sure this method calls until no object is removed - and therefore there is no more object inside list that equals propertyStore2.

Both your and my solution can be a bit slow though, as both start at 0 when finding the element to remove. If there are mulitple occurrances, both go through the entire list multiple times. If the list is long that can be quite a performance hit.

The following are both more efficient:
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

You know, you can do the same using the methods in List and Collection in just one line:

Both your and my solution can be a bit slow though, as both start at 0 when finding the element to remove. If there are mulitple occurrances, both go through the entire list multiple times.

You could use the removeAll method which I guess would only iterate thru the list once (but I haven't actually looked at the source to see if this is the case).

Note that the remove and removeAll methods are optional so some List implementations may not support them.
 
Sunil Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Joanne/Rob .. both the implementations will require the equals method for PropertyStore to compare its methods in correct way. And with the current implementation of equals as provided by Karnati will always throw a NullPointerException as i mentioned above
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sunil Kumar:
Hey Joanne/Rob .. both the implementations will require the equals method for PropertyStore to compare its methods in correct way. And with the current implementation of equals as provided by Karnati will always throw a NullPointerException as i mentioned above



You're right. Both Rob (I assume) and I were just pointing out better/alternative ways to write the OPs original code once he had corrected the equals method (which you had already told him how to do).
[ December 16, 2008: Message edited by: Joanne Neal ]
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hence you need a null check to compare any objects (in your case String)



Sunil, Can you please suggest in details how to do that..

Thanks in Advance.

Rob Prime,

Can you suggest me how to implement the equals method to avoid the Null Pointer exception as suggested by Sunil.Many thanks for your suggestion i am looking at those two options.

Joanne Neal,

Thanks for your reply.I am using your code but still i am struck at this exception..
Main.java



PropertyStore.java



Output:

Exception in thread "main" java.lang.NullPointerException
at xmlparser.PropertyStore.equals(PropertyStore.java:16)
at java.util.ArrayList.indexOf(ArrayList.java:220)
at java.util.ArrayList.contains(ArrayList.java:201)
at java.util.AbstractCollection.removeAll(AbstractCollection.java:353)
at xmlparser.Main.main(Main.java:110)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)



Please any body suggest me how to remove those objects from the list.

Thanks in Advance
Sudhakar
 
Sunil Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Use this.. but i suggest you should try to visualize the correct behaviour of your objects yourself, i.e. think when would your two objects be equal

I just assumed even when you have null values in your PropertyName and LayoutName for both the objects.. they will be equal

[ December 16, 2008: Message edited by: Sunil Kumar ]
[ December 16, 2008: Message edited by: Sunil Kumar ]
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sunil.

It's working.

Regards
Sudhakar
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic