【发布时间】: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){...}