user2092509
user2092509

Reputation: 15

Java: Invoking Methods From Class / null logic error

I'm having a hard time wrapping my head around object based programming. Im attempting to invoke a method from a class. However, all I invoke is a variable that has been declared, while I'm trying to pull the variable later. (Im sure my terminology is off - feel free to correct)

Im getting logic errors. Instead of a value, im getting "null".

The Class:

public class NumberOperations {
   int number;
   String oddsUnder;
   String powersTwoUnder;
   int isGreater;
   String toString;



public NumberOperations(int numberIn) {
   number = numberIn;

}

public int getValue() {
   return number;
}

public String oddsUnder() {
   String output = "";
   int i = 0;
   while (i < number) {
      if (i % 2 != 0) {
         output += i + "\t";
      }
      i++;   
   }
   return output;
}

public String powersTwoUnder() {
   String output2 = "";
   int powers = 1;
   while (powers < number) {
   output2 += powers + "\t";
   powers = powers * 2;
   }
   return output2;
}

public int isGreater(int compareNumber) {
   if (number > compareNumber) {
      return 1;
   }
   else if (number < compareNumber) {
      return -1;
   }
   else {      
      return 0;
   }   
}

public String toString() {
   return number + "";
}

}          

The Program:

import java.util.Scanner; import java.util.ArrayList;

/**  * Demonstrates the NumberOperations class.  */ public class NumberOpsDriver {

   /**
    * Reads a set of positive numbers from the user until the user enters 0.     * Prints odds under and powers of 2 under for each number.      *
    * @param args - Standard commandline arguments
    */    public static void main(String[] args) {

      Scanner in = new Scanner(System.in);

      // declare and instantiate ArrayList with generic type <NumberOperations>
      ArrayList<NumberOperations> numOpsList = new ArrayList<NumberOperations>();

      // prompt user for set of numbers
      System.out.println("Enter a list of positive integers separated "
                        + "with a space followed by 0:");

      // get first user input using in.nextInt()
      int firstInput = in.nextInt();
      // add a while loop as described below:
      while (firstInput != 0) {

         numOpsList.add(new NumberOperations(firstInput));

         firstInput = in.nextInt();
      }     

    // while the input is not "0"
         // add NumberOperations object to array based on user input
         // get the next user input using in.nextInt()

      int index = 0;
      while (index < numOpsList.size()) {
         NumberOperations num = numOpsList.get(index);
         System.out.println("For: " + num);
         System.out.println("Odds under: " + num.oddsUnder);
         System.out.println("Powers of 2 under: " + num.powersTwoUnder);

        // add print statement for odds under num
        // add print statement for powers of 2 under num

         index++;
      }    } }

Upvotes: 0

Views: 273

Answers (2)

Cyrille Ka
Cyrille Ka

Reputation: 15523

  1. You never assign to your member variables oddsUnder and powersTwoUnder. So of course they are null when you read them, and when you try to print them you have a NullPointerException it prints "null".

  2. You probably actually want to call the methods of the same name instead of taking the variables

         System.out.println("Odds under: " + num.oddsUnder());
         System.out.println("Powers of 2 under: " + num.powersTwoUnder());
    

Upvotes: 1

Arsen Alexanyan
Arsen Alexanyan

Reputation: 3141

Make your properties as private to avoid this kind of situations and change your properties in System.out... to call the methods not the object fields. For example

System.out.println("Odds under: " + num.oddsUnder()); //<-changed

Upvotes: 0

Related Questions