Android:NullPointerException对于循环isn不在doInBackground方法中工作

时间:2014-08-21 15:11:57

标签: java android for-loop nullpointerexception

我有一个asyncTask,在方法doInBackground中我有两个for循环,一个用于我创建的每个节点列表。在第一个for循环中,它完美地运行并返回我想要的内容。尽管第二个几乎是克隆,但nodeList是唯一改变的东西,应用程序崩溃了。什么可能导致这次崩溃?

这是for循环代码:

                NodeList nl = doc.getElementsByTagName(KEY_FUEL);
                NodeList nl2 = doc2.getElementsByTagName(KEY_FUEL);


                // looping through all item nodes <item>
                for (int i = 0; i < nl.getLength(); i++) 
                {
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    HashMap<String, String> map1 = new HashMap<String, String>();
                    HashMap<String, String> map2 = new HashMap<String, String>();

                    Element e = (Element) nl.item(i);
                    // adding each child node to HashMap key => value
                    map.put(KEY_HIGHEST, "Highest units = " + parser.getValue(e, KEY_HIGHEST));
                    Log.v(TAG, "indexmap=" + map);
                    map1.put(KEY_AVERAGE, "Average units = " + parser.getValue(e, KEY_AVERAGE));
                    Log.v(TAG, "indexmi=" + map1);
                    map2.put(KEY_LOWEST, "Lowest units = " +parser.getValue(e, KEY_LOWEST));
                    Log.v(TAG, "indexmi=" + map2);

                    // adding HashList to ArrayList
                    menuItems.add(map);
                    Log.v(TAG, "indexmi=" + menuItems);
                    menuItems2.add(map1);
                    Log.v(TAG, "indexmi2=" + menuItems2);
                    menuItems3.add(map2);
                    Log.v(TAG, "indexmi3=" + menuItems3);

                    menuItemsString = menuItems.toString();
                    menuItemsString2 = menuItems2.toString();
                    menuItemsString3 = menuItems3.toString();

                }

                for (int i = 0; i < nl2.getLength(); i++)   
                {
                    Log.v(TAG, "indexnl2=" + nl2.getLength());
                    // creating new HashMap
                    HashMap<String, String> map3 = new HashMap<String, String>();
                    HashMap<String, String> map4 = new HashMap<String, String>();
                    HashMap<String, String> map5 = new HashMap<String, String>();

                    Element e = (Element) nl2.item(i);
                    // adding each child node to HashMap key => value
                    map3.put(KEY_HIGHEST, "Highest units = " + parser.getValue(e, KEY_HIGHEST));
                    Log.v(TAG, "indexmaps3=" + map3);
                    map4.put(KEY_AVERAGE, "Average units = " + parser.getValue(e, KEY_AVERAGE));
                    Log.v(TAG, "indexmaps4=" + map4);
                    map5.put(KEY_LOWEST, "Lowest units = " +parser.getValue(e, KEY_LOWEST));
                    Log.v(TAG, "indexmaps5=" + map5);

                    // adding HashList to ArrayList
                    menuItems4.add(map3);
                    Log.v(TAG, "indexmi4=" + menuItems4);
                    menuItems5.add(map4);
                    Log.v(TAG, "indexmi5=" + menuItems5);
                    menuItems6.add(map5);
                    Log.v(TAG, "indexmi6=" + menuItems6);

                    menuItemsString4 = menuItems4.toString();
                    menuItemsString5 = menuItems5.toString();
                    menuItemsString6 = menuItems6.toString();


                }

这是错误logcat:

08-21 14:56:25.611: E/Trace(1851): error opening trace file: No such file or directory (2)
08-21 14:56:46.131: E/AndroidRuntime(1851): FATAL EXCEPTION: AsyncTask #1
08-21 14:56:46.131: E/AndroidRuntime(1851): java.lang.RuntimeException: An error occured while executing doInBackground()
08-21 14:56:46.131: E/AndroidRuntime(1851):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at java.lang.Thread.run(Thread.java:856)
08-21 14:56:46.131: E/AndroidRuntime(1851): Caused by: java.lang.NullPointerException
08-21 14:56:46.131: E/AndroidRuntime(1851):     at org.me.myandroidstuff.ComparePrices$asyncTaskCompare.doInBackground(ComparePrices.java:317)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at org.me.myandroidstuff.ComparePrices$asyncTaskCompare.doInBackground(ComparePrices.java:1)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-21 14:56:46.131: E/AndroidRuntime(1851):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-21 14:56:46.131: E/AndroidRuntime(1851):     ... 3 more

