SAXParser-Problem

  • Antworten:1
Jan Kressin
  • Forum-Beiträge: 9

29.06.2011, 13:40:49 via Website

Ich bekomme immer eine Fehlermeldung innerhalb des SaxParsers:
hier der Code:

1public class SaxParser extends DefaultHandler{
2
3 private String line;
4 private String subline;
5 private String direction;
6 private String station;
7
8 private Line l;
9 private Station s;
10 private Point p;
11
12 public Manager mng;
13
14 Resources res;
15
16 public SaxParser(Resources res){
17 this.res = res;
18 }
19
20 public void load(){
21
22 this.mng = Manager.getInstance();
23
24 SAXParser saxParser;
25 SaxParser handler = new SaxParser(res);
26
27 InputStream inputStream = res.openRawResource(R.raw.plan_hl);
28 InputSource inputSource = new InputSource(new InputStreamReader(inputStream));
29
30 try {
31 saxParser = SAXParserFactory.newInstance().newSAXParser();
32 saxParser.parse(inputSource , handler);
33 } catch (ParserConfigurationException e) {
34 // TODO Auto-generated catch block
35 e.printStackTrace();
36 } catch (SAXException e) {
37 // TODO Auto-generated catch block
38 e.printStackTrace();
39 } catch (IOException e) {
40 // TODO Auto-generated catch block
41 e.printStackTrace();
42 }
43
44 }
45
46 @Override
47 public void startDocument(){
48
49 }
50
51 @Override
52 public void endDocument(){
53
54 }
55
56 @Override
57 public void startElement(String namespaceURI, String localName, String qName, Attributes attrs){
58 if(localName.equals("line")){
59 line = attrs.getValue("number");
60 l = new Line();
61 l.setLine(line);
62
63 }
64
65 if(localName.equals("direction")){
66 direction = attrs.getValue("target");
67 l.setDirection(direction);
68 }
69
70 if(localName.equals("subline")){
71 subline = attrs.getValue("id");
72 }
73
74 if(localName.equals("station")){
75 station = attrs.getValue("name");
76 s = new Station();
77 s.setName(station);
78 }
79
80 if(localName.equals("time")){
81 p = new Point();
82 p.setDrive(Integer.parseInt(attrs.getValue("drive")));
83 }
84 }
85
86 @Override
87 public void characters( char[] buf, int offset, int len ){
88 /*if(buf.length != 0){
89 String s = new String (buf, offset, len);
90 p.setTime(s);
91 }*/
92 }
93
94 @Override
95 public void endElement(String namespaceURI, String localName, String qName){
96 if(localName.equals("time")){
97 s.addPoint(p);
98 }
99
100 if(localName.equals("station")){
101 l.addStation(s);
102 }
103
104 if(localName.equals("line")){
105 mng.addLine(l);
106 }
107 }
108}

hier die Fehlermeldung aus dem LogCat:
06-29 11:27:40.877: ERROR/AndroidRuntime(1987): Caused by: java.lang.NullPointerException
06-29 11:27:40.877: ERROR/AndroidRuntime(1987): at fh.mci.BusStop.SaxParser.endElement(SaxParser.java:118)

Der doppelclick auf die 2te Meldung verweist mich auf die Zeile

mng.addLine(l);

Zu Testzwecken habe ich diese Zeile auskommentiert und an anderer Stelle eingefügt.
Seltsamerweise funktioniert diese Zeile, sofern sie sich nicht innerhalb der Handlermethoden befindet.
z.B. hier:

1public void load(){
2
3 this.mng = Manager.getInstance();
4
5 l = new Line();
6 mng.addLine(l);
7
8 SAXParser saxParser;
9 SaxParser handler = new SaxParser(res);
10
11 InputStream inputStream = res.openRawResource(R.raw.plan_hl);
12 InputSource inputSource = new InputSource(new InputStreamReader(inputStream));

selbst in sartDocument - Methode kommt die nullpointer Exception.

hier der Simple Code vom Manager-Objekt (singleton):
1package fh.mci.BusStop;
2
3import java.util.ArrayList;
4
5public class Manager {
6
7 protected static Manager soleInstance = null;
8 ArrayList<Line> lines;
9
10 public static Manager getInstance(){
11 if(soleInstance == null){
12 soleInstance = new Manager();
13 }
14 return soleInstance;
15 }
16
17 protected Manager(){
18 lines = new ArrayList<Line>();
19 }
20
21
22
23 public void addLine(Line l){
24 lines.add(l);
25 }
26
27 public int getLineCount(){
28 return lines.size();
29 }
30}

Antworten
Stefan S.
  • Forum-Beiträge: 560

01.07.2011, 08:57:59 via Website

Lass mal this.mng weg und schreib nur mng

Antworten