Search code examples
javaclassstatic-variablesmethod-calltowers-of-hanoi

How to run a method from a different class - JAVA


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

Solution

  • 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.