peter pham

Greenhorn
+ Follow
since Aug 14, 2013
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by peter pham

Paul Clapham wrote:

peter pham wrote:I understand completely what's happening. I am consuming a publicly available API. I am getting stores in a zip code. 18/20 of the stores have all the json attributes, the other 2 don't. Why? I don't know and I'm not supposed to know, a stranger provided this data. The problem is as simple as the one json attribute, say "distance", is not available for store #10, so an exception is thrown and the parsing of the other json attributes is not accomplished.



So catch the exception and do whatever your business requirements say should be done if the distance is missing.

That's assuming you can catch the exception and identify specifically what attribute was missing. I have no idea whether you can do that or not because you have avoided posting any detailed information which tells us that. My guess is that it's the line of code Double sDist = sLoc.getDouble("distance"); which is throwing the exception, in which case you could certainly catch the exception there, but even when I asked you about the stack trace you didn't respond to that.



"It says org.json.JSONException: no value for distance". That is the root of the problem that I've determined by reading the stacktrace. I could show you the whole thing in its thread handler and zygote glory, but that's not useful. I then posted the code, which I consider "detailed information".

I can catch it if the distance is missing, but what if the street is missing and the distance is not? What can I do to avoid different nested catch blocks?
6 years ago
I understand completely what's happening. I am consuming a publicly available API. I am getting stores in a zip code. 18/20 of the stores have all the json attributes, the other 2 don't. Why? I don't know and I'm not supposed to know, a stranger provided this data. The problem is as simple as the one json attribute, say "distance", is not available for store #10, so an exception is thrown and the parsing of the other json attributes is not accomplished.

The solution seems difficult since it poses the question: "what if X attribute is missing but Y and Z aren't", or "what if X and Y are missing but Z isn't"? The point is I want to code as if I were looking at the json and able to create store objects manually, placing placeholder or default values for attributes that weren't given by whoever wrote the API. It doesn't matter why the data isn't available, or if it should be a "specific type of store", I don't have control of that. When writing a software application you don't have control over ALL the moving parts.
6 years ago

Paul Clapham wrote:If you're getting a JSONParseException, that sounds to me like more than just a missing attribute. I would suggest you look at the JSON which doesn't parse and see what the problem is with it.



It says org.json.JSONException: no value for distance



Somewhere in that function a json attribute might be missing. It isn't missing the other times it was called for other "currentStoreJSON" values, but in this case it is.

What is the best way to structure the code so that if an attribute like "distance" is missing, the store is instantiated with the other values from the json, and the "distance" attribute is any placeholder value?

Currently the exception will be caught so values present in the json such as "address" will not be used, and this function returns null.
6 years ago

Winston Gutkowski wrote:

peter pham wrote:I have an android application that consumes a REST API and parses a JSON response. Sometimes the json response is unexpected. For example, if an attribute is missing one day, this could lead to a NPE if not handled properly. To resolve this, I check to see if the parsed value is null to avoid the app crashing from an NPE. That's a lot of if statements in my try block, one for each parsed variable Is there a better way to go about this?


Probably, but first we'd need to know:
1. What version of Java is this Android app using?
2. (probably more important) What do you plan to do if you do get a null value?

And it's that last one that's likely to be the toughest to answer, because there may be more than one depending on which value (or values) is/are null.

If you could also tell us a bit more about why a response might be "unexpected", or why an attribute might be "missing", it might help us to advise. Indeed, if you could tell what this app is doing, you might get responses from people who've been through the same problem.

Winston



1. Java 1.7
2. I have a List of "Store" objects parsed from json. Most of the store objects have an attribute in the json called "years", but one doesnt. When this store's json is parsed, a JSONParseException is caught in the generic Exception e catch block. This store never made it to the line of code to create a store object before the exception was thrown, and null is returned. I want to display the years, so later on I concatenate this int with a string, NPE occurs, and the app crashes.
6 years ago
I have an android application that consumes a REST API and parses a JSON response. Sometimes the json response is unexpected. For example, if an attribute is missing one day, this could lead to a NPE if not handled properly. To resolve this, I check to see if the parsed value is null to avoid the app crashing from an NPE. That's a lot of if statements in my try block, one for each parsed variable Is there a better way to go about this?
6 years ago

Campbell Ritchie wrote:Why have you got <= in that for loop?
Why are you mixing Java and pseudo‑code in the same block?
We cannot tell how you should make that list; start by telling us what the algorithm is.



because statefulPath is supposed to be exactly what the name entails, the state of path through recursion. Path is a reference, which is why I created a new list called statefulPath to simulate what i wanted. The algorithm is to store all paths from root to leaf into listOfLists. the only pseduocode I had was the condition for making a link, whick is irrelevant to the question I am asking.
6 years ago


I have similar code that I stepped through in a debugger. After running the code I found that it built the desired tree, in this case a root node H with left child L and right child P. I want list of lists to contain all paths from root to leaf. I expected [H, L] and [H, P]. I discovered that statefulPath is not stateful; after a recursive stack frame pops, statefulPath still contains n! But that stack frame just popped! I expected to see statefulPath be [H] in the debugger and it was [H, L]! So I later have a list [H,L,P] which I don't want. How do I make the statefulPath list I want for my algorithm? Thanks
6 years ago

Paul Clapham wrote:It's the BufferedWriter which is clearing your file, not the reader.



Thanks that was it.
7 years ago
Followed this: http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/



In the constructor I create the BufferedReader using the FileReader and path to the input text file(in same dir as this project). When I am done reading I close it. In the debugger it is unable to read a single line, then goes to close the file.
7 years ago
I'm going through this tutorial: http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html and http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

I'm building a server that just accepts client messages, so basically a subset of the first link's example. I am developing in eclipse. I ran this project and the program hangs when the server socket tries to accept a connection; why?



and here is my test program, the client which sends messages to the server:
7 years ago

Jesper de Jong wrote:What is the return type of the method that you're calling via reflection (or are you calling different methods)? Can you show us your code?



It can be Cat[], Pizza[], etc, an array of references. Here's some code:


java reflection's invoke method returns an Object. At runtime, invoke here produces an array of objects(reference types). Because oneMethod becomes a different method with each iteration, this array of objects changes too. So at runtime objectArray could be Cat[], then Pizza[], then Student[], etc. My problem is that I need to iterate through this array(to call that specific object's getters, like getTailLength() for a Cat, getCheeseType() for Pizza, etc). However, the javadoc says invoke can only return an object type. I tried casting objectArray:

but got the ClassCastException: [LCat; cannot be cast to java.lang.iterable
7 years ago
I am calling java reflection api's "invoke(instance, null)" method, which returns an Object type. In my case at runtime this Object specifically is an array of a reference type. This reference type will change since I am calling invoke within a loop. For example, the first iteration would produce Cat[], the second iteration would produce Spaceship[], the third iteration would produce Pizza[], etc. I need to iterate through this array that I get back. When I try to convert the object I get back from invoke I get a "ClassCastException: [LCat; cannot be cast to java.lang.Iterable". How do I fix this, get an iterator from an Object that will always be a reference array at runtime? Thanks
7 years ago