- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在编写一个数独解决程序,但我遇到了以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.HashMap.hash(Unknown Source)
at java.util.HashMap.getEntry(Unknown Source)
at java.util.HashMap.get(Unknown Source)
at sun.awt.AppContext.get(Unknown Source)
at sun.awt.SunToolkit.flushPendingEvents(Unknown Source)
at java.awt.EventQueue.postEvent(Unknown Source)
at java.awt.EventQueue.invokeLater(Unknown Source)
at javax.swing.SwingUtilities.invokeLater(Unknown Source)
at javax.swing.text.DefaultCaret.changeCaretPosition(Unknown Source)
at javax.swing.text.DefaultCaret.handleSetDot(Unknown Source)
at javax.swing.text.DefaultCaret.setDot(Unknown Source)
at javax.swing.text.DefaultCaret$Handler.insertUpdate(Unknown Source)
at javax.swing.text.AbstractDocument.fireInsertUpdate(Unknown Source)
at javax.swing.text.AbstractDocument.handleInsertString(Unknown Source)
at javax.swing.text.AbstractDocument.insertString(Unknown Source)
at javax.swing.text.PlainDocument.insertString(Unknown Source)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.setText(Unknown Source)
我找不到这个错误的原因。我的程序代码如下:
package code;
import java.awt.Color; //Import nötiger Klassen
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SOLVE implements ActionListener{
private int[][] matrix; //Eingelesenes Sudokufeld
private boolean[][]input; //Überfrüfungsfeld zur bestimmung von eingegebenen Zahlen
private JFrame fenster; //GUI Fenster
private JTextArea field[][]; //GUI Text Sudoku Feld
private JButton start; //Start Button
private JButton reset;
public void gui(){ //Erstellt die Grafische Oberfläche
Border b1 = BorderFactory.createLineBorder( Color.black );
//Erstellt Fenster
fenster = new JFrame();
fenster.setLocation(100, 100);
fenster.setSize(500, 320);
fenster.getContentPane().setLayout(null);
fenster.setTitle("Sudoku Solver");
//Erstellt Sudoku TextFelder
field = new JTextArea[9][9];
int x = 0; //x-Koordinate
for(int i=0;i<9;i++){
int y = 0; //y-Koordinate
for(int j=0;j<9;j++){
field[i][j] = new JTextArea();
field[i][j].setLocation((200+x),(30+y));
field[i][j].setSize(20, 20);
field[i][j].setText("0");
field[i][j].setBorder(b1);
field[i][j].setVisible(true);
fenster.add(field[i][j]);
if(j==2||j==5){
y=y+32;
}
else{
y=y+25;
}
}
if(i==2||i==5){
x=x+32;
}
else{
x=x+25;
}
}
//Erstellt Start Button
start = new JButton();
start.setText("Solve");
start.setSize(150,50);
start.setLocation(30,30);
start.addActionListener(this);
//Erstellt Reset Button
reset = new JButton();
reset.setText("Reset");
reset.setSize(150,50);
reset.setLocation(30,100);
reset.addActionListener(this);
//Fügt Komponenten zum Fenster hinzu
fenster.add(reset);
fenster.add(start);
fenster.setVisible(true);
}
public SOLVE(){ //Startet Klasse
gui();
matrix = new int[9][9];
input = new boolean[9][9];
}
public void copy(){ //Einlesen der TextFelder in die Matrix une Überprüfungs Matrix
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
matrix[i][j]=Integer.parseInt(field[i][j].getText());
if(matrix[i][j]!=0){
input[i][j]=false;
}
else{
input[i][j] = true;
}
}
}
}
public boolean check(int x,int y, int solution){ //Überprüfung ob Lösung möglich ist für eine bestimmte Zahl bei einem bestimmten Feld
boolean moeglich = false; //Insgesammt möglich
boolean wmoeglich = true; //Waagrecht möglich
boolean smoeglich = true; //Senkrecht möglich
boolean fmoeglich = true; //Feld möglich
//Überprüft waagrecht und senkrecht ob möglich
for(int i=0;i<9;i++){
if(matrix[x][i]==solution){
wmoeglich = false;
}
if(matrix[i][y]==solution){
smoeglich = false;
}
}
//Sucht nötiges Feld und überprüft dieses
//Oberste Reihe
if(x<3){
//linkes Feld
if(y<3){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//mittleres Feld
else if(y<6&& y>2){
for(int i=0;i<3;i++){
for(int j=3;j<6;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//rechtes Feld
else if(y<9 && y>5){
for(int i=0;i<3;i++){
for(int j=6;j<9;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
}
//mittlere Reihe
else if(x<6 && x>2){
//linkes Feld
if(y<3){
for(int i=3;i<6;i++){
for(int j=0;j<3;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//mittleres Feld
else if(y<6 && y>2){
for(int i=3;i<6;i++){
for(int j=3;j<6;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//rechtes Feld
else if(y<9 && y>5){
for(int i=3;i<6;i++){
for(int j=6;j<9;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
}
//unterste Reihe
else if(x<9 && x>5){
//linkes Feld
if(y<3){
for(int i=6;i<9;i++){
for(int j=0;j<3;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//mittleres Feld
else if(y<6 && y>2){
for(int i=6;i<9;i++){
for(int j=3;j<6;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
//rechtes Feld
else if(y<9 && y>5){
for(int i=6;i<9;i++){
for(int j=6;j<9;j++){
if(matrix[i][j]==solution){
fmoeglich = false;
}
}
}
}
}
//wenn eine Überprüfung fehlgeschlagen Lösung nicht möglich
if(smoeglich == true && wmoeglich == true && fmoeglich == true){
moeglich = true;
}
return moeglich;
}
public void search(int i, int j){ //Lösen des Sudokus (Eigentlich Rekursiv)
if(input[i][j]==true){ //Wenn keine menschliche eingabe
int l = 1;
for(l=1;l<10;l++){
if(check(i,j,l)==true){ //Wenn Überprüfung erfolgreich war
matrix[i][j]=l; //Schreibt Lösung in Matrix
field[i][j].setText(""+l); //Und Schreibt Lösung in TextFeld
//Nachfolger suchen
if(i<=7){
search(i+1,j);
}
else if(i==8 && j<=7){
search(i-8,j+1);
}
else{
break;
}
}
//Da Rekursion nicht funktionierte hier den Schritt zurück
else if(check(i,j,l)==false && l==9){
matrix[i][j]=0;
if(i>0){
search(i-1,j);
}
else if(i==0 && j>0){
search(i+8,j-1);
}
else{
break;
}
}
}
}
else{
//Nachfolger suchen
if(i<=7){
search(i+1,j);
}
else if(i==8 && j<=7){
search(i-8,j+1);
}
else{}
}
}
public void reset(){ //Resettet Fenster und Matrix
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
field[i][j].setText("0");
matrix[i][j]=0;
input[i][j]=true;
}
}
}
public void master(){ //Master Methode (Führt die nötigen Methoden nacheinander aus)
System.gc();
copy();
search(0,0);
}
public static void main(String[] args) { //Main Methode (Start Methode)
new SOLVE();
}
public void actionPerformed(ActionEvent evt){ //Button Abfrage
Object source = evt.getSource();
if(source==start){ //Wenn Button 'start' keklckt wird Methode main ausführen
master();
}
else if(source == reset){
reset();
}
}
}
这个错误的原因可能是什么?很抱歉代码中的注释是德语,但如果您需要,我可以将它们翻译成英语。
最佳答案
据我所知,“AWT-EventQueue-0”异常是在您使用 HashMap 执行异步操作时产生的。我强烈建议您将 View 与模型分开!如果有必要,还要分离 Controller :
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
您是否从执行停止的那一行得到了一个数字?因为这确实有助于确定问题的根源。您也可以对代码进行调试。我推荐 Eclipse (Indigo),因为它有一个很好的内置调试器和许多其他巧妙的东西。它在你身上成长!
小贴士
尝试按照问题形式:
https://stackoverflow.com/questions/how-to-ask
因为当您留下 3 页完全不相关的代码时,这真的很难!(关于语言;我认为德语中的注释和变量名称并不难理解。我的意思是,我知道您正在实例化什么,因此名称可以是“asdf”或“foobar”,我只关心。)
此外,请务必将类的构造函数放在类变量减速后的第一个方法中;因为这使得快速识别它变得容易得多。
关于java - 找不到错误 (StackOverflowError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22996820/
我有一个使用 play scala 2.0 开发的项目,它工作正常,我需要将版本升级到 2.3.8。所以我通过此链接迁移了我的应用程序版本 https://www.playframework.com/
为此我需要一些帮助。 首先我有一个 BinarySearchTree 类 import java.util.ArrayList; import java.util.List; public class
我正在尝试使用递归方法计算字母“e”在给定字符串中出现的次数。我的测试字符串是 请数我的 e!。这是到目前为止的代码: public static int showE(String s, int co
您将如何调整这个简单的递归示例,以便进行尾调用优化(而不是 StackOverflowError)? count 0 = 0 count n = succ (count (pred n)) count
我根据自身定义流(递归定义)。当试图访问流的第二个元素时,StackOverflowError被抛出。来自Scala控制台的代码: scala> val s1 = Stream.iterate(1)(
我在 Java 中有一个 StackOverflowError,它没有告诉我我自己的代码中的任何一行,堆栈跟踪的相关部分是: java.lang.StringBuilder.append(String
这个隐式 val 如何导致 StackOverFlowError? (削减我的原始代码,仍然导致错误) object Complicit { // a class with name, defau
在 Groovy Console我有这个: import groovy.util.* import org.codehaus.groovy.runtime.* def gse = new Groovy
为什么此代码片段执行会导致 StackOverflowError: lazy val primes: Stream[Int] = 2 #:: Stream.from(3, 2) filter { pc
(reduce concat (repeat 10000 [])) 我知道展平可能是执行此操作的更好方法,但我仍然很好奇为什么这会导致错误。 最佳答案 因为concat产生一个惰性序列。 所以,当你打
当我使用 (avg-bids 4000 10 5) 调用以下 Clojure 代码时,会导致 java.lang.StackOverflowError。我试图找出原因,因为 sum-bids 是作为尾
我在运行递归程序时遇到了 Java StackOverFlowError。程序正确,需要实现递归。我尝试使用命令查找当前堆栈大小 java -XX:+PrintFlagsFinal -vers
美好的一天!运行快速排序算法时,我收到 StackOverflowError 错误。当数组中的元素 > 50 000 时,会发生此错误。 我的代码如下: public void recQuickSor
我正在删除一个 Android 应用程序,其中有一个无限重复的动画,导致 StackOverflowError。当同一对象上的另一个动画开始时,它会执行此操作。 private fun pulse()
我创建了一个公共(public)类PermissionManager来管理来自一个地方的所有权限,通常它工作正常,但上传后它显示崩溃分析的错误报告我无法重现,详细信息是下面提到 Fatal Excep
我得到了一组称为“字典”的字符串,存储为字段,代表单词字典。 我要编写一个方法,它接受一个字符串参数(“短语”)并返回一个包含字典集中所有单词的集合,这些单词可以通过重新排列给定短语中的字符来实现。基
我正在尝试生成一个相对较小(1296 个元素)的向量列表,本质上枚举从 [0 0 0 0] 到 [5 5 5 5] 的 4 个基数 6 数字 [0 0 0 0], [1 0 0 0] ... [5 0
我正在尝试用java编写二进制插入排序。 public static int binarySearch(double[] a, int max, int min, double k) {
我目前正在 Clojure 中实现欧拉项目问题之一的解决方案,即埃拉托斯特尼筛法 ( https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes )。这是我
我遇到了与错误递归和 StackOverflowError 相关的编程问题。我在一个单独的线程中处理了这个案例: public void subscribe(final String channel)
我是一名优秀的程序员,十分优秀!