Results 1 to 13 of 13
  1. Default Tower's of Hanoi using Stacks - Java


    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
    Last edited by Marksman Bryan; 2013-02-03 at 06:44 PM.

  2. Default Re: Tower's of Hanoi using Stacks - Java


    Post the TOHApp part too. Complete code. With that debug error, you're looking at an off by one error somewhere.

  3. Default Re: Tower's of Hanoi using Stacks - Java


    Oops.
    Added.

  4. Default Re: Tower's of Hanoi using Stacks - Java


    Try creating a another program and just use TOHModel code. See if you can even initialize it.

  5. Default Re: Tower's of Hanoi using Stacks - Java


    poof
    Last edited by Marksman Bryan; 2013-02-03 at 06:44 PM.

  6. Default Re: Tower's of Hanoi using Stacks - Java


    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

  7. Default Re: Tower's of Hanoi using Stacks - Java


    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

  8. Default Re: Tower's of Hanoi using Stacks - Java


    probably used the wrong terminology when replying, but basically you create an array of references but don't create the actual objects.

  9. Default Re: Tower's of Hanoi using Stacks - Java


    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?

  10. Default Re: Tower's of Hanoi using Stacks - Java


    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.

  11. Default Re: Tower's of Hanoi using Stacks - Java


    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();
    }

  12. Default Re: Tower's of Hanoi using Stacks - Java


    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

  13. Default Re: Tower's of Hanoi using Stacks - Java


    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.

  14.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •