Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What's wrong with my code? (generate sequences of numbers using a stack)

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question What's wrong with my code? (generate sequences of numbers using a stack)

    Hi! I am having a small (and possibly really simple) problem with my code (I am using java)...

    Here is the assignment given by my teacher:

    Use a Stack to write a method allSeqs(int max, int len) that will generate all sequences, consisting of the numbers between 1 and max, of length len. For instance, calling allSeqs(3,2) gives: [1, 1][1, 2][1, 3][2, 1][2, 2][2, 3][3, 1][3, 2][3, 3] while calling allSeqs(2, 4) gives [1, 1, 1, 1][1, 1, 1, 2][1, 1, 2, 1][1, 1, 2, 2][1, 2, 1, 1][1, 2, 1, 2][1, 2, 2, 1][1, 2, 2, 2][2, 1, 1, 1][2, 1, 1, 2][2, 1, 2, 1][2, 1, 2, 2][2, 2, 1, 1][2, 2, 1, 2][2, 2, 2, 1][2, 2, 2, 2]

    You can use a backtracking method, very similar to what we did for the N Queens problem: Push the proper number of 1's onto a Stack, and then repeatedly

    print out the stack contents (that's one permutation), and
    pop the top element, and if it's not equal to max, add one to it, and push it back.


    And here is my code:

    import java.util.*;
     
    public class NewStack {
     
      private Stack<Integer> s;
     
      public String toString() {
        String result = "[ ";
     
        while ( !s.isEmpty())
          result = result + s.pop() + ",";
        result = result + "]";
        return result;           
      }
     
     
     
      public void allSeqs(int max, int len) {      
        System.out.println(s); 
     
        while (s.size() < len){
          s.push(1);      
        }
        System.out.println(s);
     
        for (int i=0; i<len; i++){
          try{
     
            int a = s.pop();
     
            if (a < max) {
              a = a + 1;
              s.push(a);
            }    
            else {
              s.push(1);
            }
            System.out.print(s);
          }//end try block
          catch (NullPointerException e) {
            System.out.println("The last slot has been reached, the next one is empty so, we are done!");
          }
        }//end for loop 
      } //end method
     
     
     
      public static void main(String[] args) {
        NewStack nt = new NewStack();
     
        nt.allSeqs(2,3); 
      }
    }

    It compiles, but I can't get it to work properly, it gives me a NullPointerException whenever I try to run it...
    Do you know what's wrong??

    Thank you!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: What's wrong with my code? (generate sequences of numbers using a stack)

    it gives me a NullPointerException whenever I try to run it...
    What does it say?

  3. The Following User Says Thank You to jps For This Useful Post:

    mescoff (November 14th, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: What's wrong with my code? (generate sequences of numbers using a stack)

    Quote Originally Posted by mescoff View Post
    ...it gives me a NullPointerException ..
    The error message has a line number. The statement on that line is, somehow, dereferencing a reference variable that was declared but not initialized in your code. For example, invoking a method of some object where the object was never created.

    What is that variable?

    Where is it declared?

    Where is it initialized? (Note that for a reference variable that is declared but is not explicitly initialized in the declaration statement, it is initialized with a null value.)


    Cheers!

    Z
    Last edited by Zaphod_b; October 18th, 2012 at 01:24 PM.

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    mescoff (November 14th, 2012)

Similar Threads

  1. Generate all possible sequences from Event Tree
    By masterblaster in forum Algorithms & Recursion
    Replies: 0
    Last Post: November 22nd, 2011, 02:57 AM
  2. What am I doing wrong with this stack? Java Programming?
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2011, 12:19 PM
  3. How to generate 13 digit numbers to text file
    By duanedtp in forum Java Theory & Questions
    Replies: 3
    Last Post: April 27th, 2011, 11:30 AM
  4. trying to generate prime numbers
    By yingyang69 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2011, 12:21 PM
  5. Generate prime numbers within fiboncacci series?
    By Manish87 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 7th, 2010, 12:24 PM