android解析xml文件又报java.lang.NullPointerException错误
package com.huawei.biz;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.huawei.entity.*;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LoadXml extends Activity {
private final static String TAG="LoadXml";
public static Context context = null;
Document document = null;
NodeList childsNodes = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
InputStream inputStreams = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(button1OnClickListener);
}
OnClickListener button1OnClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
getComplateXml();
}
};
public void getComplateXml(){
try {
Log.d(TAG, "getComplateXml");
readUserConfig();
int j = 0;
for (int i = 0; i < childsNodes.getLength(); i++) {
Log.d(TAG, "1");
Node node = (Node) childsNodes.item(i);
Log.d(TAG, "2");
Uri insertUri = Uri.parse("content://com.huawei.biz.LoadContentProvider/STUDENTS");
Log.d(TAG, "3");
ContentValues values = new ContentValues();
Log.d(TAG, "4");
values.put("name", node.getNodeName());
Log.d(TAG, node.getNodeName());
values.put("id", node.getFirstChild().getNodeValue());
Log.d(TAG, node.getFirstChild().getNodeValue());
values.put("Photo",DataManager.getDrawableList().get(j));
Log.d(TAG, DataManager.getDrawableList().get(j).toString());
getContentResolver().insert(insertUri, values);
j++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void readUserConfig() throws Exception{
Log.d(TAG, "readUserConfig");
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
inputStreams = getResources().getAssets().open("student.xml");
document = builder.parse(inputStreams);
childsNodes = document.getDocumentElement().getChildNodes();
}
}
这次是在Log.d(TAG, "4"); 的后面开始报错的。。
student.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<Students>
<WangChen photo="">TB0904060</WangChen>
<WangTing photo="">TB0904061</WangTing>
<WangYa photo="">TB0904062</WangYa>
<TaoJun photo="">TB0904063</TaoJun>
<HanQi photo="">TB0904064</HanQi>
<WangJInlong photo="">TB0904065</WangJInlong>
<ZhangLing photo="">TB0904066</ZhangLing>
<HuangHaibo photo="">TB0904067</HuangHaibo>
<ShenQiang photo="">TB0904068</ShenQiang>
<YuKuan photo="">TB0904069</YuKuan>
<WangJiuCong photo="">TB0904070</WangJiuCong>
<XiaTing photo="">TB0904071</XiaTing>
<ZhangYang photo="">TB0904072</ZhangYang>
<YaoShun photo="">TB0904073</YaoShun>
</Students>
[解决办法]
for (int i = 0; i < childsNodes.getLength(); i++)
//下面应该都是i吧 ,怎么是j呢
values.put("Photo",DataManager.getDrawableList().get(j));
Log.d(TAG, DataManager.getDrawableList().get(j).toString());
getContentResolver().insert(insertUri, values);
j++;
[解决办法]