Java:从类/空逻辑错误调用方法

时间:2013-02-20 18:43:02

标签: java

我很难围绕基于对象的编程。我试图从类中调用一个方法。但是,我调用的所有内容都是已声明的变量,而我之后尝试拉变量。 (我确定我的术语已经关闭 - 随意纠正)

我遇到了逻辑错误。而不是一个值,我得到“空”。

班级:

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 + "";
}

}          

该计划:

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

2 个答案:

答案 0 :(得分:1)

  1. 您永远不会分配给您的成员变量oddsUnderpowersTwoUnder。所以当你阅读它们时它们当然是空的,当你试图打印它们时,你有一个NullPointerException 它打印“null”。

  2. 您可能实际上想要调用相同名称的方法而不是使用变量

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

答案 1 :(得分:0)

将您的属性设为private以避免此类情况,并在System.out...中更改您的属性,以调用方法而不是对象字段。例如

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