【问题标题】:"incompatible types: unexpected return value" when my method isnt void and has a return“不兼容的类型:意外的返回值”当我的方法不是 void 并且有返回值时
【发布时间】:2017-04-26 02:44:48
【问题描述】:

我正在尝试将我的 into 分配给 java 类,其中此方法从文本文件中读取对象数组并返回它们,但我遇到了不兼容的类型:此方法出现意外返回值错误:

public readContacts(String fileName){
    Contact[] fileContents;
    try(ObjectInputStream fileIn = new ObjectInputStream(
    new FileInputStream(fileName)))
    {
        while (true)
        {
            fileContents = (Contact[]) fileIn.readObject();
            return fileContents;
        }
    }
    catch (EOFException e)
    {}
    catch (Exception e)
    {}
}

我看到的所有其他与此错误有关的问题,有人将方法设置为 void put 仍然输入了返回值。显然这不适用于我的情况。

【问题讨论】:

  • 首先将返回类型添加到您的方法声明中: public Array readContacts(String fileName){...}

标签: java methods types return


【解决方案1】:

您没有指定函数的数据类型。

Contact [] readContacts(String fileName) {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 2011-07-13
    • 2019-04-28
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多