![]() |
|
Tower's of Hanoi using Stacks - Java - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: Tower's of Hanoi using Stacks - Java (/showthread.php?tid=52398) |
Tower's of Hanoi using Stacks - Java - Marksman Bryan - 2012-03-10 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 Tower's of Hanoi using Stacks - Java - XTOTHEL - 2012-03-10 Post the TOHApp part too. Complete code. With that debug error, you're looking at an off by one error somewhere. Tower's of Hanoi using Stacks - Java - Marksman Bryan - 2012-03-10 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. Tower's of Hanoi using Stacks - Java - XTOTHEL - 2012-03-10 try creating a another program and just useTOHModel code. See if you can even initialize it. Tower's of Hanoi using Stacks - Java - XTOTHEL - 2012-03-10 Marksman Bryan Wrote:Oops. Try creating a another program and just use TOHModel code. See if you can even initialize it. Tower's of Hanoi using Stacks - Java - Marksman Bryan - 2012-03-10 poof Tower's of Hanoi using Stacks - Java - MoldyBunny - 2012-03-10 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 Tower's of Hanoi using Stacks - Java - Marksman Bryan - 2012-03-10 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 Tower's of Hanoi using Stacks - Java - MoldyBunny - 2012-03-10 Marksman Bryan Wrote:public Tower[] t = new Tower[3]; is the initializer, no? probably used the wrong terminology when replying, but basically you create an array of references but don't create the actual objects. Tower's of Hanoi using Stacks - Java - XTOTHEL - 2012-03-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? Tower's of Hanoi using Stacks - Java - Marksman Bryan - 2012-03-10 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. Tower's of Hanoi using Stacks - Java - MoldyBunny - 2012-03-10 just make a loop and create a new object for each element of the array Code: for(int i =0; i < 3; i++)Tower's of Hanoi using Stacks - Java - Marksman Bryan - 2012-03-10 MoldyBunny Wrote:-snip- That gives me: Code: Exception in thread "main" java.lang.NullPointerExceptionWhich 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;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 Tower's of Hanoi using Stacks - Java - Marksman Bryan - 2012-03-11 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 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. |