0

wont if filter the nulls out

No.
The code tests one element in the array and then uses another element that wasn't tested.

2

is this a syntax mistake?

No, the syntax is fine. The logic is possibly flawed, though.

if(pieces [a][b]!=null){
  array[a][b]=pieces [9-a][b].print();                    
}

wont if filter the nulls out. and leave me with the objects which i will be able to apply my method to?

No, it won't do anything like that at all. It will check whether the object at pieces[a] is null. If it is not null, it'll call the print method of the object at[9-a] and try to put the result of that into array[a]. You haven't checked either of those to see if there's anything there.

Try amending the code as follows, adding a few lines:


if(pieces [a][b]!=null){
  if array[a][b] == null
    System.out.println("array[a][b] is null!");
  if pieces[9-a][b] == null
    System.out.println("pieces[9-a][b] is null!");
  array[a][b]=pieces [9-a][b].print();                    
}

This won't prevent the exception, but it'll tell you which one was the culprit.

Votes + Comments
Applied it and now i found where i went wrong
Good suggestion :)
0

@neworder:
Could you please post your whole program (that is everything you need to compile it and let it run). I prefer something that I can compile-test-debug-and-play-with.

1
for (a = 0; a <9; a++) {
   for (b = 0; b < 9; b++){

Am I the only one who plays chess on an 8x8 board?

Edited by JamesCherrill: n/a

Votes + Comments
Of course not, there's still me :P
0

alright, jon, nice tip. i did what you said

first time i got all the pieces (all objects).
i did this:

for (a = 0; a <9; a++) {
      			
      			for (b = 0; b < 9; b++){
      			if(pieces [a][b]!=null ){
      			
                               	
                           array[a][b]=pieces[9-a][b].print();

System.out.println(array[a][b] +" is null!");

i got

Wrook||Wknight||WBishop||WKing||WQueen||WBishop||Wknight||Wrook|
| Wpn1 || Wpn2 || Bpn3 || Wpn4 || Wpn5 || Wpn6 || Wpn7 || Wpn8 
| Bpn1 || Bpn2 || Bpn3 || Bpn4 || Bpn5 || Bpn6 || Bpn7 || Bpn8 |
|Brook||Bknight||BBishop||BKing||BQueen||BBishop||Bknight||Brook|

all in one line

the second i got only these

Wrook||Wknight||WBishop||WKing||WQueen||WBishop||Wknight||Wrook|
| Wpn1 || Wpn2 || Wpn3 || Wpn4 || Wpn5 || Wpn6 || Wpn7 || Wpn8

it didnt print any black pieces :(

why doesnt it filter the nulls the second time . to tell you the truth jon, i have got 0 column with nulls in it, i just printed straight from the first column. i dont know why i did that. but still ? why not to filter everything like the first time?

All the program

import java.io.*;
class ChessInterface {


public static void main(String[] arg){
	   	   	   
	   Console console=System.console();
	  	   
		   

	   String chessPiece="";
	   int rowStart = 0;
	   int number=0;  
	 
	   char firstLetter=' ';
	   char secondLetter=' ';
	   boolean pieceDevour=false;
	   int rowEnd=0;
	  

	   ChessPiece[][]pieces =new ChessPiece[9][9];
	   boolean nonCollision=false;
	   boolean secondLetterNotNull=false;   
	   boolean checkMate=false;
	   boolean checkSafe=false;
	   boolean promotion = false;
	   boolean validPromotion=false;
	   boolean moveValid=false;
	   boolean valid1=false;
           boolean valid2 = false;
	   int columnStart = 0;
	   int columnEnd=0;	
			
			   pieces[2][1]=new Pawn("Bpn1");
			   pieces[2][2]=new Pawn("Bpn2");
			   pieces[2][3]=new Pawn("Bpn3");
			   pieces[2][4]=new Pawn("Bpn4");
			   pieces[2][5]=new Pawn("Bpn5");
			   pieces[2][6]=new Pawn("Bpn6");
			   pieces[2][7]=new Pawn("Bpn7");
			   pieces[2][8]=new Pawn("Bpn8");
			   					  
			          pieces [1][1]=new Rook("BR1");
				  pieces [1][2]=new Knight("BN1");
				  pieces [1][3]=new Bishop("BB1");
				  pieces [1][4]=new King("BKing");
				  pieces [1][5]=new Queen("BQueen");;
				  pieces [1][6]=new Bishop("BB2");
				  pieces [1][7]=new Knight("BN2");
				  pieces [1][8]=new Rook("BR2");		  
		  
				   pieces[7][1]=new Pawn("Wpn1");
				   pieces[7][2]=new Pawn("Wpn2");
				   pieces[7][3]=new Pawn("Bpn3");
				   pieces[7][4]=new Pawn("Wpn4");
				   pieces[7][5]=new Pawn("Wpn5");
				   pieces[7][6]=new Pawn("Wpn6");
				   pieces[7][7]=new Pawn("Wpn7");
				   pieces[7][8]=new Pawn("Wpn8");
				   	   	  
				      pieces [8][1]=new Rook("WR1");
					  pieces [8][2]=new Knight("WN1");
					  pieces [8][3]=new Bishop("WB1");
					  pieces [8][4]=new King("WKing");
					  pieces [8][5]=new Queen("WQueen");;
					  pieces [8][6]=new Bishop("WB2");
					  pieces [8][7]=new Knight("WN2");
					  pieces [8][8]=new Rook("WR2");
					  
					  
			        	 
			          
			         

			        	 
		int z=8;
int b=0;	        

	
            while(b<z){
	System.out.println("where now");
            	print(pieces);

		               			         					  
				         					
				         							
				         						       System.out.println("what piece do you want to move?");  
				         						       chessPiece=console.readLine();	
				         						       
				         						       System.out.println("where is the piece located?(type row number)");
				         							   rowStart=Integer.parseInt(console.readLine());
				         							   System.out.println("where is the piece located?(type column letter)");
				         						    	 String columnStartC=console.readLine();
				         							 
				         						       
				         						       System.out.println("where do you want to move it to?(type row number)");
				         							   rowEnd=Integer.parseInt(console.readLine());
				         							   System.out.println("where do you want to move it to?(type column letter)");
				         							   String columnEndC=console.readLine();
				         							   
				         							 
				         							   	  	valid1=true;
                                                                                                              valid2=true;
				         							 
													switch(columnStartC.charAt(0))
			         									{
			         										case 'a':  columnStart=1; break;
			         										case 'b':  columnStart=2; break;
			         										case 'c':  columnStart=3; break;
			         										case 'd':  columnStart=4; break;
			         										case 'e':  columnStart=5; break;
			         										case 'f':  columnStart=6; break;
			         										case 'g':  columnStart=7; break;
			         										case 'h':  columnStart=8; break;
			         										default: valid1=false; break;
			         											
			         									} 
			         									 
				         									switch(columnEndC.charAt(0))
				         									{
				         										case 'a':  columnEnd=1; break;
				         										case 'b':  columnEnd=2; break;
				         										case 'c':  columnEnd=3; break;
				         										case 'd':  columnEnd=4; break;
				         										case 'e':  columnEnd=5; break;
				         										case 'f':  columnEnd=6; break;
				         										case 'g':  columnEnd=7; break;
				         										case 'h':  columnEnd=8; break;
				         										default: valid2=false; break;
				         											
				         									} 
                                                           
				         									 if(valid2==false && valid1==false){
				         									 System.out.println("Column doesnt exist , please try again");
				         									 }
                                                                                                                  
				         									  
                                                                                                                      
				         						                         

                                                          
				         						
				         						
				         		
				         				
				         				 
				         				   firstLetter=pieces[rowStart][columnStart].FindPiece(chessPiece).charAt(0); 
				         				   if(pieces[rowEnd][columnEnd]!=null){					   
				         				   secondLetterNotNull=true;
				         				   secondLetter=pieces[rowEnd][columnEnd].FindPiece(chessPiece).charAt(0);
				         				   }
				         				   else{
				         					   secondLetterNotNull=false;
				         				   }


				         				   
				                                       if(secondLetter!=firstLetter && secondLetterNotNull==true){
				                         	       pieceDevour=true;               	 
				                                       }
				                         	           else
				                         	       {
				         		        	    pieceDevour=false;
				                         	       }
				                            
				         				  
				                            if(pieces[rowStart][columnStart].piecesCollision(pieces,rowStart,columnStart ,columnEnd,rowEnd ) | pieceDevour==true){ 
				                         	   nonCollision=true;
				                            }
				                            else{
				                              nonCollision=false;
				                         	   System.out.println("Sorry, your move cant collide with another piece, try again");
				                            }

				         			  
				         			 
				         		    if(pieces[rowStart][columnStart]!=null && pieces[rowStart][columnStart].isMoveValid(pieces, columnStart,rowStart,columnEnd,rowEnd) &&   

nonCollision){                                                  System.out.println("Piece has moved");	
				         		       
				         			 pieces[rowEnd][columnEnd]=pieces[rowStart][columnStart];
                                                                 pieces[rowStart][columnStart]=null;  
                                                                  moveValid=true; 
                                                               
				         		    }
				         		    else{
				         		    	moveValid=false;
				         		    	System.out.println("Sorry, that piece cant make that move, try again");
				         		    }
				         		    
				         		    		
				         		   
				         		    b++;
				         		
				         		    	System.out.println("where now");
				         		    }
                                                        
                                                 
				         		  
				         	
				         		
				









         	}	
		
               public static void print(ChessPiece[][] pieces){
                  int a=0;
                  int b=0;
                  int num1=9;
                  String rows=" abcdefgh";
                  String nullSquare="| Null |";   
        	  String[][] array=new String[9][9];
		  boolean veryTrue=false;     
  			
      		for (a = 0; a <9; a++) {
      			
      			for (b = 0; b < 9; b++){
      			if(pieces [a][b]!=null ){
      			
                               	
                           array[a][b]=pieces[9-a][b].print();

System.out.println(array[a][b] +" is null!");
                              }   

      				else{
      				
      				     array[a][b]=nullSquare;	
      				
      			}
      			
      		 }
      	}
      		  

      	 rows=" abcdefgh";
      	 num1=9;
           int i=0;
            int j=0;

      		for ( i = 0; i < 9; i++) {
      	
      			
      		for ( j = 1; j < 9; j++) {
      			if(i==0){
      			System.out.print("   "+rows.charAt(j)+"    ");
      			}
      			else{
      				if(i>0)
      			System.out.print(array[i][j]);
      			}
      		}	
      		if(i==0)
      		System.out.println();
              if(i>0)
      		System.out.println(num1-i);
      		}				 
          }


}
			         		     
				         		
				         	







	
				         		
				         		
				         		
				         		
				         		
				         		
				         		
				         		
				         		
				         		
				         		
				         		

	







class Pawn extends ChessPiece{
int columnEnd;
int rowEnd;
int columnStart;
int rowStart;
String name;
boolean valid;
boolean valid2;
boolean valid3;
boolean valid4;        
String chessPiece;
boolean valid5;

public Pawn(String name){
	this.name=name;
}


//pieces, columnStart,rowStart,columnEnd,rowEnd
public boolean isMoveValid(ChessPiece[][] pieces, int columnStart,	int rowStart, int columnEnd, int rowEnd) {

		int deltaX=rowEnd- rowStart;
		deltaX=deltaX<0?-deltaX:deltaX;
		if(deltaX==1 || deltaX==-1){
			valid=true;
                 }
		else
                  {
			valid=false;
		  }
		
			

	


	return  valid ;

}	



String pawn="";

public String print() {
	if(name.charAt(0)=='B'){
		pawn= "| "+ name + " |";   
	}	
	else{
		pawn="| "+ name + " |";
	    }
	return pawn;		    	    
}

	



	public String FindPiece(String chessPiece) {
		return name;
	}
	
	
	
	

	public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStart ,int columnEnd,int rowEnd, int kingRow, int kingColumn) {
	boolean valid = false;		
	   if( (pieces[rowEnd+1][columnEnd-1]==pieces[kingColumn][kingRow] || pieces[rowEnd+1][columnEnd+1]==pieces[kingColumn][kingRow])){
		   valid=true;
	   }
	   else{
		   valid=false;  	
	   }
	return valid;
      }


	
//pieces,rowStart,columnStart ,columnEnd,rowEnd
public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {

		 if(pieces[rowEnd][columnEnd]==null && (rowStart!=8 || rowStart!=1)){
			 valid=true;
		 }
		 else{
		
	        valid=false;
		 }
    
	return valid;
  }

} 




class Rook extends ChessPiece {
	 int columnEnd;
	 int rowEnd;
	 int columnStart;
	 int rowStart;
	 String name; 
	 boolean valid;
	 
	public Rook(String name) {
		this.name=name;
		}

	public boolean isMoveValid(ChessPiece[][] pieces, int columnStartN,
			int rowStart, int columnEndN, int rowEnd)
	{

         if(columnStartN==columnEndN & rowStart!=rowEnd || rowStart==rowEnd & columnStartN!=columnEndN)
            valid=true;

		return valid;

	}



	String rook="";

	
	public String print() {
		if(name.charAt(0)=='B'){
			rook="|Brook|";
		}
		else{
			rook="|Wrook|";
		     
		}
		return rook;
	}

	@Override
	public String FindPiece(String chessPiece) {
	
		return name;
	}



	

	ChessPiece [][] pieces=new ChessPiece[7][7];
	public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStart,int columnEnd,int rowEnd, int kingRow, int kingColumn) {
		

		boolean valid = false;
		boolean valid2=false;
		int j=columnEnd;
		int i=rowEnd;
		for( i=rowEnd;i<pieces.length;i++)
		
				
		   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
			   valid=true;
		   else
			   valid=false;
			  System.out.println("Check: Pawn Threatens king");	
			  
			  
			  for(j=columnEnd;j<pieces[i].length;j++)
					
				   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
					   valid2=true;
				   else
					   valid2=false;
					  System.out.println("Check: Pawn Threatens king");	
			  
			  
		return valid==valid2;
		
	}

	@Override
	public boolean piecesCollision(ChessPiece[][]pieces,int rowStartN,int columnStart ,int columnEnd,int rowEnd) {
		boolean valid = false;
		boolean valid2=false;
		int j=columnStart;
		int i=rowStart;
		
		for( i=rowStartN;i<pieces.length;i++)
		{
				
		   while(pieces[i][j]!=pieces[rowEnd][columnEnd])
			 if(pieces==null || pieces[i][j]==pieces[rowEnd][columnEnd])
				 valid=true;
			 else
				 pieces[i][j]=pieces[rowEnd][columnEnd];
		        valid=false;
				 
			 
		   }
			  
			  
			  for(j=columnStart;j<pieces[j].length;j++){
					
					 if(pieces==null || pieces[i][j]==pieces[rowEnd][columnEnd])
						 valid=true;
					 else
						 pieces[i][j]=pieces[rowEnd][columnEnd];
				        valid=false;
			  }
		return valid==valid2;
	}
}






class Knight extends ChessPiece
{
	 int columnEnd;
	 int rowEnd;
	 int columnStart;
	 int rowStart;
	 String name; 
	 
	public Knight(String name) {
		// TODO Auto-generated constructor stub
		this.name=name;
	}



	public boolean isMoveValid(ChessPiece[][] pieces, int columnStart,int rowStart, int columnEnd, int rowEnd)
	{

		int deltaX=columnEnd-columnStart;
		deltaX=deltaX<0?-deltaX:deltaX;
		int deltaY=rowEnd-rowStart;
		deltaY=deltaY<0?-deltaY:deltaY;
		return (deltaY==2 && deltaX==1) || (deltaY==1 && deltaX==2);


	}


	String Knight="";

	public String print() {
		if(name.charAt(0)=='B'){
		 Knight="|Bknight|";
		}
		else{
			 Knight="|Wknight|";
		}
		return Knight;
	}



	@Override
	public String FindPiece(String chessPiece) {
	
		return Knight;
	}






	ChessPiece [][] pieces=new ChessPiece[7][7];
	public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStart, int columnEnd,int rowEnd, int kingRow, int kingColumn) {
		
       boolean valid=false;
       boolean valid2=false;
       
		int deltaX=columnEnd-columnStart;
		deltaX=deltaX<0?-deltaX:deltaX;
		int deltaY=rowEnd-rowStart;
		deltaY=deltaY<0?-deltaY:deltaY;
		if( (deltaY==2 && deltaX==1) || (deltaY==1 && deltaX==2)){
			valid=true;
		}
		
		
				
		   if(pieces[rowEnd][columnEnd]!=null &&  pieces[rowEnd][columnEnd]==pieces[kingColumn][kingRow])
			   valid2=true;
		   else
			   valid2=false;
			  
		return valid==valid2;
		
	}



	@Override
	public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
		boolean valid;
		valid=true;
		return valid;
	}


}










class Bishop extends ChessPiece
{
	 int columnEnd;
	 int rowEnd;
	 int columnStart;
	 int rowStart;
	 String name; 
	 boolean valid;
	
	public Bishop(String name) {
		this.name=name;
	}


	public boolean isMoveValid(ChessPiece[][] pieces, int columnStart,
			int rowStart, int columnEnd, int rowEnd)
	{

		int deltaX=columnEnd-columnStart;
		deltaX=deltaX<0?-deltaX:deltaX;
		int deltaY=rowEnd-rowStart;
		deltaY=deltaY<0?-deltaY:deltaY;
		return deltaY==deltaX;


	}




	String Bishop="";
	public String print() {
		if(name.charAt(0)=='B'){
			 Bishop="|BBishop|";
		     }
		
		else{
			Bishop="|WBishop|";
		}
		return Bishop;
	}



	@Override
	public String FindPiece(String chessPiece) {
		
		return Bishop;
	}





	
	
  
	public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStart ,int columnEnd,int rowEnd, int kingRow, int kingColumn) {
		

	
	  valid = false;
		
		for(int i=rowEnd;i<pieces.length;i++)
			for(int j=columnEnd;j<pieces[i].length;j++)
				
		   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
			   valid=true;
		   else
			   valid=false;
			  	
		return valid;
		
	}



	@Override
	public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
		for(int i=rowEnd;i<pieces.length;i++)
			for(int j=columnEnd;j<pieces[i].length;j++)
				 while(pieces[i][j]!=pieces[rowEnd][columnEnd]){
					if(pieces[i][j]==null || pieces[i][j]==pieces[rowEnd][columnEnd])
						valid=true;
					else
						pieces[i][j]=pieces[rowEnd][columnEnd];
					    valid=false;
				 }
	
	
		return valid;
	}






}










