Originally Posted by
orbin
I managed to solve the problem...
In my opinion, your fix wasn't a very good fix, stylistically speaking, but you did at least get the "list" variable initialized.
So...
Maybe we can start with where you are now, and later on, we can get back to your original code (if you haven't figured it out by then).
Anyhow...
Originally Posted by
orbin
...
I know that I'm controlling a DataElement in the [0] position of the array. Then I use the compareTo method, because I want to compare position 0 in the array to position 1 in the array....
Running your current code, I find that it reports that it inserted 100 elements. However,
.
.
.
bunch of numbers
.
.
.
41
19
70
13
77
5
83
Inserted 100 randomly generated numbers into the log.
Exception in thread "main" java.lang.NullPointerException
at IntElement.compareTo(IntElement.java:46)
at UnsortedArrayList.min(UnsortedArrayList.java:90)
at TestArray.main(TestArray.java:15)
So, now what?
Here's what: Go back and find why IntElement.compareTo() is throwing the NPE. IntElement.compareTo() is called by UnsortedArrayList.min(), which is called from TestArray.main().
For starters:
public int compareTo(DataElement obj)
{
System.out.println("In IntElement.compareTo(obj): obj = " + obj);
IntElement temp = (IntElement)obj;
if (number < temp.number) return -1;
else if (number > temp.number) return 1;
else return 0;
}
Now I get:
In IntElement.compareTo(obj): obj = null
Exception in thread "main" java.lang.NullPointerException
.
.
.
Back up to the place where compareTo() is called. Namely in UnsortedArrayList.min():
public int min() {
System.out.println("UnsortedArrayList.min(): list = " + list);
int lowest = 0;
for(int i = 0; i<100; i++){
System.out.println("UnsortedArrayList.min(): list[" +
i + "] = " + list[i] +
", list[" + (i+1) + "] = " + list[i+1]);
if(list[i].compareTo(list[i+1]) == 1){
IntElement temp = (IntElement)(list[i].getCopy());
lowest = temp.getNum();
}
Now I get:
Inserted 100 randomly generated numbers into the log.
UnsortedArrayList.min(): list = [LIntElement;@9931f5
UnsortedArrayList.min(): list[0] = IntElement@19ee1ac, list[1] = null
In IntElement.compareTo(obj): obj = null
Exception in thread "main" java.lang.NullPointerException
We see that list is not null. That's no surprise, since if it were, the previous stuff would have crashed, right?
Anyhow...
We see that list[0] has a value, but list[1] is still null. Where in your code did you expect that all of the list[i] variables would be given values? UnsortedArrayList.insert() or where? Look at the code. Really. Look. (If you can't see the problem, instrument that function with a print statement or two.)
See how it goes? When you get a problem, look at the code. If you think you need to make a bunch of changes (or if you want to start all over again), then go for it.
But if you want to understand how
that code got to
that point where the NPE was thrown, try to track it down. Maybe it's just a simple programming error and not one of those abstract things. (In other words: Don't panic!)
Anyhow, here's my suggestion:
Pick one thing. Fix it. Maybe in the process of fixing it you will come to an understanding of other problems that you will be able to fix
before they raise their ugly heads (or at least to recognize the effects when they happen in another context).
Tattooed on my psyche is part of a verse from a "Blindside" song that reminds me of my approach to debugging. It's a first-person description of a battle between me and the bug:
"
I grab you by the throat
And we come crashing down through the window
On the dirt ground below
And we wrestle in the mud and the blood and the beer"
---"Fell in Love With the Game"
(A really tender love ballad.)
Oh, the humanity! All that mud! All that blood! All that beer!
Cheers!
Z