• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Java help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

JigPu

Inactive Pokémon Moderator
Joined
Jun 20, 2001
Location
Vancouver, WA
I had to turn this in already, but I'd still like to know if anybody can tell me what's wrong with my code. Everything is good except for the second constructor for MultiSetB from what I can tell. When that constructor is run, I get a NullPointerReference expection (theElements is null for all values of i), and can't figure out why.

Code:
Exception in thread "main" java.lang.NullPointerException
        at MultiSetB.<init>(MultiSetB.java:71)
        at TestMultiSet.main(TestMultiSet.java:95)

Just as an intro to the program, I'm supposed to be writing a class which implements the MultiSetI interface using an array of ElementNodes (an ElementNode contains an Object el and an int count). The MultiSet stores objects and the count of the number of objects within it's private array theElements. You can find more info (the entire assignment description :D) here if you need it.

Code is attached (since it's kinda long and in 3 source files...). Thanks guys :)
JigPu
 

Attachments

  • MultiSet.zip
    3.8 KB · Views: 65
on line 71, you set theElements.el = els, which would work just fine if you had instantiated theElements first. If you insert the line "theElements = new ElementNode()" right before that, it should work just fine.

The reason you're getting a null pointer exception is because you're assuming that theElements is an object, which it isn't....it's still NULL because you haven't instantiated it yet.
 
OK, that makes sense :) I thought that declaring the array of ElementNode[] would go ahead and instantiate all the elements, but forgot that dosen't work for objects (it works for primitives, but not objects right?)

JigPu
 
No, what you're actually doing is instantiating an array, which is different. Say you have an array of 10,000 large objects, you'd want to control their instantiation as opposed to automatically instantiating all 10,000 of them...that could cause memory dumps like crazy.

I think you answered your own question about primitives...it doesn't really matter about array index instantiation because they're not objects.
 
Back