Exception: java.lang.ClassCastException

HerrInfo

Mitglied
Hallo zusammen

Ich bin es nochmal. Beim Verbessern der Objektorientierung bin ich nun auf folgenden Fehler gestoßen, welcher für mich keinen wirklichen Sinn ergibt. Der Fehler sei eine Cast Exception. Ich wüsste aber nicht, dass ich irgendwo caste. Zudem wird keine Zeile angegeben, in welcher der Fehler ist. Ich bitte um eure Hilfe. Hier ist erstmal der Fehler:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
    at java.desktop/javax.swing.LayoutComparator.compare(LayoutComparator.java:96)
    at java.desktop/javax.swing.LayoutComparator.compare(LayoutComparator.java:49)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:650)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:665)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.legacySort(SortingFocusTraversalPolicy.java:161)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:150)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:123)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:476)
    at java.desktop/javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:172)
    at java.desktop/javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:569)
    at java.desktop/java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:174)
    at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:494)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4877)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4828)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
    at java.desktop/java.awt.SequencedEvent.dispatch(SequencedEvent.java:206)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Java:
package net.tim.gui;

import javax.swing.*;

public class Frame extends JFrame {

    public Frame() {
            setTitle("Tower Defense");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            add(new GamePanel());
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
    }

}
Java:
package net.tim.gui;

import javax.swing.*;
import java.awt.*;

public class GamePanel extends JPanel {

    //screen Settings
    public static final int maxCol = 15;
    public static final int maxRow = 10;
    final int TileSize = 75;

    final int screenWidth = maxCol * TileSize;
    final int screenHeight = maxRow * TileSize;

    //Tiles
    Tile[][] tile = new Tile[maxRow][maxCol];

    public GamePanel() {
        setPreferredSize(new Dimension(screenWidth, screenHeight));
        setBackground(Color.BLACK);
        setLayout(new GridLayout(maxRow, maxCol));
        setFocusable(true);

        //create tiles
        int col = 0;
        int row = 0;

        while (col < maxCol && row < maxRow) {

            tile[row][col] = new Tile(col, row);
            this.add(tile[row][col]);

            col++;
            if (col == maxCol) {
                col = 0;
                row++;
            }
        }
    }
}

