JAVA Casting error

Posted by user1612725 on Stack Overflow See other posts from Stack Overflow or by user1612725
Published on 2012-09-06T21:12:53Z Indexed on 2012/09/07 9:38 UTC
Read the original article Hit count: 326

Filed under:
|
|
|
|

Im creating a program that uses Dijksrtras algorithm, im using nodes that represent cities on an imported map, and you can create edges between two cities on the map. My problem is every edge has a "weight" where it will represent distance in minutes and i have a function where i want to see the distance between the two edges.

But i keep getting the error "Cannot cast from Stad to Edge" at the line

Edge<Stad> selectedEdge = (Edge) fvf.visaFörbLista.getSelectedValue();

where "Stad" represents the city and "Edge" an edge.

        FormVisaförbindelse fvf = new FormVisaförbindelse();
                    for(;;){
                        try{
                            int svar = showConfirmDialog(null, fvf, "Ändra Förbindelser", JOptionPane.OK_CANCEL_OPTION);
                            if (svar != YES_OPTION)
                                return;
                            if (fvf.visaFörbLista.isSelectionEmpty() == true){
                                showMessageDialog(mainMethod.this, "En Förbindelse måste valjas.","Fel!", ERROR_MESSAGE);
                                return;
                            }

                            Edge<Stad> selectedEdge = (Edge) fvf.visaFörbLista.getSelectedValue();

                            FormÄndraförbindelse faf = new FormÄndraförbindelse();
                            faf.setförbNamn(selectedEdge.getNamn());
                            for(;;){
                                try{
                                    int svar2 = showConfirmDialog(mainMethod.this, faf, "Ändra Förbindelse", OK_CANCEL_OPTION);
                                    if (svar2 != YES_OPTION)
                                        return;
                                    selectedEdge.setVikt(faf.getförbTid());
                                    List<Edge<Stad>> edges = lg.getEdgesBetween(sB, sA);
                                    for (Edge<Stad> edge : edges){
                                        if (edge.getNamn()==selectedEdge.getNamn()){
                                            edge.setVikt(faf.getförbTid());
                                        }
                                    }
                                    return;
                                }
                                catch(NumberFormatException e){
                                    showMessageDialog(mainMethod.this, "Ogiltig inmatning.","Fel!", ERROR_MESSAGE);
                                }

© Stack Overflow or respective owner

Related posts about java

Related posts about oop