user3071845
user3071845

Reputation: 47

Why isn't my for-loop executing?

In my try block i have a for loop which I'm certain isn't working as when I log in the for loop I get nothing back meaning in my application the loop isn't being accessed.

Here is my try block which includes the for loop:

try 
        {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();

            if (response == HttpURLConnection.HTTP_OK) 
            {
                anInStream = httpConn.getInputStream();
                InputStreamReader in= new InputStreamReader(anInStream);
                BufferedReader bin= new BufferedReader(in);

                String line = new String();
                while (( (line = bin.readLine())) != null)
                {

                    result = result + line;
                }
            }

                    menuItems = new ArrayList<HashMap<String, String>>();
                    Log.v(TAG, "index1=" + menuItems);

                    Handler parser = new Handler();
                    String xml = result; // getting XML
                    Document doc = parser.getDomElement(xml);

                    NodeList nl = doc.getElementsByTagName(KEY_FUEL);
                    Log.v(TAG, "index2=" + nl);

                    for (int i = 0; i < nl.getLength(); i++) 
                    {
                        Log.v(TAG, "index3=" + nl);

                        HashMap<String, String> map = new HashMap<String, String>();
                        Element e = (Element) nl.item(i);

                        map.put(KEY_HIGHEST, parser.getValue(e, KEY_HIGHEST));
                        map.put(KEY_AVERAGE, parser.getValue(e, KEY_AVERAGE));
                        map.put(KEY_LOWEST, parser.getValue(e, KEY_LOWEST));
                        map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
                        Log.v(TAG, "index4=" + map);

                        menuItems.add(map);
                        Log.v(TAG, "index5=" + menuItems);
                    }
        }
        catch (IOException ex) 
        {
            try 
            {
                throw new IOException("Error connecting");
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
         }
        return menuItems;

i added the catch and return for completeness incase perhaps these are the cause. Any help would be appreciated.

Upvotes: 0

Views: 168

Answers (1)

chiastic-security
chiastic-security

Reputation: 20520

Have you checked whether your NodeList is empty? If your getElementsByTagName call returns an empty list, then the length of it will be 0, and your for loop will be skipped because the termination condition will be met before the first iteration. Try logging the length of nl.

Upvotes: 1

Related Questions