我有我想要添加到DefaultTableModel中的XML解析數據。 根據文檔,DefaultTableModel將Vector或Object []作爲參數。java.lang.ClassCastException:java.lang.String不能轉換爲java.util.Vector
但是當我使用一個Vector我得到這個異常:
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vector
這是我使用的解析,並添加到載體的類。
public class PropXMLParsing {
static PropXMLParsing instance = null;
private Vector<String> header = new Vector<String>();
private Vector<String> data = new Vector<String>();
public static PropXMLParsing getInstance() {
if (instance == null) {
instance = new PropXMLParsing();
try {
instance.ParserForObjectTypes();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
return instance;
}
public void ParserForObjectTypes() throws SAXException, IOException,
ParserConfigurationException {
try {
FileInputStream file = new FileInputStream(new File(
"xmlFiles/CoreDatamodel.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//prop/*";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild()
.getNodeValue());
data.addElement(nodeList.item(i).getFirstChild().getNodeValue());
header.addElement(nodeList.item(i).getFirstChild()
.getNodeValue());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
public Vector<String> getHeader() {
return header;
}
public Vector<String> getData() {
return data;
}
}
而這最後一行代碼是它拋出異常。這是從我的GUI類:
model = new DefaultTableModel(PropXMLParsing.getInstance().getHeader(),
PropXMLParsing.getInstance().getData());
table = new JTable(model);
幫助我,請
邊注:當添加一個例外,你的問題,這是很好的紀念該線路在哪裏被拋出。 – Fildor