Java Android:NullPointerException For loop是';t在doInBackground方法中工作
我有一个asyncTask,在方法doInBackground中我有两个for循环,一个用于我创建的每个节点列表。在第一个for循环中,它工作得很好,返回的正是我想要的。即使第二个几乎是克隆,节点列表是唯一改变的东西,应用程序还是崩溃了。是什么导致了这次坠机 以下是for循环代码:Java Android:NullPointerException For loop是';t在doInBackground方法中工作,java,android,for-loop,nullpointerexception,Java,Android,For Loop,Nullpointerexception,我有一个asyncTask,在方法doInBackground中我有两个for循环,一个用于我创建的每个节点列表。在第一个for循环中,它工作得很好,返回的正是我想要的。即使第二个几乎是克隆,节点列表是唯一改变的东西,应用程序还是崩溃了。是什么导致了这次坠机 以下是for循环代码: NodeList nl = doc.getElementsByTagName(KEY_FUEL); NodeList nl2 = doc2.getEle
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();
}
以下是节点列表nl2的长度,表明这不是for循环中的问题:
08-21 14:56:46.131: V/ComparePrices(1851): indexnl2=4
下面是显示代码输出的logcat。正如您所看到的,第一个for循环工作正常,但在第二个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;
}
protectedstring[]doInBackground(String…params)
{
字符串urlString=newFifthString;
字符串urlString2=newEighthString;
字符串结果=”;
字符串result2=“”;
InputStream=null;
InputStream2=null;
int响应=-1;
int-response2=-1;
URL=null;
URL url2=null;
尝试
{
url=新url(url字符串);
url2=新URL(urlString2);
}
捕获(格式错误)
{
//TODO自动生成的捕捉块
返回null;
}
URLConnection conn=null;
URLConnection conn2=null;
尝试
{
conn=url.openConnection();
conn2=url2.openConnection();
}
捕获(IOE异常)
{
//TODO自动生成的捕捉块
返回null;
}
//检查连接是否可以打开
if(!(HttpURLConnection的conn instanceof HttpURLConnection和&conn2 instanceof HttpURLConnection))
{
尝试
{
抛出新IOException(“非HTTP连接”);
}
捕获(IOE异常)
{
//TODO自动生成的捕捉块
返回null;
}
}
尝试
{
//开放连接
HttpURLConnection httpConn=(HttpURLConnection)conn;
HttpURLConnection httpConn2=(HttpURLConnection)conn2;
httpConn.setAllowUserInteraction(假);
httpConn.setInstanceFollowRedirects(真);
httpConn.setRequestMethod(“GET”);
httpConn.connect();
response=httpConn.getResponseCode();
httpConn2.setInstanceFlowRedirects(true);
httpConn2.setRequestMethod(“GET”);
httpConn2.connect();
response2=httpConn.getResponseCode();
//检查连接是否正常
if(response==HttpURLConnection.HTTP\u OK&&response2==HttpURLConnection.HTTP\u OK)
{
//连接正常,请打开一个读卡器
anInStream=httpConn.getInputStream();
anInStream2=httpConn2.getInputStream();
InputStreamReader in=新的InputStreamReader(anInStream);
InputStreamReader in2=新的InputStreamReader(InStream2);
BufferedReader bin=新的BufferedReader(in);
BufferedReader bin2=新的BufferedReader(in2);
//从RSS流中读入数据
字符串行=新字符串();
String line2=新字符串();
而(((line=bin.readLine())!=null)
{
结果=结果+行;
Log.v(标签,“indexres=”+结果);
}
而(((line2=bin2.readLine())!=null)
{
结果2=结果2+第2行;
Log.v(标签“indexres2=“+result2”);
}
}
menuItems=newarraylist();
menuItems2=新的ArrayList();
menuItems3=新的ArrayList();
menuItems4=新的ArrayList();
menuItems4=新的ArrayList();
menuItems6=新的ArrayList();
处理程序解析器=新处理程序();
字符串xml=result;//获取xml
字符串xml2=result2;
Document doc=parser.getdoElement(xml);
Document doc2=parser.getDoElement(xml2);
//获取DOM元素
NodeList nl=doc.getElementsByTagName(KEY\u FUEL);
NodeList nl2=doc2.getElementsByTagName(键燃料);
//循环通过所有项目节点
对于(int i=0;ivalue
map.put(KEY_HIGHEST,“HIGHEST units=“+parser.getValue(e,KEY_HIGHEST));
Log.v(标签,“indexmap=“+map”);
map1.put(KEY_AVERAGE),Avera
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;
}
menuItems4 = new ArrayList<HashMap<String, String>>();
menuItems4 = new ArrayList<HashMap<String, String>>();
menuItems4 = new ArrayList<HashMap<String, String>>();
menuItems5 = new ArrayList<HashMap<String, String>>();