英文:
Java - Method invocation 'add' will produce 'NullPointerException'
问题
我正在尝试读取一个用于制作基本文本游戏的 JSON 文件,但是当我尝试将 JSON 转换为 POJO(Plain Old Java Object,纯粹的 Java 对象)时,我得到了一个警告。我的 JSON 如下所示:
{
"rooms": [
{
"roomCoordinates": [0, 0],
"availableDoors": ["east"],
"unavailableDoors": [],
"items": []
}
]
}
我的 Room 对象和包装类如下所示:
import java.util.ArrayList;
public class Room {
private int[] roomCoordinates;
private ArrayList<String> availableDoors;
private ArrayList<String> unavailableDoors;
private ArrayList<String> items;
public Room(int[] currentRoomCoordinates, ArrayList<String> currentAvailableDoors,
ArrayList<String> currentUnavailableDoors, ArrayList<String> currentItems) {
this.roomCoordinates = currentRoomCoordinates;
this.availableDoors = currentAvailableDoors;
this.unavailableDoors = currentUnavailableDoors;
this.items = currentItems;
}
public int[] getRoomCoordinates() {
return roomCoordinates;
}
public ArrayList<String> getAvailableDoors() {
return availableDoors;
}
public ArrayList<String> getUnavailableDoors() {
return unavailableDoors;
}
public ArrayList<String> getItems() {
return items;
}
}
public class GameBoard {
private List<Room> gameBoard = new ArrayList<>();
public GameBoard(int[] roomCoordinates, ArrayList<String> availableDoors,
ArrayList<String> unavailableDoors, ArrayList<String> items) {
gameBoard.add(new Room(roomCoordinates, availableDoors, unavailableDoors, items));
System.out.println(gameBoard);
}
public Room getRoom(int i) {
return gameBoard.get(i);
}
public List<Room> getGameBoard() {
return gameBoard;
}
}
我实际运行的初始化代码如下所示:
@Test
public void setUp() {
try {
Gson gson = new Gson();
Reader reader = Files.newBufferedReader(Paths.get("src/main/java/student/adventure/Rooms.json"));
GameBoard board = gson.fromJson(reader, GameBoard.class);
reader.close();
System.out.println(board.getGameBoard());
Room room = board.getRoom(0);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
我在 GameBoard 构造函数中得到一个警告,警告内容是 "Method invocation 'add' will produce 'NullPointerException'"。当我查看实际的 gameBoard 列表时,我发现它为 null,我确定这与警告有关。有人知道为什么构造函数中会调用警告吗?
英文:
I'm trying to read in a JSON file for a basic text-game I'm making, but I'm getting a warning when I try to convert the JSON into a POJO. My Json looks as follows:
{ "rooms": [
{
"roomCoordinates": [0, 0],
"availableDoors": ["east"],
"unavailableDoors": [],
"items": []
}
]
}
and my Room object and wrapper class look as follows:
import java.util.ArrayList;
public class Room {
private int[] roomCoordinates;
private ArrayList<String> availableDoors;
private ArrayList<String> unavailableDoors;
private ArrayList<String> items;
public Room(int[] currentRoomCoordinates, ArrayList<String> currentAvailableDoors,
ArrayList<String> currentUnavailableDoors, ArrayList<String> currentItems){
this.roomCoordinates = currentRoomCoordinates;
this.availableDoors = currentAvailableDoors;
this.availableDoors = currentUnavailableDoors;
this.items = currentItems;
}
public int[] getRoomCoordinates(){
return roomCoordinates;
}
public ArrayList<String> getAvailableDoors(){
return availableDoors;
}
public ArrayList<String> ungetAvailableDoors(){
return unavailableDoors;
}
public ArrayList<String> getItems(){
return items;
}
}
public class GameBoard {
private List<Room> gameBoard = new ArrayList<>();
public GameBoard(int[] roomCoordinates, ArrayList<String> availableDoors,
ArrayList<String> unavailableDoors, ArrayList<String> items){
gameBoard.add(new Room(roomCoordinates, availableDoors, unavailableDoors, items));
System.out.println(gameBoard);
}
public Room getRoom(int i){
return gameBoard.get(i);
}
public List<Room> getGameBoard(){
return gameBoard;
}
}
The code I'm actually running to initialize this is as follows:
@Test
public void setUp() {
try {
Gson gson = new Gson();
Reader reader = Files.newBufferedReader(Paths.get("src/main/java/student/adventure/Rooms.json"));
GameBoard board = gson.fromJson(reader, GameBoard.class);
reader.close();
System.out.println(board.getGameBoard());
Room room = board.getRoom(0);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
I'm getting a warning in the GameBoard constructor saying "Method invocation 'add' will produce 'NullPointerException'". When I look at the actual gameBoard List, I find that it's null and I'm sure it has to do with the warning. Does anybody now why the warning is being called in the constructor?
答案1
得分: 1
GameBoard 应该这样初始化:
private List<Room> gameBoard = new ArrayList<>();
而这个文件路径可能不是你尝试获取的确切路径。请确保你正在读取准确的文件位置。
Reader reader = Files.newBufferedReader(Paths.get("src/main/java/student/adventure/Rooms.json"));
英文:
GameBoard should be initialized with:
private List<Room> gameBoard = new ArrayList<>();
And this file path may not be the exact path you are trying to get. Try to make sure you are reading exact file location.
Reader reader = Files.newBufferedReader(Paths.get("src/main/java/student/adventure/Rooms.json"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论