class Queen extends ChessPiece{

	 int columnEnd;
	 int rowEnd;
	 int columnStart;
	 int rowStart;
	 String name; 
	 boolean valid;
	 
	public Queen(String name) {
		this.name=name;
	}

	public boolean isMoveValid(ChessPiece[][] pieces, int columnStartN,
			int rowStart, int columnEndN, int rowEnd)
	{

		int deltaX=columnEndN-columnStartN;
		deltaX=deltaX<0?-deltaX:deltaX;
		int deltaY=rowEnd-rowStart;
		deltaY=deltaY<0?-deltaY:deltaY;
		 if(deltaY==deltaX);
		 valid=true;
		 
        if(columnStartN==columnEndN & rowStart!=rowEnd || rowStart==rowEnd & columnStartN!=columnEndN && valid)
            valid=true;
        

		
		 
		 
         return valid;
	}


	String Queen="";

	public String print() {
		if(name.charAt(0)=='B'){
		 Queen="|BQueen|";
		}
		else{
		 Queen="|WQueen|";
		}
		return Queen;
	}

	@Override
	public String FindPiece(String chessPiece) {
		
		return Queen;
	}


	ChessPiece [][] pieces=new ChessPiece[7][7];
	public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStartN ,int columnEndN,int rowEnd, int kingRow, int kingColumn) {
		
		
		boolean valid = false;
		boolean valid2=false;
		boolean valid3=false;
		int j=columnEndN;
		int i=rowEnd;
		for( i=rowEnd;i<pieces.length;i++)
		
				
		   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
			   valid=true;
		   else
			   valid=false;
		
			  
			  
			  for(j=columnEndN;j<pieces[i].length;j++)
					
				   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
					   valid2=true;
				   else
					   valid2=false;
					  
			  
		

		for( i=rowEnd;i<pieces.length;i++)
			for( j=columnEndN;j<pieces[i].length;j++)
				
		   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
			   valid3=true;
		   else
			   valid3=false;
		
		
		return valid==valid2 && valid2==valid3;
		
	}

	@Override
	public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStartN,int columnEnd,int rowEnd) {
		boolean valid = false;
		boolean valid2=false;
		boolean valid3=false;
		int j=columnStartN;
		int i=rowStart;
		
		for( i=rowStart;i<pieces.length;i++)
		{
				
		   while(pieces[i][j]!=pieces[rowEnd][columnEnd]){
			 if(pieces==null || pieces[i][j]==pieces[rowEnd][columnEnd])
				 valid=true;
			 else
				 pieces[i][j]=pieces[rowEnd][columnEnd];
		        valid=false;
				 
		   }
		   }
			  
			  
			  for(j=columnStartN;j<pieces[j].length;j++){
					
					 if(pieces==null || pieces[i][j]==pieces[rowEnd][columnEnd])
						 valid=true;
					 else
						 pieces[i][j]=pieces[rowEnd][columnEnd];
				        valid=false;
			  }
			  
			 j=columnStartN;
			 i=rowStart;
				for( i=rowEnd;i<pieces.length;i++)
					for( j=columnEnd;j<pieces[i].length;j++)
						 while(pieces[i][j]!=pieces[rowEnd][columnEnd]){
							if(pieces[i][j]==null || pieces[i][j]==pieces[rowEnd][columnEnd])
								valid3=true;
							else
								pieces[i][j]=pieces[rowEnd][columnEnd];
							    valid3=false;
			  
		
	}
				return valid==valid2 && valid2==valid3;
	}
}











 class King extends ChessPiece {
	 int columnEnd;
	 int rowEnd;
	 int columnStart;
	 int rowStart;
	 String name; 
	 boolean valid;
	 
	public King(String name) {
		this.name=name;

	}

	public boolean isMoveValid(ChessPiece[][] pieces, int columnStart,
			int rowStart, int columnEnd, int rowEnd) {
	{

		int deltaX=columnEnd-columnStart;
		deltaX=deltaX<0?deltaX:deltaX;
		
		if(deltaX==1)
			valid=true;
		
		int deltaY=rowEnd-rowStart;
		deltaY=deltaY<0?deltaY:deltaY;
		
		if(deltaY==1&& valid)
		 valid=true;
		return valid;


	}


	}

	String King="";
	public String print() {
		if(name.charAt(0)=='B'){
			 King="|BKing|";
		     }
		
		else{
			King="|WKing|";
		}
		return King;
	}

	@Override
	public String FindPiece(String chessPiece) {
	
		return King;
	}



	ChessPiece [][] pieces=new ChessPiece[7][7];
	public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStartN, int columnEndN,int rowEnd, int kingRow, int kingColumn) {
		

		boolean valid = false;
		int j=columnEndN;
		boolean valid2=false;
		for(int i=rowEnd;i<pieces.length;i=1)
	   
				
		   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
			   valid=true;
		   else
			   valid=false;
			  System.out.println("Check: Pawn Threatens king");	

			  
				for(int i=rowEnd;i<pieces.length;i=1)
					for( j=columnEndN;j<pieces[i].length;j=1)
						
				   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
					   valid2=true;
				   else
					   valid2=false;
		
		
		
		return valid==valid2;
		
	}

	@Override
	public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
		boolean valid = false;
		boolean valid2=false;
		boolean valid3=false;
		int j=columnStart;
		int i=rowStart;
		
		for( i=rowStart;i<pieces.length;i=1)
		{
				
		   while(pieces[i][j]!=pieces[rowEnd][columnEnd]){
			 if(pieces==null || pieces[i][j]==pieces[rowEnd][columnEnd])
				 valid=true;
			 else
				 pieces[i][j]=pieces[rowEnd][columnEnd];
		        valid=false;
				 
		   }
		   }
			  
			  
			  for(j=columnStart;j<pieces[j].length;j=1){
					
					 if(pieces==null || pieces[i][j]==pieces[rowEnd][columnEnd])
						 valid=true;
					 else
						 pieces[i][j]=pieces[rowEnd][columnEnd];
				        valid=false;
			  }
			  
			 j=columnStart;
			 i=rowStart;
				for( i=rowEnd;i<pieces.length;i=1)
					for( j=columnEnd;j<pieces[i].length;j=1)
						 while(pieces[i][j]!=pieces[rowEnd][columnEnd]){
							if(pieces[i][j]==null || pieces[i][j]==pieces[rowEnd][columnEnd])
								valid3=true;
							else
								pieces[i][j]=pieces[rowEnd][columnEnd];
							    valid3=false;
			  
		
	}
				return valid==valid2 && valid2==valid3;
	
	}
}












