i read this on the net. and i cant understand why i have this mistake?
Thrown when an application attempts to use null in a case where an object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.Applications should throw instances of this class to indicate other illegal uses of the null object.
i think the problem is here:
if(pieces[rowStart][columnStart]!=null && pieces[rowStart][columnStart].isMoveValid(pieces, columnStart,rowStart,columnEnd,rowEnd) && nonCollision){
System.out.println(" "+ columnStart+" "+rowStart+" "+columnEnd+" "+rowEnd);
pieces[rowEnd][columnEnd]=pieces[rowStart][columnStart];
}
else{
moveValid=false;
System.out.println("Sorry, that piece cant make that move, try again");
}
might it be because i try to manipulate the array in that way (piecies)? and then send to print..
i tried to run my code. it works, but when it comes back , second type through the loop , it gives me that mistake.
it says 2 lines.
chessInterface.print
chessInterface.main
here is the code i am trying to run. all the methods work the first time. it prints me the screen and asks the questions then coverts them to numbers
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");
do{
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(" "+ columnStart+" "+rowStart+" "+columnEnd+" "+rowEnd);
pieces[rowEnd][columnEnd]=pieces[rowStart][columnStart]; // is this line causes the problem?
pieces[rowStart][columnStart]=null;
}
else{
moveValid=false;
System.out.println("Sorry, that piece cant make that move, try again");
}
System.out.println(" "+ columnStart+" "+rowStart+" "+columnEnd+" "+rowEnd);
}while(moveValid!=true);
}
second method outside the main class, simply prints the board. 1st time it prints with, no problems.
public static void print(ChessPiece[][] pieces){
String[][] array=new String[9][9];
String nullSquare="| Null |";
for (int a = 0; a <pieces.length; a++) {
for (int b = 0; b < pieces[a].length; b++){
if(pieces [a][b]!=null){
array[a][b]=pieces [9-a][b].print();
}
else{
array[a][b]=nullSquare;
}
}
}
String rows=" abcdefgh";
int num1=9;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; 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);
}
}
board output is like this:
a b c d e f g h
|Wrook||Wknight||WBishop||WKing||WQueen||WBishop||Wknight||Wrook|8
| Wpn1 || Wpn2 || Bpn3 || Wpn4 || Wpn5 || Wpn6 || Wpn7 || Wpn8 |7
| Null || Null || Null || Null || Null || Null || Null || Null |6
| Null || Null || Null || Null || Null || Null || Null || Null |5
| Null || Null || Null || Null || Null || Null || Null || Null |4
| Null || Null || Null || Null || Null || Null || Null || Null |3
| Bpn1 || Bpn2 || Bpn3 || Bpn4 || Bpn5 || Bpn6 || Bpn7 || Bpn8 |2
|Brook||Bknight||BBishop||BKing||BQueen||BBishop||Bknight||Brook|1
although the array has got [0] column, where there is only null
but i never refer to 0. my object array starts from 1