即使已初始化,仍出现 "Variable data not initialized" 错误
Getting "Variable data not initialized" error even though it's initialized
我有一段时间没有接触Java,所以我对一些细节有点生疏。
我正在尝试从一个充满数字的文件中读取所有的数字。文件中的第一个数字告诉我文件中还有多少其他数字,以便我可以适当调整数组大小。我试图获取这些数字并将它们放入一个整数数组中,但我的 return 语句中不断出现 "error: variable data might not have been initialized"。我知道这必须是一些简单的事情,但我无法终生找出我做错了什么简单的事情。
public static int[] numbers(String filename)
{
int[] data;
try
{
FileReader input = new FileReader(filename);
BufferedReader buffer = new BufferedReader(input);
int arraySize = Integer.parseInt(buffer.readLine());
data = new int[arraySize];
for (int x = 0; x < arraySize; x++)
{
data[x] = Integer.parseInt(buffer.readLine());
}
buffer.close();
}
catch(Exception e)
{
System.out.println("Error reading: "+e.getMessage());
}
return data;
}
如果 try
块中抛出异常,则 data
返回时可能尚未初始化。
在声明时将其初始化为某些东西,即使值为 null
,以满足编译器的要求。
另一方面,看起来 IOException
是这里唯一抛出的异常。或者,您可以将您的方法声明为抛出 IOException
,并删除 try
-catch
块,以便在 return
语句时始终初始化 data
被执行。您当然需要在调用 numbers
方法的方法中捕获异常。
您收到此错误是因为您正在 try 块内初始化 data 数组,但如果您的 try 块捕获异常,则 data 数组可能未初始化,但无论如何都会返回。
在 try-catch 之外初始化您的数组:
例如:
int[] data = new int[1]; //default initialization
这是由于 try/catch 块中的初始化,如果在 try/catch 块中初始化之前抛出异常,则数组可能永远不会被实例化。
我认为只需在声明时将数组长度设置为 1 或 null 即可解决您的问题
int[] data = new int[1];
try
// the rest
或
int[] data = null;
我有两个建议与其他答案略有不同(仅略有不同)。如果有异常,那么您应该 1) 抛出异常(如 RuntimeException
)或 2)仅 return null
(我的猜测是您不希望出现异常实际上被抛出)。
public static int[] numbers(String filename)
{
BufferedReader buffer = null;
try
{
final FileReader input = new FileReader(filename);
BufferedReader buffer = new BufferedReader(input);
final int arraySize = Integer.parseInt(buffer.readLine());
final int[] data = new int[arraySize];
for (int x = 0; x < arraySize; x++)
{
data[x] = Integer.parseInt(buffer.readLine());
}
buffer.close();
return data;
}
catch(Exception e)
{
System.out.println("Error reading: "+e.getMessage());
}
finally{
// a NumberFormatException may be thrown which will leave
// the buffer open, so close it just in case
if(buffer != null)
buffer.close();
}
// else there was an exception, return null
return null;
}
我有一段时间没有接触Java,所以我对一些细节有点生疏。
我正在尝试从一个充满数字的文件中读取所有的数字。文件中的第一个数字告诉我文件中还有多少其他数字,以便我可以适当调整数组大小。我试图获取这些数字并将它们放入一个整数数组中,但我的 return 语句中不断出现 "error: variable data might not have been initialized"。我知道这必须是一些简单的事情,但我无法终生找出我做错了什么简单的事情。
public static int[] numbers(String filename)
{
int[] data;
try
{
FileReader input = new FileReader(filename);
BufferedReader buffer = new BufferedReader(input);
int arraySize = Integer.parseInt(buffer.readLine());
data = new int[arraySize];
for (int x = 0; x < arraySize; x++)
{
data[x] = Integer.parseInt(buffer.readLine());
}
buffer.close();
}
catch(Exception e)
{
System.out.println("Error reading: "+e.getMessage());
}
return data;
}
如果 try
块中抛出异常,则 data
返回时可能尚未初始化。
在声明时将其初始化为某些东西,即使值为 null
,以满足编译器的要求。
另一方面,看起来 IOException
是这里唯一抛出的异常。或者,您可以将您的方法声明为抛出 IOException
,并删除 try
-catch
块,以便在 return
语句时始终初始化 data
被执行。您当然需要在调用 numbers
方法的方法中捕获异常。
您收到此错误是因为您正在 try 块内初始化 data 数组,但如果您的 try 块捕获异常,则 data 数组可能未初始化,但无论如何都会返回。
在 try-catch 之外初始化您的数组:
例如:
int[] data = new int[1]; //default initialization
这是由于 try/catch 块中的初始化,如果在 try/catch 块中初始化之前抛出异常,则数组可能永远不会被实例化。
我认为只需在声明时将数组长度设置为 1 或 null 即可解决您的问题
int[] data = new int[1];
try
// the rest
或
int[] data = null;
我有两个建议与其他答案略有不同(仅略有不同)。如果有异常,那么您应该 1) 抛出异常(如 RuntimeException
)或 2)仅 return null
(我的猜测是您不希望出现异常实际上被抛出)。
public static int[] numbers(String filename)
{
BufferedReader buffer = null;
try
{
final FileReader input = new FileReader(filename);
BufferedReader buffer = new BufferedReader(input);
final int arraySize = Integer.parseInt(buffer.readLine());
final int[] data = new int[arraySize];
for (int x = 0; x < arraySize; x++)
{
data[x] = Integer.parseInt(buffer.readLine());
}
buffer.close();
return data;
}
catch(Exception e)
{
System.out.println("Error reading: "+e.getMessage());
}
finally{
// a NumberFormatException may be thrown which will leave
// the buffer open, so close it just in case
if(buffer != null)
buffer.close();
}
// else there was an exception, return null
return null;
}