abstract class ChessPiece {
	  
	 abstract public  boolean isMoveValid(ChessPiece[][] pieces,int columnStart, int rowStart, int columnEnd, int rowEnd);
	
	  abstract public String print();

	 abstract public String FindPiece(String chessPiece); 
	 
	 abstract public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStart, int columnEnd,int rowEnd, int kingRow, int kingColumn);

	 abstract public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd);
	 

   	}

just copy and paste it to a notepad.
i need to work on the checkmate aspect by modifying the code in the main class and puttting it in another loop

Edited by NewOrder: n/a

0

i have got a chessboard of pieces:
pieces[2][1]=new Pawn("Bpn1");
pieces[2][2]=new Pawn("Bpn2");
pieces[2][3]=new Pawn("Bpn3");
pieces[2][4]=new Pawn("Bpn4");
pieces[2][5]=new Pawn("Bpn5");
pieces[2][6]=new Pawn("Bpn6");
pieces[2][7]=new Pawn("Bpn7");
pieces[2][8]=new Pawn("Bpn8");

pieces [1][1]=new Rook("BR1");
pieces [1][2]=new Knight("BN1");
pieces [1][3]=new Bishop("BB1");
pieces [1][4]=new King("BKing");
pieces [1][5]=new Queen("BQueen");;
pieces [1][6]=new Bishop("BB2");
pieces [1][7]=new Knight("BN2");
pieces [1][8]=new Rook("BR2");

