equals method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darryl Woodford

    equals method

    Hi,

    Can anyone give me a clue what's wrong with the following code, it
    compiles fine but errors on running with a NullPointerExce ption:

    for (int i = 0; i < adic.length; i++) {

    int cntone = 0;
    int cnttwo = 1;

    for (cntone = 0; cntone < adic.length; cntone++) {
    if (adic[i].word.equals(ad ic[cntone].word)) {
    adic[i].links[cnttwo] = adic[cntone].links[0];
    //delete adic[cntone]
    cnttwo++;
    }
    }
    }

    Basically it's supposed to be searching an anagram dictionary. Psuedo
    as
    follows:

    - take adic[i]
    - if adic[i].word = adic[cntone].word, adic[i].links[cnttwo] = adic
    [cntone].links[0]
    - increment cnttwo
    - loop

    adic[i].links[0] contains the first word I already know matches the
    anagram, in the below the first is adic.word, second adic.links[0]...

    ehinorsstw worthiness
    ghinnoorttw worthington
    ehlorsstw worthless
    eehlnorsssstw worthlessness
    horstw worths

    Thanks for any help,

    Darryl
  • Anthony Borla

    #2
    Re: equals method


    "Darryl Woodford" <[email protected] c.uk> wrote in message
    news:6d51bd21.0 311171546.5214a [email protected] gle.com...[color=blue]
    > Hi,
    >
    > Can anyone give me a clue what's wrong with the following
    > code, it compiles fine but errors on running with a
    > NullPointerExce ption:
    >
    > for (int i = 0; i < adic.length; i++) {
    >
    > int cntone = 0;
    > int cnttwo = 1;
    >
    > for (cntone = 0; cntone < adic.length; cntone++) {
    > if (adic[i].word.equals(ad ic[cntone].word)) {
    > adic[i].links[cnttwo] = adic[cntone].links[0];
    > //delete adic[cntone]
    > cnttwo++;
    > }
    > }
    > }
    >[/color]

    The most likely cause of the NullPointerExce ption [if, indeed, it is *this*
    code which is the source] is in not having a valid Object-derived entity in
    each array slot - a call to 'equals' or access of 'links' from an element
    containing null will certainly cause your problem.

    Another [potential, though not enough info available to be sure] problem,
    one which may cause an ArrayIndexOutOf BoundsException is the possible
    incrementing of 'cnttwo' to one value beyond 'cntone'. If it is subsequently
    used as an index, it may [possibly] point to one position beyond the last
    array element, and cause the aforementioned exception.

    I hope this helps.

    Anthony Borla


    Comment

    Working...