我用下面的代碼創建一個HTTP GET請求:HTTP GET請求返回Null? (JAVA)
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
顯示爲空值,在我的代碼導致一個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":"abcdefghijklmnopqrstuvwxyz!~*.,/][()[email protected]#$%\"';:_`^&{}\\|<>?","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)
我想知道:
- 爲什麼發生這種情況
- 而如何修復它
請出示完整的堆棧跟蹤,並指示代碼行發生異常的地方。 –
在此代碼中,'inputLine'不可能爲null,而是在讀取循環結束時,您不再使用它。 – EJP
@EJP是的,這就是爲什麼我很困惑。 – Bennett