java.lang.ClassCastException:java.lang.String无法强制转换为java.util.Vector

时间:2014-01-21 09:48:44

标签: java user-interface xpath vector xml-parsing

我正在尝试将XML解析数据添加到DefaultTableModel中。 根据文档,DefaultTableModel将Vectors或Object []作为参数。

但是当我使用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);

请帮帮我

2 个答案:

答案 0 :(得分:2)

根据the javadoc for DefaultTableModel,数据必须为Vector<Vector<String>>。此外,您还在DefaultTableModel构造函数参数中获得了数据和标头。

答案 1 :(得分:1)

Java Docs将使用的DefaultTableModel构造函数声明为:

public DefaultTableModel(Vector data,
                 Vector columnNames)

Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.

Parameters:
    data - the data of the table, a Vector of Vectors of Object values
    columnNames - vector containing the names of the new columns

setDataVector()接受dataVector(i.e.first arguement) Vector of vectors of Object

所以在你的情况下,构造函数(i.e. PropXMLParsing.getInstance().getHeader())中的第一个论点应该是Vector<Vector<String>>

更新

示例代码

    JFrame frame = new JFrame("DemoFrame");

    Vector<String> id = new Vector<String>();
    id.add("1");
    id.add("2");
    id.add("3");

    Vector<String> name = new Vector<String>();
    name.add("A");
    name.add("B");
    name.add("C");

    Vector<Vector<String>> dataVector = new Vector<Vector<String>>();
    dataVector.add(id);
    dataVector.add(name);

    Vector<String> header = new Vector<String>(2);
    header.add("ID");
    header.add("NAME");


    TableModel model = new DefaultTableModel(dataVector,header);

    JTable table = new JTable(model);

    frame.add(new JScrollPane(table));
    frame.setSize(300, 200);
    frame.setVisible(true);

如果您的dataVector或标头中的任何一个为空表,则不会显示