当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Can't find the mistake (StackOverflowError)

问题描述:

I'm writing a Sudoku-solving program, but I'm encountering the following error:

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)

I can't find the reason for this error. The code of my programm is as follows:

package code;

import java.awt.Color; //Import ntiger 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; //berfrfungsfeld 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 Oberflche

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

//Fgt 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 berprfungs 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){ //berprfung ob Lsung mglich ist fr eine bestimmte Zahl bei einem bestimmten Feld

boolean moeglich = false; //Insgesammt mglich

boolean wmoeglich = true; //Waagrecht mglich

boolean smoeglich = true; //Senkrecht mglich

boolean fmoeglich = true; //Feld mglich

//berprft waagrecht und senkrecht ob mglich

for(int i=0;i<9;i++){

if(matrix[x][i]==solution){

wmoeglich = false;

}

if(matrix[i][y]==solution){

smoeglich = false;

}

}

//Sucht ntiges Feld und berprft 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 berprfung fehlgeschlagen Lsung nicht mglich

if(smoeglich == true && wmoeglich == true && fmoeglich == true){

moeglich = true;

}

return moeglich;

}

public void search(int i, int j){ //Lsen 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 berprfung erfolgreich war

matrix[i][j]=l; //Schreibt Lsung in Matrix

field[i][j].setText(""+l); //Und Schreibt Lsung 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 zurck

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 (Fhrt die ntigen 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 ausfhren

master();

}

else if(source == reset){

reset();

}

}

}

What could be the cause of this error? Sorry about the comments in the code being in German, but if you want I could translate them into English.

网友答案:

From what I recall, "AWT-EventQueue-0" exceptions are from when you do something asynchronous with the HashMap. I would highly recommend that you separate the view from the model! And if necessary, separate the controller as well:

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Do you get a number from what line the execution stoped? As this would really help identify from where the problem starts. Also you could do a debugging on the code. I recommend the Eclipse (Indigo), as it has a good debugger built in and a lot of other neat stuff. It grows on you!


TIPS

Try following the question form:

http://stackoverflow.com/questions/how-to-ask

because it's really hard when you leave 3 pages of code that's not relevant at all! (About the language; the comment and variable names in germen, I think, is not so hard to understand. I mean, I know what you're instantiating so the names could be "asdf" or "foobar" for all I care.)

Also, please do put the constructor for the class as the first method after deceleration of class variables; as this makes it much easier to quickly identify it.

分享给朋友:
您可能感兴趣的文章:
随机阅读: