Exception in thread main java.lang.StackOverflowError

flerair

Mitglied
Ich bekomme die Fehlermeldung

Excpetion in thread main java.lang.StackOverflowError
at java.lang.System.arraycopy(Native Method)
at java.lang.String.getChars(String.java:826)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:416)
at java.lang.StringBuffer.append(StringBuffer.java:237)
at java.lang.StringBuffer.<init>(StringBuffer.java:121)
at AsciImage.setPixel(AsciImage.java:80)
at AsciImage.fill(AsciImage.java:95)
at AsciImage.fill(AsciImage.java:97)
at AsciImage.fill(AsciImage.java:99)
at AsciImage.fill(AsciImage.java:97)
at AsciImage.fill(AsciImage.java:99)
at AsciImage.fill(AsciImage.java:97)
at AsciImage.fill(AsciImage.java:99)
at AsciImage.fill(AsciImage.java:97)
at AsciImage.fill(AsciImage.java:99)
at AsciImage.fill(AsciImage.java:97)
at AsciImage.fill(AsciImage.java:99)
at AsciImage.fill(AsciImage.java:97)
at AsciImage.fill(AsciImage.java:99)

Die 97 und 99 wiederholt sich dann öfter.

Hier ist mein Code:

Java:
public class AsciImage {
	
	private int width;
	private String image;
	AsciImage(int h){
		width = 0;
		image = "";
	}
	
	public boolean addLine(String line){
		if(width == 0){
			if(line.length() < 1){
				return false;
			}else{
				width = line.length();
				image = line;
				return true;

			}
		}else if(width != line.length()){
			return false;
		}else{
			image += line;
			return true;
		}
	}
	public int getWidth(){
		return width;
	}
	public int getHeight(){
		if(width == 0){
			return 0;
		}else{
			return image.length()/width;	
		}
		
	}
	
	public String toString(){
		String flood = "";
		for(int i=0; i< getHeight();i++){
			if(i+1 == getHeight()){
				flood += image.substring(i*width,i* width + width);
			}
			flood += image.substring(i*width,i * width + width) + "\n";
		}
		return flood;
	}
	
	public int getUniqueChars(String image){
		
		String tmpUniqueChars = "";
		for(int i=0;i<image.length();i++){
			if(image.contains("" + image.charAt(i)) && !tmpUniqueChars.contains("" + image.charAt(i))){
				
				tmpUniqueChars += image.charAt(i);
			}
	}
	return tmpUniqueChars.length(); 
		
	}
	
	public void flipV(){
		String tmpFliped ="";
		
		for(int i = getHeight()-1;i>=0;i--){
			tmpFliped += image.substring((i*width),(i*width+width));
		}
		image = tmpFliped;
	}
	
	public void transpose(){
		
	}
	
	private void setPixel(int x, int y, char c){
		if (((0 <= x) && (x <= width)) && ((0 <= y) && (y <= getHeight()))) {

			StringBuffer newPic = new StringBuffer(image);
			newPic.setCharAt(y * width + x, c);
		}
	}
	private  char getPixel(int x, int y){
		char c = ' ';
		
		if( (x>=0 && y>=0) && (x<width && y<getHeight())){
			c = image.charAt(x+y*width);
		}
		return c;
	}
	
	public void fill(int x, int y, char c){
		char oldPicture = getPixel(x,y);
		setPixel(x,y,c);
		if( x-1 >= 0  && getPixel(x-1,y) == oldPicture){
			fill(x-1, y, c);	
		}if(x+1 < width && getPixel(x+1,y) == oldPicture){
			fill(x+1, y, c);
		}if(y-1 >= 0  && getPixel(x,y-1) == oldPicture){
			fill(x, y-1, c);
		}if(y+1 < getHeight() && getPixel(x,y+1) == oldPicture){
			fill(x, y+1, c);
		}else return;
		
	}
}
Java:
import java.util.Scanner;


public class AsciShop {
	
	static AsciImage p;
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int Zeilen = 0;
		boolean flag = true;
		if(sc.hasNext("read")){
			sc.next();
			Zeilen = sc.nextInt();
			p = new AsciImage(Zeilen);
			sc.nextLine();
			
			while(sc.hasNextLine()){
				if(Zeilen == p.getHeight()){
					if(sc.hasNext("transpose")){
						System.out.println("Transposed");
						break;
					}else if(sc.hasNext("flip-v")){
						System.out.println("flip-v");
						break;
					}else if(sc.hasNext("uniqueChars")){
						System.out.println("uniqueChars");
						break;
					}else if(sc.hasNext("fill")){
						p.fill(0,0,'#');
						break;
					}else{
						System.out.println("INPUT MISMATC2H");
						break;
					}
				}else{
					flag = p.addLine(sc.nextLine());
					if(!flag){
						System.out.println("inputmismatchorino2");
						break;
					}
				}
			}
			
		}
		
	}
}


Ich verstehe einfach nicht wieso ich diese Fehlermeldung bekomme.
Jeder Tipp ist hilfreich.
 
Das sieht in deiner Fill Methode irgendwie nach einer ungewollten schleife aus, die dann dementsprechend den Stack überflutet

ich vermute es liegt an irgendeiner deiner if Anweisungen im block wie es auch schon im Stacktrace steht, hast du diese Methode und deren Anweisungen mal gedebuggt ?

falls ja wie werden die werte der Variablen belegt ? der Stacktrace zeigt ja schon woran es in etwa liegt und durch welche Methoden es sich zieht.
 
Ja aber ich kann den Fehler nicht erkennen. Ich glaube der Fehler liegt in der setPixel. Wird der neue Pixel auch gesetzt? Ich habe immer noch Probleme mit dem Stringbuilder. Kannst du vielleicht einen Fehler in der setPixel Methode erkennen?
 
Ich habe den Fehler mitlerweile gefunden


Java:
private void setPixel(int x, int y, char c){
		String helpString = image.substring(0,x+y*getWidth())+c+image.substring(x+1+y*getWidth());
        image=helpString;
	}






Java:
public void fill(int x, int y, char c){
		char oldPicture = getPixel(x,y);
		setPixel(x,y,c);
		if(y+1 < getHeight() && getPixel(x,y+1) == oldPicture){
			fill(x,y+1,c);
		}if(y-1 >= 0 && getPixel(x,y-1) == oldPicture){
			fill(x,y-1,c);
		}if(x+1 < getWidth() && getPixel(x+1,y) == oldPicture){
			fill(x+1, y, c);
		}if(x-1 >= 0 && getPixel(x-1,y) == oldPicture){
			fill(x-1,y,c);
		}
		
	}
 

Zurück
Oben