本文介绍了XML SAX解析器不工作 - NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
整个一天,我试图找出如何让我的解析器的工作,但由于某种原因,我总是得到一个NullPointerException异常,这里是我的code:
MainActivity:
公共类MainActivity延伸活动{
XMLHandler数据; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
尝试{
Log.i(PARSER,versuch!); / **
*创建SAX解析器的新实例
** / 的SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser的saxP = saxPF.newSAXParser();
XMLReader的xmlR = saxP.getXMLReader();
网址URL =新的URL(我的xml文件); //将XML的URL / **
*创建的处理程序来处理每个XML标记。
** /
数据=新XMLHandler();
xmlR.setContentHandler(数据);
xmlR.parse(新的InputSource(url.openStream()));
}赶上(例外五){
的System.out.println(E);
Log.i(PARSER,ES KLAPPT NICHT!);
}
TextView的TXT1 =(的TextView)findViewById(R.id.txtview1);
txt1.setText(data.getTournament()得到(1)获得(0).getTeam1()的toString()); }
XMLHandler:
公共类XMLHandler扩展的DefaultHandler { 私人锦标赛赛事;
私人TournamentDay CURRENTDAY;
私人匹配currentMatch;
私人StringBuilder的建设者; @覆盖
公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
抛出的SAXException { 如果(qName.equalsIgnoreCase(TEAM1))
currentMatch.setTeam1(builder.toString());
否则如果(qName.equalsIgnoreCase(TEAM2))
currentMatch.setTeam2(builder.toString());
否则如果(qName.equalsIgnoreCase(resultfinal))
currentMatch.setResultfinal(builder.toString());
否则如果(qName.equalsIgnoreCase(RESULT1))
currentMatch.setResult1(builder.toString());
否则如果(qName.equalsIgnoreCase(结果2))
currentMatch.setResult2(builder.toString());
否则,如果(qName.equalsIgnoreCase(场地))
currentMatch.setVenue(builder.toString());
否则,如果(qName.equalsIgnoreCase(GMT))
currentMatch.setGmt(builder.toString());
否则,如果(用于QName.equals(匹配))
currentDay.add(currentMatch);
否则,如果(用于QName.equals(天))
tournament.add(CURRENTDAY);
} @覆盖
公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
org.xml.sax.Attributes属性)抛出的SAXException {
如果(用于QName.equals(数据)){
赛=新的赛会();
}
如果(用于QName.equals(天)){
CURRENTDAY =新TournamentDay();
}
否则,如果(用于QName.equals(匹配)){
currentMatch =新的匹配();
}
其他{
建设者=新的StringBuilder();
}
} @覆盖
公共无效字符(字符[]字符,诠释开始,诠释长度)抛出的SAXException {
builder.append(字符,开始位置,长度);
} 公开锦标赛getTournament(){
回到比赛;
}
}
锦标赛:
公共类锦标赛{ 私人列表< TournamentDay>天; 公开锦标赛(){
this.days =新的ArrayList< TournamentDay>();
} 公共无效添加(TournamentDay天){
days.add(天);
}
公共TournamentDay得到(int i)以{
返回days.get(ⅰ);
}
}
匹配:
公共类匹配{
私人字符串TEAM1;
私人字符串TEAM2;
私人字符串resultfinal;
私人字符串RESULT1;
私人字符串RESULT2;
私人字符串日期;
私人字符串地点;
私人字符串GMT; 公共字符串getTeam1(){
返回TEAM1;
} 公共无效setTeam1(字符串TEAM1){
this.team1 = TEAM1;
} 公共字符串getTeam2(){
返回TEAM2;
} 公共无效setTeam2(字符串TEAM2){
this.team2 = TEAM2;
} 公共字符串getResultfinal(){
返回resultfinal;
} 公共无效setResultfinal(字符串resultfinal){
this.resultfinal = resultfinal;
} 公共字符串getResult1(){
返回RESULT1;
} 公共无效setResult1(字符串RESULT1){
this.result1 = RESULT1;
} 公共字符串getResult2(){
返回结果2;
} 公共无效setResult2(字符串RESULT2){
this.result2 = RESULT2;
} 公共字符串GETDATE(){
归期;
} 公共无效的setDate(字符串日期){
this.date =日期;
} 公共字符串getVenue(){
返回场地;
} 公共无效setVenue(字符串会场){
this.venue =地点;
} 公共字符串getGmt(){
返回GMT;
} 公共无效setGmt(字符串GMT){
this.gmt = GMT;
}}
最后我的XML文件:
<数据>
<日间及GT; <&比赛GT; <&TEAM1 GT; IND< / TEAM1>
<&TEAM2 GT; AFG< / TEAM2> < resultfinal>< / resultfinal>
<&RESULT1 GT;< / RESULT1>
<&RESULT2 GT;< / RESULT2>
<日期和GT;&19.09 LT; /日期和GT;
<场地> 1 LT; /场地>
<&北京时间GT; 14:00< / GMT>
< /匹配> <&比赛GT; <&TEAM1 GT;工程< / TEAM1>
<&TEAM2 GT; AFG< / TEAM2> < resultfinal>< / resultfinal>
<&RESULT1 GT;< / RESULT1>
<&RESULT2 GT;< / RESULT2> <日期和GT;&21.09 LT; /日期和GT;
<场地> 1 LT; /场地>
<&北京时间GT; 14:00< / GMT>
< /匹配>
<&比赛GT; <&TEAM1 GT; IND< / TEAM1>
<&TEAM2 GT;工程< / TEAM2> < resultfinal>< / resultfinal>
<&RESULT1 GT;< / RESULT1>
<&RESULT2 GT;< / RESULT2>
<日期和GT 23.09< /日期和GT;
<场地> 2'; /场地>
< GMT→10:00< / GMT>
< /匹配>
< /天>
<日间及GT;
<&比赛GT; <&TEAM1 GT; AUST< / TEAM1>
<&TEAM2 GT; IRL< / TEAM2> < resultfinal>< / resultfinal>
<&RESULT1 GT;< / RESULT1>
<&RESULT2 GT;< / RESULT2>
<日期和GT;&19.08 LT; /日期和GT;
<场地> 1 LT; /场地>
< GMT→10:00< / GMT>
< /匹配>
<&比赛GT; <&TEAM1 GT; AUST< / TEAM1>
<&TEAM2 GT;西至< / TEAM2> < resultfinal>< / resultfinal>
<&RESULT1 GT;< / RESULT1>
<&RESULT2 GT;< / RESULT2>
<日期和GT;&22.09 LT; /日期和GT;
<场地> 1 LT; /场地>
<&北京时间GT; 14:00< / GMT>
< /匹配>
<&比赛GT; <&TEAM1 GT;西至< / TEAM1>
<&TEAM2 GT; IRL< / TEAM2> < resultfinal>< / resultfinal>
<&RESULT1 GT;< / RESULT1>
<&RESULT2 GT;< / RESULT2> <日期和GT;&24.09 LT; /日期和GT;
<场地> 1 LT; /场地>
<&北京时间GT; 14:00< / GMT>
< /匹配> < /天>< /数据>
这里是logcat的:
15 09-17:26:49.299:我/的System.out(3392):显示java.lang.NullPointerException
09-17 15:26:49.299:I /解析器(3392):ES KLAPPT NICHT!
09-17 15:26:49.299:D / AndroidRuntime(3392):关闭VM
09-17 15:26:49.299:W / dalvikvm(3392):主题ID = 1:螺纹未捕获的异常(组= 0xb40a64f0)退出
09-17 15:26:49.299:I /流程(3392):发送信号。 PID:3392 SIG:9
09-17 15:26:49.299:D / AndroidRuntime(3392):从PROCNAME CMDLINE:com.example.xmldownloader
09-17 15:26:49.299:E / AndroidRuntime(3392):在writeCrashedAppName,PKGNAME:com.example.xmldownloader
09-17 15:26:49.299:D / AndroidRuntime(3392):com.example.xmldownloader StringBuffer的:与内容成功写入文件中; com.example.xmldownloader
09-17 15:26:49.299:E / AndroidRuntime(3392):致命异常:主要
09-17 15:26:49.299:E / AndroidRuntime(3392):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.xmldownloader / com.example.xmldownloader.MainActivity}:显示java.lang.NullPointerException
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1726)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:949)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.os.Handler.dispatchMessage(Handler.java:99)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.os.Looper.loop(Looper.java:130)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.app.ActivityThread.main(ActivityThread.java:3770)
09-17 15:26:49.299:E / AndroidRuntime(3392):在java.lang.reflect.Method.invokeNative(本机方法)
09-17 15:26:49.299:E / AndroidRuntime(3392):在java.lang.reflect.Method.invoke(Method.java:507)
09-17 15:26:49.299:E / AndroidRuntime(3392):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:880)
09-17 15:26:49.299:E / AndroidRuntime(3392):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
09-17 15:26:49.299:E / AndroidRuntime(3392):在dalvik.system.NativeStart.main(本机方法)
09-17 15:26:49.299:E / AndroidRuntime(3392):因:显示java.lang.NullPointerException
09-17 15:26:49.299:E / AndroidRuntime(3392):在com.example.xmldownloader.MainActivity.onCreate(MainActivity.java:58)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-17 15:26:49.299:E / AndroidRuntime(3392):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1662)
09-17 15:26:49.299:E / AndroidRuntime(3392):11 ...更多
有人可以帮助我?还是指出,我在做什么错了?
谢谢你。
解决方案
txt1.setText(data.getTournament()得到(1)获得(0).getTeam1()。的toString());
这不是一个code,在那里你想一下吧!
请把它分割成每行一个声明,并记录了!
完全相同这是问题的了。
the whole day I tried to figure out how to get my parser working, but for some reason I always get a NullPointerException, here is my code:
MainActivity:
public class MainActivity extends Activity {
XMLHandler data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Log.i("PARSER", "versuch!");
/**
* Create a new instance of the SAX parser
**/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL("my xml file"); // URL of the XML
/**
* Create the Handler to handle each of the XML tags.
**/
data = new XMLHandler();
xmlR.setContentHandler(data);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
Log.i("PARSER", "ES KLAPPT NICHT!");
}
TextView txt1 = (TextView) findViewById(R.id.txtview1);
txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString());
}
XMLHandler:
public class XMLHandler extends DefaultHandler {
private Tournament tournament;
private TournamentDay currentDay;
private Match currentMatch;
private StringBuilder builder;
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("team1"))
currentMatch.setTeam1(builder.toString());
else if (qName.equalsIgnoreCase("team2"))
currentMatch.setTeam2(builder.toString());
else if (qName.equalsIgnoreCase("resultfinal"))
currentMatch.setResultfinal(builder.toString());
else if (qName.equalsIgnoreCase("result1"))
currentMatch.setResult1(builder.toString());
else if (qName.equalsIgnoreCase("result2"))
currentMatch.setResult2(builder.toString());
else if (qName.equalsIgnoreCase("venue"))
currentMatch.setVenue(builder.toString());
else if (qName.equalsIgnoreCase("gmt"))
currentMatch.setGmt(builder.toString());
else if (qName.equals("match"))
currentDay.add(currentMatch);
else if (qName.equals("day"))
tournament.add(currentDay);
}
@Override
public void startElement(String uri, String localName, String qName,
org.xml.sax.Attributes attributes) throws SAXException {
if (qName.equals("data")) {
tournament = new Tournament();
}
if (qName.equals("day")) {
currentDay = new TournamentDay();
}
else if (qName.equals("match")) {
currentMatch = new Match();
}
else {
builder = new StringBuilder();
}
}
@Override
public void characters(char[] chars, int start, int length) throws SAXException {
builder.append(chars, start, length);
}
public Tournament getTournament() {
return tournament;
}
}
Tournament:
public class Tournament {
private List<TournamentDay> days;
public Tournament() {
this.days = new ArrayList<TournamentDay>();
}
public void add(TournamentDay day) {
days.add(day);
}
public TournamentDay get(int i) {
return days.get(i);
}
}
Match:
public class Match {
private String team1;
private String team2;
private String resultfinal;
private String result1;
private String result2;
private String date;
private String venue;
private String gmt;
public String getTeam1() {
return team1;
}
public void setTeam1(String team1) {
this.team1 = team1;
}
public String getTeam2() {
return team2;
}
public void setTeam2(String team2) {
this.team2 = team2;
}
public String getResultfinal() {
return resultfinal;
}
public void setResultfinal(String resultfinal) {
this.resultfinal = resultfinal;
}
public String getResult1() {
return result1;
}
public void setResult1(String result1) {
this.result1 = result1;
}
public String getResult2() {
return result2;
}
public void setResult2(String result2) {
this.result2 = result2;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
public String getGmt() {
return gmt;
}
public void setGmt(String gmt) {
this.gmt = gmt;
}
}
And finally my XML file:
<data>
<day>
<match>
<team1>ind</team1>
<team2>afg</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>19.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>eng</team1>
<team2>afg</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>21.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>ind</team1>
<team2>eng</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>23.09</date>
<venue>2</venue>
<gmt>10:00</gmt>
</match>
</day>
<day>
<match>
<team1>aust</team1>
<team2>irl</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>19.08</date>
<venue>1</venue>
<gmt>10:00</gmt>
</match>
<match>
<team1>aust</team1>
<team2>west</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>22.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
<match>
<team1>west</team1>
<team2>irl</team2>
<resultfinal></resultfinal>
<result1></result1>
<result2></result2>
<date>24.09</date>
<venue>1</venue>
<gmt>14:00</gmt>
</match>
</day>
</data>
Here is the logcat:
09-17 15:26:49.299: I/System.out(3392): java.lang.NullPointerException
09-17 15:26:49.299: I/PARSER(3392): ES KLAPPT NICHT!
09-17 15:26:49.299: D/AndroidRuntime(3392): Shutting down VM
09-17 15:26:49.299: W/dalvikvm(3392): threadid=1: thread exiting with uncaught exception (group=0xb40a64f0)
09-17 15:26:49.299: I/Process(3392): Sending signal. PID: 3392 SIG: 9
09-17 15:26:49.299: D/AndroidRuntime(3392): procName from cmdline: com.example.xmldownloader
09-17 15:26:49.299: E/AndroidRuntime(3392): in writeCrashedAppName, pkgName :com.example.xmldownloader
09-17 15:26:49.299: D/AndroidRuntime(3392): file written successfully with content: com.example.xmldownloader StringBuffer : ;com.example.xmldownloader
09-17 15:26:49.299: E/AndroidRuntime(3392): FATAL EXCEPTION: main
09-17 15:26:49.299: E/AndroidRuntime(3392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xmldownloader/com.example.xmldownloader.MainActivity}: java.lang.NullPointerException
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1726)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:949)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.os.Looper.loop(Looper.java:130)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.main(ActivityThread.java:3770)
09-17 15:26:49.299: E/AndroidRuntime(3392): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 15:26:49.299: E/AndroidRuntime(3392): at java.lang.reflect.Method.invoke(Method.java:507)
09-17 15:26:49.299: E/AndroidRuntime(3392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
09-17 15:26:49.299: E/AndroidRuntime(3392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
09-17 15:26:49.299: E/AndroidRuntime(3392): at dalvik.system.NativeStart.main(Native Method)
09-17 15:26:49.299: E/AndroidRuntime(3392): Caused by: java.lang.NullPointerException
09-17 15:26:49.299: E/AndroidRuntime(3392): at com.example.xmldownloader.MainActivity.onCreate(MainActivity.java:58)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-17 15:26:49.299: E/AndroidRuntime(3392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1662)
09-17 15:26:49.299: E/AndroidRuntime(3392): ... 11 more
Can somebody help me with this? Or point out, what I am doing wrong?Thank you.
解决方案
txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString());
it is not a code, where you would like to look at it!Please split it into one statement per line, and log it!
Exactly the same it was the problem here too.
这篇关于XML SAX解析器不工作 - NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!