Java:
package net.tim.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Tile extends JButton implements ActionListener {
    Tile parent;
    int col;
    int row;
    int gCost;
    int hCost;
    int fCost;
    boolean start;
    boolean goal;
    boolean solid;
    boolean open;
    boolean checked;

    public Tile(int col, int row) {
        this.col = col;
        this.row = row;

        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
        addActionListener(this);
    }

    public void setAsStart() {
        start = true;
        setText("Start");
    }

    public void setAsGoal() {
        goal = true;
        setText("Goal");
    }

    public void setAsSolid() {
        solid = true;
    }

    public void setAsOpen() {
        open = true;
    }

    public void setAsChecked() {

        checked = true;
    }

    public void setAsPath() {

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    //Getters and Setters

    @Override
    public Tile getParent() {
        return parent;
    }

    public void setParent(Tile parent) {
        this.parent = parent;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getgCost() {
        return gCost;
    }

    public void setgCost(int gCost) {
        this.gCost = gCost;
    }

    public int gethCost() {
        return hCost;
    }

    public void sethCost(int hCost) {
        this.hCost = hCost;
    }

    public int getfCost() {
        return fCost;
    }

    public void setfCost(int fCost) {
        this.fCost = fCost;
    }

    public boolean isStart() {
        return start;
    }

    public void setStart(boolean start) {
        this.start = start;
    }

    public boolean isGoal() {
        return goal;
    }

    public void setGoal(boolean goal) {
        this.goal = goal;
    }

    public boolean isSolid() {
        return solid;
    }

    public void setSolid(boolean solid) {
        this.solid = solid;
    }

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
Java:
package net.tim.pathfinding;

import net.tim.gui.Tile;

import java.util.ArrayList;
import java.util.List;

import static net.tim.gui.GamePanel.maxCol;
import static net.tim.gui.GamePanel.maxRow;

public class Pathfinding {
    Tile[][] tile;
    Tile startTile, goalTile, currentTile;
    List<Tile> openList = new ArrayList<>();
    List<Tile> checkedList = new ArrayList<>();

    //other
    boolean goalReached = false;

    public Pathfinding(Tile[][] tile) {
        this.tile = tile;
    }

    public void autoSearch() throws RuntimeException {
        int step = 0;

        while (!goalReached && step < 1000) {
            search();
            step++;
        }

        if (goalReached) {
            System.out.println("Goal reached");
        } else {
            throw new RuntimeException("No path found");
        }

    }

    private void search(){
        if (!goalReached) {

            int col = currentTile.getCol();
            int row = currentTile.getRow();

            currentTile.setAsChecked();
            checkedList.add(currentTile);
            openList.remove(currentTile);

            //open the node left
            if (col -1 >= 0) {
                openNode(tile[row][col - 1]);
            }

            //open the node above
            if (row -1 >= 0) {
                openNode(tile[row - 1][col]);
            }

            //open the node right
            if (col + 1 < maxCol) {
                openNode(tile[row][col + 1]);
            }

            //open the node below
            if (row + 1 < maxRow) {
                openNode(tile[row + 1][col]);
            }

            int bestNodeIndex = -1;
            int bestNodeFCost = Integer.MAX_VALUE;

            for (int i = 0; i < openList.size(); i++) {
                if (openList.get(i).getfCost() < bestNodeFCost) {
                    bestNodeFCost = openList.get(i).getfCost();
                    bestNodeIndex = i;
                } else if (openList.get(i).getfCost() == bestNodeFCost) {
                    if (openList.get(i).getgCost() < openList.get(bestNodeIndex).getgCost()) {
                        bestNodeIndex = i;
                    }

                }
            }

            if (bestNodeIndex == -1) {
                throw new RuntimeException("No best node found most likely no path");
            }

            currentTile = openList.get(bestNodeIndex);

            if (currentTile == goalTile) {
                goalReached = true;
                trackThePath();
            }

        }
    }

    private void setStartNode(int col, int row) {
        tile[row][col].setAsStart();
        startTile = tile[row][col];
        currentTile = startTile;
    }

    private void setGoalNode(int col, int row) {
        tile[row][col].setAsGoal();
        goalTile = tile[row][col];
    }

    private void setSolidNode(int col, int row) {
        tile[row][col].setAsSolid();
    }

    private void setCostOnNodes() {
        int col = 0;
        int row = 0;

        while (col < maxCol && row < maxRow) {
            getCost(tile[row][col]);

            col++;
            if (col == maxCol) {
                col = 0;
                row++;
            }
        }
    }
    private void getCost(Tile tile){
        //gCost
        int xDistance = Math.abs(tile.getCol() - startTile.getCol());
        int yDistance = Math.abs(tile.getRow() - startTile.getRow());
        tile.setgCost(xDistance + yDistance);

        //hCost
        xDistance = Math.abs(tile.getCol() - goalTile.getCol());
        yDistance = Math.abs(tile.getRow() - goalTile.getRow());
        tile.sethCost(xDistance + yDistance);

        //fCost
        tile.setfCost(tile.getgCost() + tile.gethCost());

        //display costs
        if (tile != startTile && tile != goalTile) {
            tile.setText("<html>F: " + tile.getfCost() + "<br>G: " + tile.getgCost() + "</html>");
        }
    }

    private void openNode(Tile tile) {
        if (!tile.isOpen() && !tile.isChecked() && !tile.isSolid()) {
            tile.setAsOpen();
            tile.setParent(currentTile);
            openList.add(tile);
        }
    }

    private void trackThePath() {
        Tile parent = goalTile.getParent();
        while (parent != startTile) {
            parent.setAsPath();
            parent = parent.getParent();
        }
    }
}
 

LimDul

Top Contributor
Java:
    @Override
    public Tile getParent() {
        return parent;
    }

    public void setParent(Tile parent) {
        this.parent = parent;
    }
Das ist böse. getParent darfst du nicht überschreiben, du zerhaust damit interne Swing Objektgeflecht und dann legt sich Swing lang. Das ist die Ursache, dass du das überschrieben hast.

Ändere Beziehungen nur über die add/remove Methoden. Und wenn das Programm Thread erstellt verwendet, dann dürfen diese Methoden auch nur aus dem Swing EDT aufgerufen werden. Sprich, aus anderen Threads muss man über SwingUtilities.invokeLater die GUI ändern
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
T Exception in thread "main" java.lang.NoClassDefFoundError AWT, Swing, JavaFX & SWT 4
X Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 AWT, Swing, JavaFX & SWT 6
L exception in thread awt-eventqueue-0 java.lang.nullpointerexception AWT, Swing, JavaFX & SWT 2
C Event Handling Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException AWT, Swing, JavaFX & SWT 43
M Exception in thread "Thread-3" java.lang.NullPointerException AWT, Swing, JavaFX & SWT 18
F Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null AWT, Swing, JavaFX & SWT 5
S Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException AWT, Swing, JavaFX & SWT 7
A Exception in thread "AWT-EventQueue-0" java.lang.N AWT, Swing, JavaFX & SWT 4
S java.fxml.load.exception und keine automatische Aktualliseriung der Mainausgabe AWT, Swing, JavaFX & SWT 5
M 3D-Grafik Mac Java 3D in Eclipse Exception AWT, Swing, JavaFX & SWT 0
M java headless exception AWT, Swing, JavaFX & SWT 5
M Seltsame Exception bei setVisible(true) Methode in einem JFrame AWT, Swing, JavaFX & SWT 2
W Nullpointer Exception beim übertragen von Daten von Scene zu Scene AWT, Swing, JavaFX & SWT 6
G Exception javafx Thread -> caused by removing children while in EventHandler AWT, Swing, JavaFX & SWT 28
beli3ver JavaFX Anwendung startet nicht, kann den Fehler nicht finden (Exception in Application start method) AWT, Swing, JavaFX & SWT 1
Z JavaFX Exception in Application start method AWT, Swing, JavaFX & SWT 2
J "Exception in thread "AWT-EventQueue-0"" Fehler AWT, Swing, JavaFX & SWT 3
B JavaFX Warum wird hier eine NullPointer-Exception geworfen? AWT, Swing, JavaFX & SWT 7
R AWT no such child Exception AWT, Swing, JavaFX & SWT 7
L JavaFX Exception nach includieren einer fxml // nested controller AWT, Swing, JavaFX & SWT 1
J Exception beim JFrame erstellen AWT, Swing, JavaFX & SWT 6
A Swing Exception in thread "AWT-EventQueue-0" AWT, Swing, JavaFX & SWT 1
W JavaFX Warum kriege ich eine Exception? AWT, Swing, JavaFX & SWT 23
S JavaFX Exception in thread "JavaFX Application Thread" AWT, Swing, JavaFX & SWT 3
L JavaFX JavaFX stürtzt durch einen Server#connect Exception AWT, Swing, JavaFX & SWT 3
ralfb1105 JavaFX Exception Message von Model Class via Controller in View darstellen AWT, Swing, JavaFX & SWT 39
L Java FX Exception beim start AWT, Swing, JavaFX & SWT 2
J ObservableList wirft exception beim zweiten füllen. AWT, Swing, JavaFX & SWT 4
VfL_Freak AWT Exception in der Eventqueue AWT, Swing, JavaFX & SWT 8
A Swing Null Pointer Exception obwohl Objekt initialisiert AWT, Swing, JavaFX & SWT 21
H JavaFX Ein View vorschalten Exception AWT, Swing, JavaFX & SWT 7
S NullPointer Exception beim Laden von Bildern AWT, Swing, JavaFX & SWT 11
Q "AWT-EventQueue-0" Exception Problem AWT, Swing, JavaFX & SWT 4
I Opencv Imgproc.floodfill exception AWT, Swing, JavaFX & SWT 1
S Swing Exception in thread "AWT-EventQueue-0" bei Jlabel AWT, Swing, JavaFX & SWT 4
T Gui Exception AWT, Swing, JavaFX & SWT 2
X Auf Fensterklasse zugreifen (Nullpointer Exception) AWT, Swing, JavaFX & SWT 2
M Threads - nicht erklärbare Exception AWT, Swing, JavaFX & SWT 6
M Bilder zeichnen, NullPointer Exception AWT, Swing, JavaFX & SWT 3
J Einträge aus Table löschen ohne Exception AWT, Swing, JavaFX & SWT 6
V Swing Nullpointer Exception bei JFormattedTextField AWT, Swing, JavaFX & SWT 4
N Swing Exception: Comparison method violates its general contract! AWT, Swing, JavaFX & SWT 13
S Exception bei BufferStrategy AWT, Swing, JavaFX & SWT 8
H Swing Exception's und MVC AWT, Swing, JavaFX & SWT 35
R Swing Mit Swing eine throws Exception Methode öffnen AWT, Swing, JavaFX & SWT 3
K AWT DataBuffer in DataBufferInt Exception AWT, Swing, JavaFX & SWT 2
lumo SWT Exception in thread "main" org.eclipse.swt.SWTError: No more handles AWT, Swing, JavaFX & SWT 3
S Swing AWT-Exception bei JTable mit JComboBox beim Löschen der letzten Zeile AWT, Swing, JavaFX & SWT 3
O Immer Exception in AWT-EventQueue-0 AWT, Swing, JavaFX & SWT 5
F Swing Seltsame Exception AWT, Swing, JavaFX & SWT 5
R Swing Files auflisten (JButton, JList, Exception) AWT, Swing, JavaFX & SWT 4
V LookAndFeel Nullpoint Exception AWT, Swing, JavaFX & SWT 2
Airwolf89 Swing Exception beim Füllen einer Tabelle AWT, Swing, JavaFX & SWT 10
hdi SWT Zwei mal Shell öffnen = Exception AWT, Swing, JavaFX & SWT 6
N Exception Behandlung mit setDefaultUncaughtExceptionHandler, insbesondere im EventDispatcher Thread AWT, Swing, JavaFX & SWT 4
O Probleme beim Zeichnen und AWT-Event-Queue-0 Exception AWT, Swing, JavaFX & SWT 5
O Exception bei repaint AWT, Swing, JavaFX & SWT 3
D Bild zeichnen lassen aus array, null pointer Exception AWT, Swing, JavaFX & SWT 2
B JTable ValueChanged verursacht Exception AWT, Swing, JavaFX & SWT 3
Daniel_L JTable, (Auto-)RowSorter und NullPointer-Exception AWT, Swing, JavaFX & SWT 6
M Exception in eine JTextArea anzeigen AWT, Swing, JavaFX & SWT 7
T Exception: Adding a window to a container AWT, Swing, JavaFX & SWT 14
G Kann mir jemand diese Exception erklaeren? AWT, Swing, JavaFX & SWT 8
T AWT Exception AWT, Swing, JavaFX & SWT 5
A Exception beim LineWrapping AWT, Swing, JavaFX & SWT 16
P Für mich unverständliche Exception AWT, Swing, JavaFX & SWT 3
N JList - Nullpointer Exception AWT, Swing, JavaFX & SWT 4
Lony AbstractTableModel Exception in thread "AWT-EventQueue- AWT, Swing, JavaFX & SWT 3
T Warum schmeißt das eine Exception? AWT, Swing, JavaFX & SWT 6
I EventDispatchThread Exception AWT, Swing, JavaFX & SWT 5
M Problem: "ArrayIndexOutOfBounds Exception" AWT, Swing, JavaFX & SWT 11
C Out of Bounds Exception, Array of JTextField AWT, Swing, JavaFX & SWT 6
B JTable Vector Exception bei eigenem CellEditor AWT, Swing, JavaFX & SWT 3
D Seltsame AWT-Exception AWT, Swing, JavaFX & SWT 2
M Button und Exception mit NetBeans AWT, Swing, JavaFX & SWT 3
P paint null pointer exception AWT, Swing, JavaFX & SWT 3
M Exception Meldung aus Thread an jTextField1 übergeben AWT, Swing, JavaFX & SWT 6
A Null pointer exception beim Neuzeichnen AWT, Swing, JavaFX & SWT 4
R DataVector bei TableModel - Class Cast Exception AWT, Swing, JavaFX & SWT 9
G Drag & Drop bzw. Data Transfer - Exception nach Drag AWT, Swing, JavaFX & SWT 1
K Exception mit "Unknown Source" AWT, Swing, JavaFX & SWT 5
Z jar-File mit SWT - Exception AWT, Swing, JavaFX & SWT 4
F Habe Problem mit dem Abfangen einer Exception AWT, Swing, JavaFX & SWT 10
V Null Pointer Exception bei JTextField setText AWT, Swing, JavaFX & SWT 25
T Programm funkt. nicht - Exception AWT, Swing, JavaFX & SWT 7
G Exception in JList oder JTextArea AWT, Swing, JavaFX & SWT 9
O Exception abfangen AWT, Swing, JavaFX & SWT 19
Juelin Für Java-Spezialisten AWT, Swing, JavaFX & SWT 4
Juelin Java <-> Delphi AWT, Swing, JavaFX & SWT 3
H Exceptions seit java: 1.6.0_65 Umstellung AWT, Swing, JavaFX & SWT 3
M JavaFX java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found AWT, Swing, JavaFX & SWT 5
J Java GUI Dropdown-Menü anzeigen AWT, Swing, JavaFX & SWT 5
M Eigene Java Klasse für allgemeine Grafikelemente AWT, Swing, JavaFX & SWT 8
thor_norsk Java - Allgemeine - Frage AWT, Swing, JavaFX & SWT 14
W Kennt jemand Dear ImGui (und den Java-Wrapper dazu)? AWT, Swing, JavaFX & SWT 0
B Java Projekt mit JavaFX und jfoenix ausführbar machen AWT, Swing, JavaFX & SWT 46
D JAVA Schieberegler AWT, Swing, JavaFX & SWT 6
N JavaFX Einfacher Taschenrechner mit Scene Builder und Java FX AWT, Swing, JavaFX & SWT 0
Jose05 Aus einer normalen Java Klasse eine FXML-Klasse laden AWT, Swing, JavaFX & SWT 12
S Welches Java Layout sollte ich verwenden? AWT, Swing, JavaFX & SWT 3

Ähnliche Java Themen

Neue Themen


Oben