Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have XML parsed data that I am trying to add into a DefaultTableModel. The DefaultTableModel takes in Vectors or Object[] as argument according to the documentation.

But when I use a Vector I get this exception :

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Vector

This is the class I am using to parse and add into the 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;
    }

}

And this last line of code is where it cast exception. This is from my GUI class :

model = new DefaultTableModel(PropXMLParsing.getInstance().getHeader(),
                PropXMLParsing.getInstance().getData());

        table = new JTable(model);

Help me please

share|improve this question
    
Side note: When adding an exception to your question, it is good to mark the line, where it is thrown. –  Fildor Jan 21 at 9:56
add comment

2 Answers

According to the javadoc for DefaultTableModel, the data must be a Vector<Vector<String>>. Also, you've got the data and header backwards in the DefaultTableModel constructor arguments.

share|improve this answer
add comment

Java Docs states for used Constructor of DefaultTableModel as:

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

And setDataVector() accepts dataVector(i.e.first arguement) as Vector of vectors of Object values

So In your case first arguement in constructor (i.e. PropXMLParsing.getInstance().getHeader()) should be Vector<Vector<String>>

UPDATE

Sample Code

    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);

In case any of your dataVector or header is null table will not be displayed

share|improve this answer
    
If I change it to a Vector <Vector> it will complain on this line : data.addElement(nodeList.item(i).getFirstChild().getNodeValue()); And say that the argument have to be a String. –  Sembrano Jan 21 at 10:03
    
your first arguement should be Vector<Vector<String>> not the second one –  Prateek Jan 21 at 10:07
    
Aight it solved the exception but sti not displaying any data in the table. –  Sembrano Jan 21 at 10:15
    
only in the console –  Sembrano Jan 21 at 10:15
    
yeh header is null : header : []. not getting data correctly. aight. –  Sembrano Jan 21 at 10:20
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.