• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help parsing a file (JSON) with an array  RSS feed

 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone! I have a file that I have saved where I was able to capture data that contained player names, locations, and some extra information. I'm trying to figure out exactly how I can parse it. I've read through a few tutorials but nothing seems to quite work. This is the output that I'm looking for in the end:

Player (X, Y) - Level 34
Player2 (X, Y) - Level 22
etc..

Here's the code that I currently have:



*Note: Even this throws a Null Pointer Exception!

Here's the pastebin for the contents of the .JSON file: http://pastebin.com/v4kAaspn

Not every iteration in the hexes field in the file contains player_name and player_level, but those are the only one's that I'm trying to also get the hex_x, and hex_y from as well! I've been playing with this for a few days and I've reached a mental roadblock. Still pretty new to a lot of things but this has really captured my attention. I'd greatly appreciate your time in pointing me into the right direction. Thank you so much!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the exact error you're getting, including the full stack trace. Without that I can only guess what the problem might be.
 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:Can you post the exact error you're getting, including the full stack trace. Without that I can only guess what the problem might be.


Absolutetly!

Exception in thread "main" java.lang.NullPointerException
at ToolMain.main(ToolMain.java:34)

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Bushnell wrote:
Mike. J. Thompson wrote:Can you post the exact error you're getting, including the full stack trace. Without that I can only guess what the problem might be.


Absolutetly!

Exception in thread "main" java.lang.NullPointerException
at ToolMain.main(ToolMain.java:34)



Well that error is telling you that someone on line 34 is null. Unfortunately the code you have posted must be formatted differently to the code you're running because line 34 of the code I can see does not correspond with an executable line of code. Which line corresponds with line 34 as you see it in your editor?
 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm very sorry about that!

The culprit:

Exception in thread "main" java.lang.NullPointerException
at ToolMain.main(ToolMain.java:26)
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Bushnell wrote:I'm very sorry about that!

The culprit:

Exception in thread "main" java.lang.NullPointerException
at ToolMain.main(ToolMain.java:26)


Well this means that 'msg' must be null when you execute that line.

If you have a look at where you assign the value to msg, you can see it depends on the return of jsonObject.get("hexes"), which is looking for the item called "hexes" at the root of the JSON document. However "hexes" is not at the root of the document, it's under "resonses". I imagine you need to reference it with something like "responses.hexes".
 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
If you have a look at where you assign the value to msg, you can see it depends on the return of jsonObject.get("hexes"), which is looking for the item called "hexes" at the root of the JSON document. However "hexes" is not at the root of the document, it's under "resonses". I imagine you need to reference it with something like "responses.hexes".


I had a gut feeling it may be related to that. I have no idea how to work around it either. I'll keep doing some research now that I know what the problem is! If I find anything I'll share it here too if someone else doesn't have a solution first. Thanks again Mike for all of your help!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well there are two possible options. Either:

1) The library supports a compound reference, such as "references.hexes" to reference something that isn't at the top level.

2) You will have to call get(...) multiple times to drill down into the data.


Incidentally, there is a method called getJsonObject(String key) that will return a JsonObject rather than an Object. you should use that when you know the value for the key you're getting is going to be more JSON. That way you will avoid needing to perform the cast.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!