私は、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'は、このコードでは、もう使用していないreadループの終わり以外ではnullにはなりません。 – EJP
@EJPええ、そういうわけで私は非常に混乱しています。 – Bennett