【问题标题】:java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vectorjava.lang.ClassCastException:java.lang.String 无法转换为 java.util.Vector
【发布时间】:2014-01-21 09:48:43
【问题描述】:

我尝试将 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);

请帮帮我

【问题讨论】:

  • 旁注:向您的问题添加异常时,最好标记出抛出异常的行。

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


【解决方案1】:

根据the javadoc for DefaultTableModel,数据必须是Vector&lt;Vector&lt;String&gt;&gt;。此外,您还可以在 DefaultTableModel 构造函数参数中反向获取数据和标题。

【讨论】:

    【解决方案2】:

    DefaultTableModel 使用的构造函数的 Java 文档状态为:

    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&lt;Vector&lt;String&gt;&gt;

    更新

    示例代码

        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 或 header 为空表将不会显示

    【讨论】:

    • 如果我将它更改为 Vector 它会在这一行抱怨:data.addElement(nodeList.item(i).getFirstChild().getNodeValue());并说参数必须是一个字符串。
    • 你的第一个论点应该是 Vector> 而不是第二个
    • 好吧,它解决了异常,但仍然没有在表格中显示任何数据。
    • 是的标头为空:标头:[]。没有正确获取数据。好吧。
    • 是的,我在表格中添加了模型。
    猜你喜欢
    • 2013-07-25
    • 2021-08-04
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多