【问题标题】:Null Exception Error while returning values to driver [duplicate]将值返回给驱动程序时出现空异常错误[重复]
【发布时间】:2018-09-27 04:06:38
【问题描述】:

所以,这是我的类,其中包含我的构造函数和访问器方法:

public class sac
{
// Initializing instance variables.
private double bal;
private double[] dep;
private double[] wit;

/**
 * Constructor for objects of class sac
 */
public sac()
{
    //Declaring instance variables
    bal = 500;

    //Deposits:
    double[] dep = new double[5];
    dep[0] = 100.00;
    dep[1] = 124.00;
    dep[2] = 78.92;
    dep[3] = 37.55;
    dep[4] = 83.47;

    //Withdrawals:
    double[] wit = new double[7];
    wit[0] = 29.88;
    wit[1] = 110.00;
    wit[2] = 27.52;
    wit[3] = 50.00;
    wit[4] = 12.90;
    wit[5] = 15.20;
    wit[6] = 11.09;
}

/**
 * Returns and sets the value of balance.
 *
 * 
 * @return bal
 */
public double setBal(double b)
{
    //Declaring instance variables
    b = bal;
    return bal;
}

/**
 * Returns and sets the values of deposits.
 *
 * 
 * @return deposits
 */
public double[] getDep()
{
    double[] d = new double[5];
    //Deposits:
    d[0] = dep[0];
    d[1] = dep[1];
    d[2] = dep[2];
    d[3] = dep[3];
    d[4] = dep[4];
    return d;
}

/**
 * Returns and sets the values of withdrawals.
 *
 * 
 * @return withdrawals
 */
public double[] getWit()
{
    double[] w = new double[7];
    //Withdrawals:
    w[0] = wit[0];
    w[1] = wit[1];
    w[2] = wit[2];
    w[3] = wit[3];
    w[4] = wit[4];
    w[5] = wit[5];
    w[6] = wit[6];
    return w;
    }

}

代码可能很草率,因为我一直在试图找出解决此错误的方法,但这是我正在尝试运行的驱动程序。尝试从另一个类中的访问器方法调用我的值时发生错误。

import java.util.*;
/**
  */
public class sacDemo
{
public static void main(String[] args)
{
    Scanner input = new Scanner(System.in); //Creates new scanner object.
    sac sac = new sac(); //Creates accessor object to retrieve variables       from sac class.

    int bal;
    double air;
    double[] dep = new double[5];;
    double[] wit = new double[7];

    double[] d = dep;
    double[] w = wit;

    w = sac.getWit();
    d = sac.getDep();

    System.out.println("What is your annual interest rate?");
    air = input.nextInt();
    air = air/10;

    System.out.println("Your deposits for the month were: ");
    for(int i=0;i<5;i++){

        System.out.println(dep[i]);
    }
    System.out.println("Your withdrawals for the month were: ");
    for(int i=0;i<6;i++){

        System.out.println(wit[i]);
    }

}
}

【问题讨论】:

  • 为什么是javascript标签?

标签: java arrays nullpointerexception


【解决方案1】:

你的 setBal 函数是错误的伙伴。

public double setBal(double b)
{
    // Declaring instance variables
    // b = bal; <--- wrong
    bal = b; // <--- right
    return bal;
}

【讨论】:

    【解决方案2】:

    在您的 sacDemo 类中,您正在初始化新的数组对象并将它们分配给变量 d 和 w。

    double[] dep = new double[5];;// {0.0 , 0.0 , 0.0 , 0.0 , 0.0 }
    double[] wit = new double[7];// {0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }
    double[] d = dep; // {0.0 , 0.0 , 0.0 , 0.0 , 0.0 }
    double[] w = wit; // {0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }
    

    因此,在这行之后,'d' 和 'w' 将具有每个初始化为 0.0 值的元素。 在 java 中,= (assignment) 运算符从右到左工作。你在 'dep' 中的内容将被分配给 'd',因此迭代 d 和 w 将产生具有 0.0 值的元素。

    w = sac.getWit();
    d = sac.getDep();
    

    在此之后,您可以使用 'w' 和 'd' 而不是 'dep' 和 'wit' 访问这些值,因为它们仍然引用之前的数组。

    所以当你迭代时,改用下面的代码片段,

       for(int i=0;i<5;i++){
    
            System.out.println(d[i]);
        }
       for(int i=0;i<6;i++){
    
            System.out.println(w[i]);
        }
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      相关资源
      最近更新 更多