-1

I have a blackberry app that essentially does a select query on information I enter so if I enter "5" it does a select * from table where id=5 so I get back results. However the else statement of this clause is supposed to return "no results found". Code posted below:

public void onResponse(String data, int code) {

    final Vector vehicleInfo = jasonParser.parseVechicleResponse(data);

    if(vehicleInfo.size() > 0 ){
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                UiApplication.getUiApplication().pushScreen(new VehicleSearchResultScreen(etfVehicleInfo.getText(),vehicleInfo));
            }
        });
    }else{
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                Dialog.alert("No Results found.");
            }
        });
    }
}

The problem with this code is if vehicleinfo.size() = 0 this else statement does not run. The code that runs is the code posted below which is my error code. Which is supposed to get called when collection errors occur. So basicallly, when a record is not found its displaying the string for connection errors, and I cannot at all figure why this else statement is not running; or does it run, and is over run by the following statment?

public void onError(final String data, int code) {
    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            removeLoader();
            Dialog.alert("No Results Found!");
        }
    });
} 

Parse Code

public Vector parseVehicleResponse(String response){
    Vector vector = new Vector();
    try{
        JSONObject jsonVehicleObject = new JSONObject(response);
        VehicleObject vehicle = new VehicleObject();
            vehicle.setChassisNum(jsonVehicleObject.getString("chassis_no"));//chassis_num
            vehicle.setEngineNum(jsonVehicleObject.getString("engine_no"));//engine_num
            vector.addElement(vehicle);
    }catch(Exception e){
        System.out.println("JSONResponseParser.parseNewsHeadlines() " + e );
    }

    return vector;
}

}

Json

  // Return  encoded with JSON
            $result = array(

    "chassis_no"=>$chassis_no,
   "engine_no"=>$engine_no,

 );
kemnet
  • 73
  • 1
  • 3
  • 11
  • You're going to need to include `parseVechicleResponse()`, since it's likely an error with the way you're querying your data. – Makoto Feb 06 '13 at 02:32
  • @Makoto isnt that what i am doing on this line? final Vector vehicleInfo = jasonParser.parseVechicleResponse(data); – kemnet Feb 06 '13 at 15:24
  • It seems both the error code and the `else` code produce the same dialog. How do you know which is which? Could you make the error code echo error description? – Floris Feb 06 '13 at 17:15
  • no they dont the error string has an "!" thats how i know – kemnet Feb 06 '13 at 18:05
  • No; what I was referring to was you'd have to include the method. There isn't anything from your current code that I could tell is incorrect without seeing the way you're querying your data. – Makoto Feb 06 '13 at 18:35
  • You sure you don't get an exception? The only way you would have entered into that other if block is if the vector was of length 0. – Makoto Feb 07 '13 at 02:18
  • @Makoto Im sure i dont get exceptions...but its very very true the only time that the other block will run if vector is 0. I dont understand – kemnet Feb 08 '13 at 12:28

2 Answers2

0

It seems to me that your jasonParser.parseVechicleResponse(data); call is throwing an error, and you never get to the if statement. Could it be a typo? Did you mean

parseVehicleResponse

instead of

parseVechicleResponse

(not the extra "c")??

