Null error.

well i have been working on this assignment for some time. when somebody gave me some usefull code (below)

while (!listMale.isEmpty() && !listFemale.isEmpty()){// until one list empty

Couple max =new Couple(null, null, -1);// max so far

for (Male m : listMale){// all males

for (Female f : listFemale){// all female

int l = likeability(f,m);// evaluate likeability

if (l > max.likeability){

max =new Couple(f, m, l);// new max

}

}

}

listCouple.add(max);

listMale.remove(max.male);// remove male

listFemale.remove(max.female);// remove female

}

//

staticint likeability(Female f, Male m){

return Math.abs(f.like - m.like);

}

i have only know got to the point in using this code the problem is i get a null error when trying to run this method, i assume it's because this method first costructs and empty "couple" but i need this refrence so i can compare to the current max couple and i need something empty to being with;

this topic explains my assignment and what i am trying to do here;

[url]http://forum.java.sun.com/thread.jspa?threadID=5114924[/url]

as always your help is appreciated.

Aaron

[2144 byte] By [aaron1uka] at [2007-11-26 13:37:19]
# 1

So somebody else wrote this for you. You try to run it as is, but it fails. Now you want us to debug more code that you didn't write to make your assignment go. Is that fair?

Please point out what you contribute to this effort besides taking credit for the contributions of others.

At which line do you get the NPE?

I'm not sure you can iterate over a List once you've removed an entry. The iterator isn't valid anymore.

Why remove values from the list anyway? Create List of compatibility values, and sort them once you have them all.

%

duffymoa at 2007-7-7 22:24:03 > top of Java-index,Java Essentials,New To Java...
# 2

Put in a default Male and default Female (with minimum likeability).

Or, initialize max to null, and make the loops do this:

for (all males)

{

for (all females)

{

if (max == null)

{

max = new Couple(f, m, l);

}

else if (l > max.likeability)

{

max = new Couple(f, m, l); // new max

}

}

}

Also, you should make "likeability" a private field in Couple, and provide a "getLikeability" method. Same with the "male" and "female" fields of Couple.

doremifasollatidoa at 2007-7-7 22:24:03 > top of Java-index,Java Essentials,New To Java...
# 3

The concept of "maximum likeability" L should be properly defined. In

the OP's previous thread I defined it as max sum(L(m_i, f_j) over all

males i and females j. This obviously is from a dating office standpoint.

OTOH, finding a pair m_i, f_j such that their L(m_i, f_j) is the maximum

of all possibilities is a trivial exercise.

If the first definition is the correct one, the problem is a simple

assignment problem which is a sub-problem of the minimum cost

network flow problem. If the second definition is correct it is, well, a

trivial exercise.

kind regards,

Jos

JosAHa at 2007-7-7 22:24:03 > top of Java-index,Java Essentials,New To Java...