私はGUI上で作業してきましたが、問題が見つかりました。プログラムを起動する時間の約10%でClassCastExceptionが発生しますが、コードはハングしておらず、不思議に実行されません。通常のように動作します。私が書いたコードをスタックトレースが参照することはないというのは奇妙なことです。私はtry-catchですべてをラップすることでこれを確認しようとしましたが、確かに、エラーはスローされ、捕まえられません。コードには決して実際には影響しないので、害はありませんが、まだ分かりません。私はそれはまだエラーを繰り返して得ることができるように簡略化としてここでこれはなぜClassCastExceptionをスローしますか?
はコードです:
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ErrorTracker extends JFrame{
private JButton addRow;
private JScrollPane pane;
private JPanel scrollPanel, panel;
private JFrame frame;
public static void main (String[] args) {
/**
* The error only occurs sometimes, so I'm running it 20 times to
* make the odds of repeating the error pretty good.
*/
for(int i = 0; i < 20; i++)
new ErrorTracker();
}
public ErrorTracker() {
/**
* Proof that the error somehow occurs outside of all the code...
* The line "An error took place" is never outputted.
**/
try {
initialize();
} catch (Exception e) {
System.out.println("An error took place");
}
}
public void initialize() {
frame = new JFrame("Apparently this is defective");
JPanel bigPanel = new JPanel(new GridBagLayout());
frame.add(bigPanel);
addRow = new JButton("...");
scrollPanel = new JPanel();
scrollPanel.add(addRow);
pane = new JScrollPane(scrollPanel);
bigPanel.add(pane);
frame.setVisible(true);
panel = new JPanel();
panel.add(addRow);
}
}
私は二回addRow
を追加すると取っているので、この理由の少なくとも一部であることを99%の確信を持って知っていますadd(addRow)
行のいずれかが問題を取り除きます。誰かがそれを変えることを提案する前に、心配しないでください。します。しかし、この質問は主に、なぜこのエラーが発生しているのかを理解するためのものです。 frame.setVisible(true);
を取り除くと、問題はなくなるので、GUIが描画されていると想像するので、自分のスレッドでは2つの同じ要素を追加することに本当に怒っているのです。スタックトレースでは、私は思う...)しかし、私は確かに知っているプロセスを十分に理解していない。
は、ここでスタックトレースです:このエラーがスローされている理由
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at javax.swing.LayoutComparator.compare(Unknown Source)
at javax.swing.LayoutComparator.compare(Unknown Source)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
at java.awt.Window.getMostRecentFocusOwner(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.SequencedEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
誰でも知っていますか?私はこれが別のスレッドにあることについて正しいのでしょうか?もしそうでなければ、なぜスタックトレースは私のコードを参照していませんか?また、なぜそれはエラーの一部を投げるだけですか?
クラスは 'JFrame'を拡張します。プライベートメンバー 'frame'は必要ありません。 – pzaenger
このバグですか? http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8013775 – khelwood
@khelwood私はそれがバグだと思う!これは、スイングスレッドが使用しているメインスレッド内のコンポーネントで 'addRow'を表示させた後、' JPanel'に 'addRow'を追加することでこれが起こっているのでしょうか?私は先に進み、この複製のために "私の問題を解決しました"をクリックしますが、私はこれを完全に把握したいと思っています –