Methoden Bei Liste als Rückgabewert: IndexOutOfBoundsException

Sherry

Aktives Mitglied
Hallo,

ich sehe den Fehler nicht in meinem Code beim Erstellen/Auslesen einer Liste.
Folgende Klasse

Java:
package com.sherry.xml;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.plaf.basic.BasicBorders.ToggleButtonBorder;

import org.jdom2.Attribute;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPath;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

public class SearchElementsXML {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		String target = "Berlin"; 

		SAXBuilder builder = new SAXBuilder();
		
		File xmlFile = new File("resources/city.xml");
		
		Document doc;
		doc = (Document) builder.build(xmlFile);
		
		@SuppressWarnings("unchecked")
		List<String> results = new ArrayList<String>();
		List<Element> nodes;
		try {
			nodes = (List<Element>) XPath.selectNodes(doc, "/costcalculator/city");
		for (Element element : nodes) {
			if (target.equals(element.getAttributeValue("name"))) {
				results.add(element.getAttributeValue("name"));
				results.add(element.getChildText("distance"));
				results.add(element.getChildText("duration"));
				results.add(element.getChildText("ticket"));
				results.add(element.getChildText("hotel"));
				results.add(element.getChildText("flightcost"));
			}
			
		}
		} catch (JDOMException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int j = 0; j < results.size(); j++) {
			System.out.println(results.get(j));
		}
	
	}
}
erzeugt aus der XML-Datei

[XML]<?xml version="1.0" encoding="UTF-8"?>
<costcalculator>
<city name = "Berlin">
<distance>633085</distance>
<duration>20500</duration>
<ticket>284.00</ticket>
<hotel>44.00</hotel>
<flightcost>149.98</flightcost>
</city>
<city name = "Wolfsburg">
<distance>527815</distance>
<duration>17631</duration>
<ticket></ticket>
<hotel></hotel>
<flightcost></flightcost>
</city>
<city name = "Braunschweig">
<distance>499231</distance>
<duration>16753</duration>
<ticket></ticket>
<hotel></hotel>
<flightcost></flightcost>
</city>
</costcalculator>[/XML]

Berlin
633085
20500
284.00
44.00
149.98

Mit der Main-Klasse und einer Methode habe ich eine IndexOutOfBoundsException, dass der Index nur 0 ist.
Java:
package com.sherry.pricecalculator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPath;
import org.xml.sax.SAXException;

public class XMLCreator throws JDOMException, IOException  {
	private File xmlFile = new File("resources/City.xml");
	private Namespace ns = Namespace
			.getNamespace("http://www.linuxmaker.com/Preiskalkulator");
	String origin = "Stuttgart";
	String target;
	String ticket = "0.0";
	String hotel = "0.0";
	String flightcost = "0.0";

	Parser duration = new Parser();

	public XMLCreator() {
		super();
	}
	@SuppressWarnings("unchecked")
	public List<String> searchElements(String target) throws JDOMException, IOException {	
		SAXBuilder builder = new SAXBuilder();
			Document doc;
			doc = (Document) builder.build(xmlFile);

			@SuppressWarnings("deprecation")
			List<Element> nodes;
				nodes = (List<Element>) XPath.selectNodes(doc, "/costcalculator/city");
			
				List<String> results = new ArrayList<String>(); 
				for (Element element : nodes) {
					if (target.equals(element.getAttributeValue("name"))) {
						results.add(element.getAttributeValue("name"));
						results.add(element.getChildText("distance"));
						results.add(element.getChildText("duration"));
						results.add(element.getChildText("ticket"));
						results.add(element.getChildText("hotel"));
						results.add(element.getChildText("flightcost"));	
					}
				}
			
			return results;				
	}
	
}

Diese Main-Klasse
Java:
package com.sherry.pricecalculator;

public class Main throws JDOMException, IOException {

	public static void main(String[] args) {
		XMLCreator test = new XMLCreator();
		
		System.out.println(test.searchElements("Berlin").get(2));

	}

}
liefert die Meldung:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.sherry.pricecalculator.Main.main(Main.java:12)
Ich verstehe nicht, warum mir hier eine leere Liste zurückgegeben wird.

Vielen Dank

Sherry
 
Zuletzt bearbeitet:

Zurück
Oben