Floris
  • 45,857
  • 6
  • 70
  • 122
  • @Makoto - Java will often compile fine, and at runtime, when it doesn't find the function, throw an error. That's my experience anyway. `Exception in thread "main" java.lang.NoClassDefFoundError: Test/class Caused by: java.lang.ClassNotFoundException: Test.class` – Floris Feb 06 '13 at 02:59
  • @Floris: Thanks but that didnt solve it because it was mispelled both places. ( i fixed that). Also i think it does run the if statement because it works fine if results are found...but when it tries to execte the else it sees an error and jumps to the error clause? Also im not sure why i was voted down by the question... – kemnet Feb 06 '13 at 15:23
  • Try changing the logic so you do the `else` when a result is found - to see if the `else` code itself is throwing an error... – Floris Feb 06 '13 at 17:11
  • if(vehicleInfo.size() == 0 ){ UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { // TODO Auto-generated method stub removeLoader(); // UiApplication.getUiApplication().pushScreen(new VehicleSearchResultScreen(etfVehicleInfo.getText(),vehicleInfo)); Dialog.alert("Yes."); } }); }else{ UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { // TODO Auto-generated method stub removeLoader(); Dialog.alert("NO."); } }); } } – kemnet Feb 06 '13 at 18:50
  • i did this code...and the else runs when i enter results that exist...what i realize is the problem is any time vehicleinfo.size() evaluates to 0 or no result is found it goes to the error statement ignoring the if or else – kemnet Feb 06 '13 at 18:55
  • I think you need to follow @Makoto 's advice and show us the parseVechicleResponse function, which is throwing an error when no results are returned. – Floris Feb 06 '13 at 19:52
  • It is puzzling... I would initialize the vector to one element (dummy) before going through the parse loop, change the if condition to >1, and ignore element zero. What happens then? – Floris Feb 07 '13 at 01:44
  • ok im going to try this...if(vehicleInfo.size() > 1 ){ } }); }else{} – kemnet Feb 07 '13 at 02:12
  • @Floris i said if vehicleinfo==1 then 1 else 2. if the vehicle is found is displays one...but if it isnt found...it doesnt display two...goes straight to the error. Anyhow i look at it...whenever the size if vehicleinfo is 0...is jumps to the error.not evaluation the if or else – kemnet Feb 07 '13 at 02:20
  • @Floris Im not sure how to initiate to 1....and in the same breath..say if the vector receives 1 element...wouldnt it always be one?because if u do a search and u find results...it will still be 1...not two...(Btw im open to correction) – kemnet Feb 07 '13 at 02:39
  • see my "other" answer... that should explain it. – Floris Feb 07 '13 at 02:45
0

In order to keep the flow of thoughts separated, I am starting a new answer here... You could try to modify the code for parseVehicleResponse by adding a "dummy element zero", as follows:

public Vector parseVehicleResponse(String response){
    Vector vector = new Vector();
    VehicleObject vehicle = new VehicleObject(); // create a "dummy object"
    vehicle.setChassisNum("-1"); // initialize members - with "valid" data... 
    vehicle.setEngineNum("-1");  // again - make it valid type, but otherwise "wrong"
    vector.addElement(vehicle);  // the vector now has at least one element

    // here comes your original code:
    try{
        JSONObject jsonVehicleObject = new JSONObject(response);
        VehicleObject vehicle = new VehicleObject();
            vehicle.setChassisNum(jsonVehicleObject.getString("chassis_no"));//chassis_num
            vehicle.setEngineNum(jsonVehicleObject.getString("engine_no"));//engine_num
            vector.addElement(vehicle);
    }catch(Exception e){
        System.out.println("JSONResponseParser.parseNewsHeadlines() " + e );
    }
    return vector;
}

Now, in the main loop, you do something like this:

Public void onResponse(String data, int code) {

    final Vector vehicleInfo = jasonParser.parseVechicleResponse(data);

    if(vehicleInfo.size() > 1 ){
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                // { add some code to strip the zero-th element from vehicleInfo() } 
                UiApplication.getUiApplication().pushScreen(new VehicleSearchResultScreen(etfVehicleInfo.getText(),vehicleInfo));
            }
        });
    }else{
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                Dialog.alert("No Results found.");
            }
        });
    }
}

This will narrow down the search for the problem some more...

