Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tower's of Hanoi using Stacks - Java
#1
so I just realized my APCS teacher will probably use the same assignment and one of my friends just posted this thread on my facebook, soooooooooooo deletin' errything
Reply
#2
Post the TOHApp part too. Complete code. With that debug error, you're looking at an off by one error somewhere.
Reply
#3
XTOTHEL Wrote:Post the TOHApp part too. Complete code. With that debug error, you're looking at an off by one error somewhere.

Oops.
Added.
Reply
#4
try creating a another program and just useTOHModel code. See if you can even initialize it.
Reply
#5
Marksman Bryan Wrote:Oops.
Added.

Try creating a another program and just use TOHModel code. See if you can even initialize it.
Reply
#6
poof
Reply
#7
If I'm not wrong you create an array of Tower objects but don't initialize them. I'm gonna assume that's what causes the issue
Reply
#8
MoldyBunny Wrote:If I'm not wrong you create an array of Tower objects but don't initialize them. I'm gonna assume that's what causes the issue

public Tower[] t = new Tower[3]; is the initializer, no?
That creates three ArrayListStack's in each Tower object, since a Tower takes no parameters.

EDIT:
nvm didn't work
Reply
#9
Marksman Bryan Wrote:public Tower[] t = new Tower[3]; is the initializer, no?
That creates three ArrayListStack's in each Tower object, since a Tower takes no parameters.

EDIT:
nvm didn't work

probably used the wrong terminology when replying, but basically you create an array of references but don't create the actual objects.
Reply
#10
Yes that is what I meant,

If you replace Towers with Stack in the code from your code above, so it becomes:

Public Stack[] t = new Stack[3]

then instead of t[0].adddisc

just do t[0].push(new Disc(i));

will it compile?
Reply
#11
MoldyBunny Wrote:probably used the wrong terminology when replying, but basically you create an array of references but don't create the actual objects.

Oh. That makes sense.
So, how to fix? Drawing a blank.

@XTOTHEL
Replacing it with Stack doesn't work. I assume it's the same problem, there isn't actually an object to add to, just a reference.
Reply
#12
just make a loop and create a new object for each element of the array

Code:
for(int i =0; i < 3; i++)
{
     t[i] = new Tower();
}
Reply
#13
MoldyBunny Wrote:-snip-

That gives me:
Code:
Exception in thread "main" java.lang.NullPointerException
    at Tower.addDisc(Tower.java:11)
    at TOHModel.<init>(TOHModel.java:10)
    at TOHFrame.<init>(TOHFrame.java:7)
    at TOHApp.main(TOHApp.java:5)
Java Result: 1

Which indicates there is a problem with Tower.
I wish I learned objects more in depth.

s is null according to the debugger, but I thought having
Code:
private Stack s;
    
    public void Tower(){
        s = new ArrayListStack();
    }
initialized it?

EDIT:
GOT IT.
I had public void Tower(), should be public Tower().
Derp mistake.

I WILL BE UPDATING THIS THREAD WITH MORE PROBLEMS, THOUGH. Thanks guys!

now to figure out how to paint the discs
Reply
#14
Finished the rest of it last night, and added in rules on the side and finished handling exceptions.
Everything works like it should, can't get it to throw an exception, so I'm happy Smile thanks again guys ~

I will be making another thread soon; I have to make an RPG game by the end of the year entirely with Java.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)