Java – 调用方法 ‘add’ 将会产生 ‘NullPointerException’

huangapple go评论68阅读模式
英文:

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:

{ &quot;rooms&quot;: [
{
&quot;roomCoordinates&quot;: [0, 0],
&quot;availableDoors&quot;: [&quot;east&quot;],
&quot;unavailableDoors&quot;:  [],
&quot;items&quot;:  []
}
]
}

and my Room object and wrapper class look as follows:

import java.util.ArrayList;
public class Room {
private int[] roomCoordinates;
private ArrayList&lt;String&gt; availableDoors;
private ArrayList&lt;String&gt; unavailableDoors;
private ArrayList&lt;String&gt; items;
public Room(int[] currentRoomCoordinates, ArrayList&lt;String&gt; currentAvailableDoors,
ArrayList&lt;String&gt; currentUnavailableDoors, ArrayList&lt;String&gt; currentItems){
this.roomCoordinates = currentRoomCoordinates;
this.availableDoors = currentAvailableDoors;
this.availableDoors = currentUnavailableDoors;
this.items = currentItems;
}
public int[] getRoomCoordinates(){
return roomCoordinates;
}
public ArrayList&lt;String&gt; getAvailableDoors(){
return availableDoors;
}
public ArrayList&lt;String&gt; ungetAvailableDoors(){
return unavailableDoors;
}
public ArrayList&lt;String&gt; getItems(){
return items;
}
}
public class GameBoard {
private List&lt;Room&gt; gameBoard = new ArrayList&lt;&gt;();
public GameBoard(int[] roomCoordinates, ArrayList&lt;String&gt; availableDoors,
ArrayList&lt;String&gt; unavailableDoors, ArrayList&lt;String&gt; items){
gameBoard.add(new Room(roomCoordinates, availableDoors, unavailableDoors, items));
System.out.println(gameBoard);
}
public Room getRoom(int i){
return gameBoard.get(i);
}
public List&lt;Room&gt; 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(&quot;src/main/java/student/adventure/Rooms.json&quot;));
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&lt;Room&gt; gameBoard = new ArrayList&lt;&gt;();

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(&quot;src/main/java/student/adventure/Rooms.json&quot;));

huangapple
  • 本文由 发表于 2020年9月11日 23:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63850131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定