我一直在Java中得到“双重不能解除引用”

时间:2013-10-08 20:25:25

标签: java macos

我必须编写一个程序,记录2D阵列中5个不同商店的12个月利润。我让构造函数接受了利润的输入。当我尝试编译时,我的totalProfit方法存在问题。它说'double'不能被解除引用'并突出显示我的第一个for循环的.length部分。

import java.util.*;
public class Profits
{
    static private double[][] profit=new double[5][12];
    private Scanner in=new Scanner(System.in);
    public static void main(String[] args){
        System.out.println("Please input your profits, each month at a time.");
        Profits year11=new Profits();
        System.out.println(Arrays.deepToString(profit));
    }
    public Profits(){
        for(int b=0; b<profit.length; b++){
            for(int m=0; m<profit[0].length; m++){
                profit[b][m]=in.nextDouble();
            }
        }    
    }
    public double totalProfit(){
        double profit=0.0;
        for(int b=0; b<profit.length; b++){
            for(int m=0; m<profit[0].length; m++){
                profit+=profit[b][m];
            }
        }  
        return profit;
   }

}

1 个答案:

答案 0 :(得分:8)

您已声明类型为double的局部变量,其名称与double[][]数组相同。

double profit=0.0;

变量profit现在隐藏了实例变量。

  • 更改变量的名称 - 首选。
  • 或使用profit - &gt;限定对this数组的访问权限this.profit.lengththis.profit[0].length - 只是为了完成答案。