Cannot invoke "java.io.Reader.close()" because "reader" is null
时间: 2023-11-09 22:10:15 浏览: 228
This error message indicates that you are trying to call the `close()` method on a `Reader` object that has not been initialized. In other words, the `reader` variable is null, meaning it does not refer to an actual `Reader` object.
To fix this error, you need to ensure that the `reader` variable is properly initialized before calling any methods on it. You can do this by creating a new `Reader` object and assigning it to the `reader` variable, like this:
```
Reader reader = new FileReader("myfile.txt");
// Use the reader object here
reader.close(); // Make sure to close the reader when you're done with it
```
Alternatively, you can use a try-with-resources statement to automatically close the `Reader` object when you're done with it, like this:
```
try (Reader reader = new FileReader("myfile.txt")) {
// Use the reader object here
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)