Bennett Bennett - 4 months ago 27
JSON Question

HTTP GET Request Returning Null? (Java)

I am using the following code to create an HTTP GET request:

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


Unfortunately, the variable
inputLine
appears to be null, causing a NullPointerException in my code. Even worse, the address works fine in my browser and returns this:

{"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"}


The stack trace:

[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)


I was wondering:


  1. Why this is happening

  2. And how to fix it


Answer

You are logging inputLine but I assume you want to log response.

For leaving the while loop inputLine has to be null, hence it makes me no wonder that you get that result when you print 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
Comments