Whats wrong with this code....

Discussion in 'Java' started by is0871@gmail.com, Apr 6, 2005.

  1. Guest

    public List loadNotes(Long id)
    {
    List notes = new ArrayList() ;
    try
    {
    notes = (List) someLocalHome.findByPrimaryKey(id) ;
    if (notes != null)
    {
    Iterator i = notes.iterator() ;
    while (i.hasNext())
    {
    DummyBean bean = new DummyBean( (DummyLocal) i.next()) ;
    System.out.println("found bean: " + bean) ;
    notes.add(bean) ;
    }
    }
    else
    {
    System.out.println("Could not find any notes!") ;
    }
    }
    catch(Exception e)
    {
    e.printStackTrace() ;
    }

    return notes ;
    }


    If you can figure out whats wrong in the above code(wihout compiling),
    then you're the kind of person we're looking for. If interested in
    working in a famous special effects house then apply online here:
    http://www.ilm.com/jobs/it/20050104.html
    , Apr 6, 2005
    #1
    1. Advertising

  2. John McGrath Guest

    On 4/6/2005 at 6:58:43 PM, wrote:

    > If you can figure out whats wrong in the above code(wihout compiling)


    One obvious problem is that you are iterating over a collection and
    adding to the same collection in the process. This will result in a
    ConcurrentModificationException.

    --
    Regards,

    John McGrath
    John McGrath, Apr 7, 2005
    #2
    1. Advertising

  3. Guest

    wrote in message news:<>...
    > public List loadNotes(Long id)
    > {


    Snipppp.

    > working in a famous special effects house then apply online here:
    > http://www.ilm.com/jobs/it/20050104.html


    Hideously off-topic:

    ILM have really lost the plot of late.

    It's almost as though they've stopped striving for realism and devoted
    themselves to computer-realism. Everything's so plasticky. So
    playstationy. So perfectly unworldly. Ok, stop-motion looks pathetic
    in retrospect, but at least they moved on. Now they seem contented
    with fatter address busses. Who knows, maybe with 1000gigs of RAM they
    can finally have Anakin looking directly at Jar-Jar rather than two
    feet to his left.

    ..ed

    www.EdmundKirwan.com - Home of The Fractal Class Composition.
    , Apr 7, 2005
    #3
  4. HK Guest

    wrote:
    > public List loadNotes(Long id)
    > {
    > List notes = new ArrayList() ;
    > try
    > {
    > notes = (List) someLocalHome.findByPrimaryKey(id) ;

    [etc.]

    The code was intentionally convoluted by putting all
    the open braces in the wrong places.

    Trolling on,
    Harald.-)
    HK, Apr 7, 2005
    #4
  5. "John McGrath" <> wrote in message
    news:...
    > On 4/6/2005 at 6:58:43 PM, wrote:
    >
    > > If you can figure out whats wrong in the above code(wihout compiling)

    >
    > One obvious problem is that you are iterating over a collection and
    > adding to the same collection in the process. This will result in a
    > ConcurrentModificationException.


    You're quite right that adding to a collection while iterating over it is
    usually a bad idea, although I don't think it's possible to tell exactly
    what will happen because we don't know the actual class for either the
    iterator or the collection. Aside from compilation errors, without knowing
    what the fragment is supposed to do it's impossible to say how it might be
    wrong.

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Apr 7, 2005
    #5
  6. Matt Humphrey wrote:
    > Aside from compilation errors, without knowing
    > what the fragment is supposed to do it's impossible to say how it might be
    > wrong.


    There is one obvious thing:

    >>> List notes = new ArrayList() ;
    >>> try
    >>> {
    >>> notes = (List) someLocalHome.findByPrimaryKey(id) ;


    But it is to obvious ...

    /Thomas


    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Apr 7, 2005
    #6
  7. "Thomas Weidenfeller" <> wrote in message
    news:d338d5$a7g$...
    > Matt Humphrey wrote:
    > > Aside from compilation errors, without knowing
    > > what the fragment is supposed to do it's impossible to say how it might

    be
    > > wrong.

    >
    > There is one obvious thing:
    >
    > >>> List notes = new ArrayList() ;
    > >>> try
    > >>> {
    > >>> notes = (List) someLocalHome.findByPrimaryKey(id) ;

    >
    > But it is to obvious ...


    I had also thought that the newly created list is spurrious because it
    appears to always be overwritten, but there is a case where the method
    returns the empty list. If the findByPrimaryKey throws an exception, the
    method will return the empty list, rather than null because the exception is
    caught. Otherwise, note is either some kind of List or null. It appears to
    me more as plain bad design rather than being formally incorrect. (What's
    it *supposed* to return if findByPrimaryKey fails?)

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Apr 7, 2005
    #7
  8. Matt Humphrey wrote:
    > It appears to
    > me more as plain bad design rather than being formally incorrect.


    I really don't know. The else part indicates that the calling method
    should better be prepared to get a null returned, because that's what
    will happen in that case. But it is all twisted. If there should be a
    distinction between a failure and a "no results found", I would have
    returned an empty list for "no results found", and the null for the
    failure (or the exception for the failure). That code does it the other
    way around.

    I think we see the typical problems with such examples. Without knowing
    what it is supposed to do, everything can be interpreted in different
    ways. Hey, that's the idea!

    I hereby declare the the problem is the name of the method. I bet the
    developer wanted to call it noteLoading() instead of loadNotes() :)

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Apr 7, 2005
    #8
  9. Virgil Green Guest

    wrote:
    > public List loadNotes(Long id)
    > {
    > List notes = new ArrayList() ;
    > try
    > {
    > notes = (List) someLocalHome.findByPrimaryKey(id) ;
    > if (notes != null)
    > {
    > Iterator i = notes.iterator() ;
    > while (i.hasNext())
    > {
    > DummyBean bean = new DummyBean( (DummyLocal) i.next()) ;
    > System.out.println("found bean: " + bean) ;
    > notes.add(bean) ;
    > }
    > }
    > else
    > {
    > System.out.println("Could not find any notes!") ;
    > }
    > }
    > catch(Exception e)
    > {
    > e.printStackTrace() ;
    > }
    >
    > return notes ;
    > }
    >
    >
    > If you can figure out whats wrong in the above code(wihout compiling),
    > then you're the kind of person we're looking for. If interested in
    > working in a famous special effects house then apply online here:
    > http://www.ilm.com/jobs/it/20050104.html


    That's the worst attempt at Fortran that I've ever seen.

    --
    Virgil
    Virgil Green, Apr 7, 2005
    #9
  10. John McGrath Guest

    On 4/7/2005 at 7:42:58 AM, Matt Humphrey wrote:

    > I don't think it's possible to tell exactly what will happen because we
    > don't know the actual class for either the iterator or the collection.


    Good point. There is no guarantee that Iterators will be fail-fast.

    --
    Regards,

    John McGrath
    John McGrath, Apr 7, 2005
    #10
  11. Guest

    wrote:
    > public List loadNotes(Long id)
    > {
    > List notes = new ArrayList() ;


    Which 'List' there are at least 2.

    Robert
    , Apr 11, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tinus

    Whats wrong in this code??

    Tinus, Sep 2, 2003, in forum: HTML
    Replies:
    2
    Views:
    502
    Samuël van Laere
    Sep 3, 2003
  2. Robert Smith

    whats wrong with this simple code?

    Robert Smith, Apr 27, 2004, in forum: C++
    Replies:
    2
    Views:
    326
    Christopher Benson-Manica
    Apr 28, 2004
  3. Matthew Wilson

    Whats wrong with this crc32 code?

    Matthew Wilson, Feb 16, 2004, in forum: C Programming
    Replies:
    1
    Views:
    523
    Jack Klein
    Feb 17, 2004
  4. Vineeth V

    whats wrong with this code???

    Vineeth V, Feb 19, 2007, in forum: VHDL
    Replies:
    2
    Views:
    408
    R Quijano
    Feb 19, 2007
  5. Alfonso Morra
    Replies:
    7
    Views:
    308
    Alfonso Morra
    Jul 19, 2005
Loading...

Share This Page