1

I'm trying to write a code that I can try a JSON object and if it's the wrong format inform the user somehow.

The code is :

 public boolean sjekkSporingsNummer (JSONObject object){       //object is passed correct
    Boolean riktigSporing = null; 
    riktigSporing = true;                                     //riktig sporing is set to true
    //if its true the json is correct
    try {
    JSONArray consignmentSet = object.getJSONArray("consignmentSet");
    JSONObject object1 = consignmentSet.getJSONObject(0);
    riktigSporing = true;
    }catch (Exception e){                                 //skips straigt to here
        e.printStackTrace();
        riktigSporing = false;
    }
    return riktigSporing;

After if failes with :

07-31 12:34:07.243  15479-15479/com.example.posten E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException

What seems wierd to me is that my app skips the try and goes straight to the return statement.

I would like it to try and of it failes set "riktigSporing" to false.

What am I doing wrong?

Vahid Boreiri
  • 3,418
  • 1
  • 19
  • 34
Dukes
  • 342
  • 1
  • 3
  • 14
  • How can you know that it skips the try? Trusting a debugger is not proof; it can mess up stepping through the function easily. How about moving the object declarations out of the try and see if something was parsed at the point of return? Lykke til med sporingen :) – oligofren Jul 31 '13 at 10:22
  • Its likely that catch with JSONException is executed. – Kartik Domadiya Jul 31 '13 at 10:23
  • how did you know that your app skipped the try block? – Gladiator Jul 31 '13 at 10:24
  • I have tried using a debugger and also trie to put in a toast message but none of them show'd that it stopped in the try. – Dukes Jul 31 '13 at 10:24
  • I don't understand what the toast message thing is. Is that something like System.out.println()? Try using unbuffered printing by using System.err.println(). – oligofren Jul 31 '13 at 10:27
  • @oligofren - are you suggesting that System.out/err buffer differently ? If so, can you publish a reference to this, please ? (or perhaps I'm misunderstanding you!) – Brian Agnew Jul 31 '13 at 10:36
  • @BrianAgnew stderr does not _have_ to be unbuffered, but it is the convention of the implementations: See this answer for more explanation and several references: http://stackoverflow.com/a/9146264/200987 – oligofren Jul 31 '13 at 11:12
  • @oligofren - interesting. Thx – Brian Agnew Jul 31 '13 at 11:14

4 Answers4

2

I bet it doesn't. I suspect your input parameter is null and you trigger an NPE very early.

Note also you could possibly unbox a null into a primitive boolean, and that won't work.

EDIT:

FATAL EXCEPTION: main
java.lang.NullPointerException

suggests this further. You should have a corresponding line number in your stacktrace.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

You need to set your boolean in this catch:

catch (JSONException e) {
        e.printStackTrace();
        riktigSporing = false;
    }

In the case of some error with JSON this catch will be called, not second one. I think in your scenario your second catch is not required.

If you want to use generally Exception, remove JSONException and let there only Exception

Note: When i looked at your snippet of code, i suggest you to use boolean as primitive data-type instead of Boolean. It will be more clear and efficient.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
1

Most probably you get an exception at the first line in the try block.

I think there is a better approach to code this method. Do not use boolean to show how successful was this method execution. If this method executes ok, then return nothing. But if there's an error upon execution, throw an exception specific to the error you have got.

Avoid use of general exception classes where possible. Use exception classes specific to a sitiation which caused the problem. I.e NullPointerException, JSONException, NumberFormatException and so on. If there's a custom specific possible reason or reasons for exceptions then make your custom exception class and throw it when necessary to mark the problem as precise as possible.

It will let the caller process this error properly or throw this exception further.

In general this approach makes your application more consistent and manageable.

0

It is impossible. I think try block is executed without a exception thrown and thus it must go to return statement. Or else your code might not have build properly.

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50