-1

I am trying to call the toh method from my main class(Driver). When I make the call it gives me a null pointer exception. How can I call the toh method in Hanoi from the driver class? When I combine the classes into one it works fine but I need them to be two separate classes. Also, I included the global variables I am using in both classes is that necessary? Any help is welcome. Thanks!

public class Hanoi {

 public static int N;
 public static int cycle = 0;
 /* Creating Stack array  */
 public static Stack<Integer>[] tower = new Stack[4]; 
 public static void toh(int n)
 {
     for (int d = n; d > 0; d--)
         tower[1].push(d);
     display();
     move(n, 1, 2, 3);         
 }
 /* Recursive Function to move disks */
 public static void move(int n, int a, int b, int c)
 {
     if (n > 0)
     {
         move(n-1, a, c, b);     
         int d = tower[a].pop();                                             
         tower[c].push(d);
         display();                   
         move(n-1, b, a, c);     
     }         
 }
 /*  Function to display */
 public static void display()
 {

     System.out.println("T"+cycle + "  Pillar 1  |  Pillar 2  |  Pillar 3");
     System.out.println("-------------------------------------");
     for(int i = N - 1; i >= 0; i--)
     {

         String d1 = " ", d2 = " ", d3 = " ";
         try
         {
             d1 = String.valueOf(tower[1].get(i));
         }
         catch (Exception e){
         }    
         try
         {
             d2 = String.valueOf(tower[2].get(i));
         }
         catch(Exception e){
         }
         try
         {
             d3 = String.valueOf(tower[3].get(i));
         }
         catch (Exception e){
         }

         System.out.println("  "+d1+"         |  "+d2+"         |    "+d3);
     }
     System.out.println("\n");
     cycle++;
 }
}

Main class(driver):

public class Driver{
 public static int N;
 public static int cycle = 0;
 /* Creating Stack array  */
 public static Stack<Integer>[] tower = new Stack[4]; 
 public static void main(String[] args)
 {
   int num = 0;
     Scanner scan = new Scanner(System.in);
     tower[1] = new Stack<>();
     tower[2] = new Stack<>();
     tower[3] = new Stack<>();
     /* Accepting number of disks */         

     while(num <=0){
         System.out.println("Enter number of disks(greater than 0):");
         num = scan.nextInt();
     }
     N = num;
     Hanoi.toh(num);
  }
 }
Art
  • 29
  • 6
  • Your code looks right to me, since `Hanoi.toh(n)` is public and static, you should be able to reference and call it as such. What is the error you are having? – EDToaster Oct 07 '17 at 02:15
  • As for you second question, it is in the best practice not to duplicate your global variables, as is convention. You will surely run into problems down the road. – EDToaster Oct 07 '17 at 02:16
  • After I run the run program and enter the number of disks I get this error: Exception in thread "main" java.lang.NullPointerException at towers.of.hanoi.Hanoi.toh(Hanoi.java:23) at towers.of.hanoi.Driver.main(Driver.java:30) Java Result: 1 – Art Oct 07 '17 at 02:16
  • 1
    That is you do not initialize your `tower` array's items... – Usagi Miyamoto Oct 07 '17 at 02:18
  • 1
    Just to troll: There is no global variable in Java.... – Usagi Miyamoto Oct 07 '17 at 02:29

3 Answers3

0

Try to initialize the tower array, something like this:

public static Stack<Integer>[] tower;

public static void toh( int n )
{
    tower = new Stack[n];
    for ( int d = 0 ; d < n ; d++ )
    {
        tower[d]=new Stack<>();
    }
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33
0

You are initializing your tower array inside your Driver class, however, you have not initialized it in your Hanoi class.

As I said in my comment, please do not write global variables twice, in different classes. This is because the different classes DO NOT share the same global variables. (when we say global variable, we mean that they are global to the Driver class only. To access those variables, use the dot operator)

For example, get rid of the N cycle and tower declarations from your Hanoi class

Then access those variables using the dot operator.

tower would become Driver.tower and N would become Driver.N and so forth.

Note: this only works if your Driver class is static, otherwise you would need to access it as an object attribute.

EDToaster
  • 3,160
  • 3
  • 16
  • 25
0

delete duplicated static values in a class (either Driver or Hanoi) then in the class that no longer has the static values and add that class to the beginning of all the missing classes.

Ex:

class A{
    public static int MyVar;
    public int aMethod(){
        return MyVar-2;
    }
}
class B{
    public static int MyVar;
    public void bMethod(){
        ++MyVar;
    }
}

↓ to ↓

class A{
    public static int MyVar;
    public int aMethod(){
        return MyVar-2;
    }
}
class B{
    public void bMethod(){
        ++A.MyVar;
    }
}
merlin
  • 534
  • 5
  • 15