I think I have it sussed. What was happening was I was splitting the response from GetMarketPricesCompressed into four elements which represented the market details and the three runners (just playing with match odds in football at the moment).
When I passed the runner details string to
private RunnerPrices getRunnerDetails(String rString)
I split that string again, ideally into three elements.
arr[0] had the runner details in it (selection ID and so on)
arr[1] has the back prices
arr[2] has the lay prices.
When I got to that part of the match where there is no money left in the market for a runner (IE, the winning team and all the 1.01s are gone) I was being thrown a NullPointer exception because arr[1] was empty. As far as the array was concerned, it was definitely a string, because the character i used to split the initial rString was there, but there were no characters in it. It probably didn't help that I was using a dodgy if statement too.
if (backP.length > 0) <-------THIS WAS A SILLY THING TO DO
Although I gave it an else statement which made sense, I forgot about the basic rule of equality in Java and didn't take into account how the operators work.
For those who are scratching their heads or those who are new to programming and happen to be reading this, the basic (somewhat simplified) premise is this:
Mathematical operators work by using comparison. To decide whether value a is equal to, greater than or less than value b, there must first be a value b.
The mistake I was making was silly, but an easy one to make I think.
In the code snippet directly above, I was asking the JVM to compare the length of a string with the value 0 and if it was greater than 0, to take the following action.
Problem is, in some cases, I was actually asking the JVM to compare the length of a non-existing string with the value 0.
The reason I was getting the null pointer exception was that the JVM was trying to compare the string, couldn't find it where I said it was and told me so.
The correct test (and the test I have included in the updated version attached) should have been like so:
if (backP != null)
I should also add that I've done a few things in here as well. For a start, I have defined a couple of strings in there which represent market prices with no cash in them. If backP == null, the code will split the nullPrice string instead of just passing it by. This means that while there is no money in the market, I still have values in there which I can work with. I have done the same with the string layP, which is the value in arr[2] I mention above.
I hope you blokes don't mind me rambling like this occasionally - I find putting it down like this reinforces the lesson in my head and it may just help other people with their problems as well.
Here is the corrected version - still messy and still has methods in there I'm not using (yet) but it isn't anything like as awkward as it has been.