Floris
  • 45,857
  • 6
  • 70
  • 122
  • by striping zero element u mean vehicle.setChassisNum("0"); – kemnet Feb 07 '13 at 15:44
  • I mean - use vehicle.removeElement(0) to take the dummy element out before you display the information; or, in the display routine, skip the zeroth element. – Floris Feb 07 '13 at 15:48
  • i did exactly what u said. and again... if(vehicleInfo.size() >1){ vehicleInfo.removeElement("0"); Dialog.alert("Yes."); } }); }else{ IF the number is found it shows "yes" but if not...the else doesnt run and it goes to the error clause – kemnet Feb 07 '13 at 16:30
  • OK. Leave the dummy element in place - then what happens? – Floris Feb 07 '13 at 16:32
  • ok when i comment out the removeElement values found also show "yes "but values not found goes to the error...skipping else – kemnet Feb 07 '13 at 17:17
  • Do u think there is an error when X ==0 so whenever that is evaluated it goes to the error clause? – kemnet Feb 07 '13 at 17:21
  • I suspect the error is generated in the line `JSONObject jsonVehicleObject = new JSONObject(response); ` - when `response` is empty, this crashes. What is the format of `response` - could you test for "empty" before it gets to this line? – Floris Feb 07 '13 at 17:29
  • That does make sense, however isnt the response what you asked for earlier and i posted in my first messaGE? The funny thing is i have this exact same code in another place, and it works perfectly...throwing the else when nothing is found. and erroring out when i cause an error to occur. and i see no difference in any of the code – kemnet Feb 07 '13 at 18:02
  • maybe we should say ==null instead of ==0? – kemnet Feb 09 '13 at 20:30
  • I am having trouble understanding your last two comments. Can you make the error handler print out the actual error message (I suspect it is passed in as the string...?). I still think the `response` variable is something that is your problem - and I can't find where you tell me what its value (or structure) is... You say "I posted in my first messaGE" but I can't see it. Would like to know what it is when something was found, and when it wasn't. Further piece of advice: print something after every line in the code (so you can see at what line the exception occurs if the error handler doesn't) – Floris Feb 09 '13 at 21:16
  • well you see...probably i forgot to mention but this is a blackberry app so for some reason.System.out.println("Print"); doesnt work but ive been using Dialog.alert("1."); all over the if and else statement it shows up when a size>0 not where size==0 – kemnet Feb 09 '13 at 22:49
  • Also when u ask for "response variable" dont you mean the code in "public Vector parseVehicleResponse(String response){" ? – kemnet Feb 09 '13 at 22:50
  • I don't mean the code - I mean the contents of the `response` variable that is passed to the function. – Floris Feb 09 '13 at 23:55
  • Well when you enter 9 in the search fields a query is run that selects those two fields from the db and place it in an array. (i posted it in my very first post) which i believe is passed to the function. Hope that was what you asked – kemnet Feb 10 '13 at 00:12
  • i just realized $result wasnt written right it was supposed to be $result[]...changed that but it didnt fix it – kemnet Feb 10 '13 at 00:33
  • I am asking about the `response` variable that is passed to JSON. What is its value when no results are found? – Floris Feb 10 '13 at 01:52
  • Then my bet is that the line `JSONObject jsonVehicleObject = new JSONObject(response);` fails when `response==NULL`. Test for that before that line, and return an array with zero elements. That will do the trick. – Floris Feb 10 '13 at 05:05
  • i said if (vehicleInfo != null) {. When its !=null the if statment here executes...but when it =null it skip the else and goes straight down to the error code...right back to square one – kemnet Feb 10 '13 at 05:49
  • it even skips this condition if vehicleinfo==null – kemnet Feb 10 '13 at 05:57
  • Do you think this error can actually be from the php? like i said i have a similar code...a mirror image...and it doesnt ignore 0 zalues or nulls and it actually execute the else – kemnet Feb 10 '13 at 06:36
  • I don't think you ever get to the `if` statement. The error is thrown when you parse the `NULL` string. That is the condition you must handle before calling the parser (before calling `new JSONobject(response)` – Floris Feb 10 '13 at 13:00
  • but how do i text for nulls if i dont is this `if` and...what object do i test ?data? – kemnet Feb 10 '13 at 16:09
  • The problem is in my php code if you scroll back to my orginal code you will see a block of code labelled JSON. if i have this syntax `$result = array(` then the else statement doesnt run. however if i change it to `$result[] = array(` then the else statement runs but even if a name IS found...it says it isnt and it evaluates ( vehicle.size==0 )an runs that statement without giving an error – kemnet Feb 13 '13 at 11:55
  • I don't think there is much more I can do. You should write a new question in which you summarize everything you have learned and tried, with all typos taken out, and all relevant bits of code - then chances are good someone can help. The information is too dispersed right now. Sorry. – Floris Feb 13 '13 at 13:04