By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

equals method

P: n/a
Hi,

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

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(adic[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
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Darryl Woodford" <dw****@essex.ac.uk> wrote in message
news:6d**************************@posting.google.c om...
Hi,

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

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(adic[cntone].word)) {
adic[i].links[cnttwo] = adic[cntone].links[0];
//delete adic[cntone]
cnttwo++;
}
}
}


The most likely cause of the NullPointerException [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 ArrayIndexOutOfBoundsException 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
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.