473,490 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 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
1 1867

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
7427
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
3
3755
by: Frank Wisniewski | last post by:
Is this suppose to work like this: I have a class called foo in which I tried to override equals and return different answers based on what was passed in. public class foo { private string...
4
2529
by: Kurt | last post by:
Wouldn't you agree all of the follwoing should produce the same result? r = (o1 == o2); r = (o2 == o1); r = object.Equals(o1, o2); r = object.Equals(o2, o1); r = (o1.Equals(o2)); r =...
4
3190
by: Chris | last post by:
This is something that has been bugging me for some time. When exactly should I be using == instead of .Equals() ? I see a lot of code that performs object evaluation e.g. Is Object1 the same as...
12
6705
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
4705
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
12
2187
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
1
4325
by: Omiris | last post by:
I'm trying to use the Equals() method that is defined on the List(Of T) class. The docs seem to state that if T implements the IEquatable interface, then it will use the Equals method that is...
5
2371
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
10
104953
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.