这是节点列表nl2长度,表明这不是for循环中的问题:

08-21 14:56:46.131: V/ComparePrices(1851): indexnl2=4

这是用于显示代码输出的logcat。正如你所看到的那样,第一个for循环正常工作,但不知怎的,第二个循环在它应该停止之前停止:

08-21 14:56:46.131: V/ComparePrices(1851): indexmi=[{Highest=Highest units = 133.1}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi2=[{Average=Average units = 128.4}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi3=[{Lowest=Lowest units = 124.7}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi=[{Highest=Highest units = 133.1}, {Highest=Highest units = 139.9}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi2=[{Average=Average units = 128.4}, {Average=Average units = 132.8}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi3=[{Lowest=Lowest units = 124.7}, {Lowest=Lowest units = 128.7}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi=[{Highest=Highest units = 133.1}, {Highest=Highest units = 139.9}, {Highest=Highest units = 141.9}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi2=[{Average=Average units = 128.4}, {Average=Average units = 132.8}, {Average=Average units = 136.9}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi3=[{Lowest=Lowest units = 124.7}, {Lowest=Lowest units = 128.7}, {Lowest=Lowest units = 132.9}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi=[{Highest=Highest units = 133.1}, {Highest=Highest units = 139.9}, {Highest=Highest units = 141.9}, {Highest=Highest units = 69.9}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi2=[{Average=Average units = 128.4}, {Average=Average units = 132.8}, {Average=Average units = 136.9}, {Average=Average units = 69.9}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi3=[{Lowest=Lowest units = 124.7}, {Lowest=Lowest units = 128.7}, {Lowest=Lowest units = 132.9}, {Lowest=Lowest units = 69.9}]
08-21 14:56:46.131: V/ComparePrices(1851): indexmi4=[{Highest=Highest units = 132.9}]

编辑:完整的doInBackground方法

protected String[] doInBackground(String...params) 

            {

                String urlString = newFifthString;
                String urlString2 = newEighthString;
                String result = "";
                String result2 = "";
                InputStream anInStream = null;
                InputStream anInStream2 = null;
                int response = -1;
                int response2 = -1;
                URL url = null;
                URL url2 = null;

                try 
                {
                    url = new URL(urlString);
                    url2 = new URL(urlString2);
                } 
                catch (MalformedURLException e) 
                {
                    // TODO Auto-generated catch block
                    return null;
                }

                URLConnection conn = null;
                URLConnection conn2 = null;
                try 
                {
                    conn = url.openConnection();
                    conn2 = url2.openConnection();
                } 
                catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    return null;
                }

                // Check that the connection can be opened
                if (!(conn instanceof HttpURLConnection && conn2 instanceof HttpURLConnection)) 
                {
                    try 
                    {
                        throw new IOException("Not an HTTP connection");
                    } 
                    catch (IOException e) 
                    {
                        // TODO Auto-generated catch block
                        return null;
                    }
                }

                try 
                {
                    // Open connection
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    HttpURLConnection httpConn2 = (HttpURLConnection) conn2;
                    httpConn.setAllowUserInteraction(false);
                    httpConn.setInstanceFollowRedirects(true);
                    httpConn.setRequestMethod("GET");
                    httpConn.connect();
                    response = httpConn.getResponseCode();
                    httpConn2.setInstanceFollowRedirects(true);
                    httpConn2.setRequestMethod("GET");
                    httpConn2.connect();
                    response2 = httpConn.getResponseCode();
                    // Check that connection is OK
                    if (response == HttpURLConnection.HTTP_OK && response2 == HttpURLConnection.HTTP_OK ) 
                    {
                        // Connection is OK so open a reader
                        anInStream = httpConn.getInputStream();
                        anInStream2 = httpConn2.getInputStream();
                        InputStreamReader in= new InputStreamReader(anInStream);
                        InputStreamReader in2= new InputStreamReader(anInStream2);
                        BufferedReader bin= new BufferedReader(in);
                        BufferedReader bin2= new BufferedReader(in2);

                        // Read in the data from the RSS stream
                        String line = new String();
                        String line2 = new String();

                        while (( (line = bin.readLine())) != null)
                        {

                            result = result + line;
                            Log.v(TAG, "indexres=" + result);

                        }

                        while (( (line2 = bin2.readLine())) != null)
                        {

                            result2 = result2 + line2;
                            Log.v(TAG, "indexres2=" + result2);
                        }

                    }

                    menuItems = new ArrayList<HashMap<String, String>>();
                    menuItems2 = new ArrayList<HashMap<String, String>>();
                    menuItems3 = new ArrayList<HashMap<String, String>>();

                    menuItems4 = new ArrayList<HashMap<String, String>>();
                    menuItems4 = new ArrayList<HashMap<String, String>>();
                    menuItems6 = new ArrayList<HashMap<String, String>>();

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


                    // getting DOM element

                    NodeList nl = doc.getElementsByTagName(KEY_FUEL);
                    NodeList nl2 = doc2.getElementsByTagName(KEY_FUEL);


                    // looping through all item nodes <item>
                    for (int i = 0; i < nl.getLength(); i++) 
                    {
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        HashMap<String, String> map1 = new HashMap<String, String>();
                        HashMap<String, String> map2 = new HashMap<String, String>();

                        Element e = (Element) nl.item(i);
                        // adding each child node to HashMap key => value
                        map.put(KEY_HIGHEST, "Highest units = " + parser.getValue(e, KEY_HIGHEST));
                        Log.v(TAG, "indexmap=" + map);
                        map1.put(KEY_AVERAGE, "Average units = " + parser.getValue(e, KEY_AVERAGE));
                        Log.v(TAG, "indexmi=" + map1);
                        map2.put(KEY_LOWEST, "Lowest units = " +parser.getValue(e, KEY_LOWEST));
                        Log.v(TAG, "indexmi=" + map2);

                        // adding HashList to ArrayList
                        menuItems.add(map);
                        Log.v(TAG, "indexmi=" + menuItems);
                        menuItems2.add(map1);
                        Log.v(TAG, "indexmi2=" + menuItems2);
                        menuItems3.add(map2);
                        Log.v(TAG, "indexmi3=" + menuItems3);

                        menuItemsString = menuItems.toString();
                        menuItemsString2 = menuItems2.toString();
                        menuItemsString3 = menuItems3.toString();

                    }

                    for (int i = 0; i < nl2.getLength(); i++)   
                    {
                        Log.v(TAG, "indexnl2=" + nl2.getLength());
                        // creating new HashMap
                        HashMap<String, String> map3 = new HashMap<String, String>();
                        HashMap<String, String> map4 = new HashMap<String, String>();
                        HashMap<String, String> map5 = new HashMap<String, String>();

                        Element e = (Element) nl2.item(i);
                        // adding each child node to HashMap key => value
                        map3.put(KEY_HIGHEST, "Highest units = " + parser.getValue(e, KEY_HIGHEST));
                        Log.v(TAG, "indexmaps3=" + map3);
                        map4.put(KEY_AVERAGE, "Average units = " + parser.getValue(e, KEY_AVERAGE));
                        Log.v(TAG, "indexmaps4=" + map4);
                        map5.put(KEY_LOWEST, "Lowest units = " +parser.getValue(e, KEY_LOWEST));
                        Log.v(TAG, "indexmaps5=" + map5);

                        // adding HashList to ArrayList
                        menuItems4.add(map3);
                        Log.v(TAG, "indexmi4=" + menuItems4);
                        menuItems5.add(map4);
                        Log.v(TAG, "indexmi5=" + menuItems5);
                        menuItems6.add(map5);
                        Log.v(TAG, "indexmi6=" + menuItems6);

                        menuItemsString4 = menuItems4.toString();
                        menuItemsString5 = menuItems5.toString();
                        menuItemsString6 = menuItems6.toString();


                    }

                }


                catch (IOException ex) 
                {
                    try 
                    {
                        throw new IOException("Error connecting");

                    } 
                    catch (IOException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                 }
                //String[] combinationString = new String[] { result, result2 };
                String[] menuItemsCollection = new String[5];

                menuItemsCollection[0] = menuItemsString;
                menuItemsCollection[1] = menuItemsString2;
                menuItemsCollection[2] = menuItemsString3;
                menuItemsCollection[3] = menuItemsString4;
                menuItemsCollection[4] = menuItemsString5;
                menuItemsCollection[5] = menuItemsString6;


                return menuItemsCollection;

               }

1 个答案:

答案 0 :(得分:0)

初始化中存在拼写错误。您正在初始化menuItems 4两次。

改变这个:

                menuItems4 = new ArrayList<HashMap<String, String>>();
                menuItems4 = new ArrayList<HashMap<String, String>>();

到此:

                menuItems4 = new ArrayList<HashMap<String, String>>();
                menuItems5 = new ArrayList<HashMap<String, String>>();