我使用以下代码创建HTTP GET请求:
URL obj = new URL(pl.getConfig().getString("Database URL") + player.getName());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
Logger.getLogger("Minecraft").info(inputLine); //Logs null
Gson gsonData = new Gson();
MinecladJson jsonData = gsonData.fromJson(inputLine, MinecladJson.class); //Returns null (because inputLine is null)
PlayerData jsonPlayerData = jsonData.getPlayer(); //Crash here
不幸的是,变量inputLine
似乎为null,导致我的代码中出现NullPointerException。更糟糕的是,该地址在我的浏览器中工作正常并返回:
{"player":{"balance":0,"permissions":["ultracosmetics.recievechest","ultracosmetics.openmenu"],"stats":{"playerKills":0,"deaths":0,"minutesPlayed":0},"achievements":{"firstJoin":false},"joinMessage":"&8[&9&l+&r&8] %r&7%p","leaveMessage":"&8[&9&l-&r&8] %r&7%p","rank":"default","firstJoin":"2016-07-01T02:37:14.458Z"},"content":{"chatFormat":"%r&7%p: %m","chatCensor":["testcensor"],"chatPrefixes":{"mage":"&5MAGE ","warrior":"&3WARRIOR ","chief":"&2CHIEF ","sage":"&7SAGE ","default":"","developer":"&9&lDEV ","moderator":"&6MOD "},"currencyName":"Coins","saveRate":30000,"cacheResetRate":1200000,"allowedCharacters":"abcdefghijklmnopqrstuvwxyz0123456789!~*.,/][()=+@#$%\"';:_`^&{}\\|<>?","tabHeader":"&8&lMineclad &r&9Network","tabFooter":"&7Mineclad.co"},"uuid":"559f6d32c2b740c9bc843dd54a55b39c"}
堆栈跟踪:
[09:16:54 WARN]: java.lang.NullPointerException
[09:16:54 WARN]: at com.techurify.minecladPlugin.event.player.PlayerJoinListener.onPlayerJoin(PlayerJoinListener.java:56)
[09:16:54 WARN]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[09:16:54 WARN]: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[09:16:54 WARN]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[09:16:54 WARN]: at java.lang.reflect.Method.invoke(Method.java:483)
[09:16:54 WARN]: at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300)
[09:16:54 WARN]: at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
[09:16:54 WARN]: at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501)
[09:16:54 WARN]: at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.PlayerList.onPlayerJoin(PlayerList.java:317)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.PlayerList.a(PlayerList.java:144)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.LoginListener.b(LoginListener.java:115)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.LoginListener.E_(LoginListener.java:53)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.NetworkManager.a(NetworkManager.java:224)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.ServerConnection.c(SourceFile:187)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:762)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:360)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:646)
[09:16:54 WARN]: at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:550)
[09:16:54 WARN]: at java.lang.Thread.run(Thread.java:745)
我在想:
答案 0 :(得分:1)
我相信而不是
MinecladJson jsonData = gsonData.fromJson(inputLine, MinecladJson.class);
你想要
MinecladJson jsonData = gsonData.fromJson(response.toString(), MinecladJson.class);
^^^^^^^^^^^^^^^^^^^
您已将个人回复行累积到response
,但之后尝试从inputLine
中提取对象,此时该对象始终为null
。
答案 1 :(得分:1)
您正在记录inputLine
,但我认为您要记录response
。
为了让while循环inputLine
必须为null,因此当你打印inputLine
时,我很难得到你的结果:
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
// leave while loop if inputLine == in.readLine() == null
response.append(inputLine);
}
in.close();
Logger.getLogger("Minecraft").info(inputLine); //Logs null