java - 错误 “double cannot be dereferenced”是什么意思?

标签 java arraylist compiler-errors double

我了解ArrayList不能保存任何原始数据
但是如何将我的方法horseFeed()与Arraylist构造函数一起调用到驱动程序中,这样我就不会遇到双重取消引用的错误

也有人可以向我解释什么是双重取消引用的错误以及为什么我得到它,请帮助

该方法在我类

public class horse
{
  .
  .
  .


  //took out a lot of code as it was not important to the problem

  public String horseFeed(double w)
  {
    double sFeed= w*.015;
    double eFeed= w*.013;
    String range = sFeed + " < " + eFeed;
    return range;
  }
}

这是ArrayList类
import java.util.*;
public class horseStable
{
 . 
 .
 . 
 public double findHorseFeed(int i)
 {
   double weight = horseList.get(i).getWeight();
   return weight;

  }
}

这是司机
public class Main 
{
  public static void main(String args[])
  {
  //returns the weight of the horse works fine
  System.out.println(stable1.findHorseFeed(1)); 
  // This is supposed to use the horseFeed method in the class by using the horse's weight. Where can i place the horseFeed method without getting an error?
  System.out.println(stable1.findHorseFeed(1).horseFeed()); 
  }
 }

最佳答案

该错误意味着您试图在double值上调用方法-在Java中,double是原始类型,您不能在其上调用方法:

stable1.findHorseFeed(1).horseFeed()
             ^               ^
      returns a double   can't call any method on it

您需要使用正确的参数在正确的对象上调用该方法,如下所示:
Horse aHorse = new Horse(...);
aHorse.horseFeed(stable1.findHorseFeed(1));

方法horseFeed()Horse类中,它接收类型为double的参数,该参数由findHorseFeed()类中的方法HorseStable返回。显然,首先您需要创建一个Horse类型的实例来调用它。

另外,请遵循类名以大写字母开头的约定。

关于java - 错误 “double cannot be dereferenced”是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319280/

相关文章:

java - 在 Java 中将字符串转换为 ByteBuffer

java - 从字符 ArrayList 中删除一个字符

c - 为什么我缺少终止“字符?

c# - 如何使用Microsoft.CodeAnalysis在内存中进行编译并获取程序集

node.js - 无法识别 Node

java - Spring Security的permitAll不适用于某些端点

java - 访问枚举变量/方法

java - 为什么Eclipse认为app engine sdk jar是app engine SDK所在的目录?

java - String.substring(i,j) 方法获取空格而不是实际字符

java - 如何返回正确类型的列表?