pieces[7][1]=new Pawn("Wpn1");
pieces[7][2]=new Pawn("Wpn2");
pieces[7][3]=new Pawn("Wpn3");
pieces[7][4]=new Pawn("Wpn4");
pieces[7][5]=new Pawn("Wpn5");
pieces[7][6]=new Pawn("Wpn6");
pieces[7][7]=new Pawn("Wpn7");
pieces[7][8]=new Pawn("Wpn8");

pieces [8][1]=new Rook("WR1");
pieces [8][2]=new Knight("WN1");
pieces [8][3]=new Bishop("WB1");
pieces [8][4]=new King("WKing");
pieces [8][5]=new Queen("WQueen");;
pieces [8][6]=new Bishop("WB2");
pieces [8][7]=new Knight("WN2");
pieces [8][8]=new Rook("WR2");

i tell java to print me the table, with a filtering condition
for (a = 0; a <9; a++) {

for (b = 0; b < 9; b++){
if(pieces [a]!=null ){
first time it prints the table well.
pieces[rowEnd][columnEnd]=pieces[rowSt…
second time i move a piece and it changes the pieces and throws a nullerpoint exception
i move Black pawn .
the White pawn becomes null.
and when it reaches last White pawn (Wpn8)
it prints nulls and then throws the exception at the column that i moved the piece

why this distorts my array so much
pieces[rowEnd][columnEnd]=pieces[rowSt…

0

the White pawn becomes null.

How does it become null?
Why doesn't your code test for null before using a variable that can be null?

0

okay look carefull at the method print()

public static void print(ChessPiece[][] pieces, int rowStart, int columnStart , int columnEnd, int rowEnd){
                  int a=0;
                  int b=0;
                  int num1=9;
                  String rows=" abcdefgh";
                  String nullSquare="| Null |";   
        	  String[][] array=new String[9][9];
		  boolean veryTrue=false;     
  			
      		for (a = 1; a <9; a++) {
      			
      			for (b = 1; b < 9; b++){
      			if(pieces [9-a][b]!=null ){ // this filters all the nulls.
i mean there is no way any null could get in and use the print method. no way!!!
      			
                               	
                           array[a][b]=pieces[a][b].print();

                         System.out.println(array[a][b] +" is a piece!");
                              }   

      				else{
      				 System.out.println(array[a][b] +" is null!");
      				     array[a][b]=nullSquare;	
      				
      			}
      			
      		 }
      	}
      		  

      	 rows=" abcdefgh";
      	 num1=9;
           int i=0;
            int j=0;

      		for ( i = 0; i < 9; i++) {
      	          
      			
      		for ( j = 1; j < 9; j++) {
                       
                        
      			if(i==0){
      			System.out.print("    "+rows.charAt(j)+"   ");
                       
      			}
      			else{
      				if(i>0)
      			System.out.print(array[i][j]);
      			}
                      
      		
                  
                }	
                
      		if(i==0)
      		 System.out.println();
              if(i>0)
      		System.out.println(i);
      		}
                 System.out.println("                                                                             \n ");
               System.out.println("Moves made: ");
                System.out.print("From Row: "+rowStart+"  From Column: "+columnStart +"    Square presentation: ");
                 
		System.out.println(array[rowStart][columnStart]);
                System.out.print("From Row: "+rowEnd+"  From Column: "+columnEnd +"    Square presentation: ");
                System.out.println(array[rowEnd][columnEnd]);  	
                System.out.println("                                                                             \n "); 	 
          }


}

that method does 2 things.
accepts the array pieces and creates a new printable array called "array".
filters what is array and what is not

first time it works.
second time it has a problem with the new position changed.

i tested the whole thing with the debugging method you taught me. once it reaches that position it throws the error.

everything that changes the array is this redefinition that is in the method that tests if the move was valid and then redefines the new postion

pieces[rowEnd][columnEnd]=pieces[rowStart][columnStart];
0
if(pieces [9-a][b]!=null ){ // this filters all the nulls.
i mean there is no way any null could get in and use the print method. no way!!!
 
 
                           array[a][b]=pieces[a][b].print();

You test one element: pieces [9-a] and then use another: pieces[a]

0

i did!
i am going to test how all the pieces work.
then i will work on mate and checkmate.
thanks Norm1, nice avatar btw

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.