我几乎无法理解代码中的异常。
package botclean;
import java.util.Scanner;
import java.lang.Math;
public class BotClean {
private static int nodeX[], nodeY[];
private static char[][] matrix;
private static int dirt[], botX, botY;
private static int number_of_dirt=0;
private static int number_of_paths=0;
private static Path firstpath;
private static Path paths[];
private static int path_counter = 0;
public static void main(String ab[])
{
String s;
matrix = new char[5][5];
Scanner sc = new Scanner(System.in);
botX = sc.nextInt();
botY = sc.nextInt();
s = sc.nextLine();
for(int i=0; i<5; i++)
{
s = sc.nextLine();
for(int j=0; j<5; j++)
{
matrix[i][j] = s.charAt(j);
}
}
searchDirt();
make_Paths(firstpath);
final_Path();
}
private static void searchDirt()
{
firstpath = new Path();
firstpath.dirtX = new int[25];
firstpath.dirtY = new int[25];
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
if(matrix[i][j]=='d')
{
firstpath.dirtX[number_of_dirt] = i;
firstpath.dirtY[number_of_dirt] = j;
++number_of_dirt;
}
}
}
number_of_paths = 1;
for(int i=2; i<number_of_dirt; i++)
number_of_paths *= i;
System.out.println("number_of_paths = " + number_of_paths);
System.out.println("number_of_dirt = " + number_of_dirt);
paths = new Path[number_of_paths];
for(int i=0; i<number_of_paths; i++)
{
paths[i].dirtX = new int[number_of_dirt];
paths[i].dirtY = new int[number_of_dirt];
}
}
private static void make_Paths(Path pth)
{
int counter=0;
for(int i=1; i<=number_of_dirt; i++)
{
for(int j=1; j<=number_of_dirt; j++)
{
if(i==j)
counter = (i/j)-1;
else if(i<j)
counter = (i/j)+(j-1);
else
counter = (j/i)+j;
paths[i-1].dirtX[counter] = pth.dirtX[j-1];
paths[i-1].dirtY[counter] = pth.dirtY[j-1];
}
}
}
private static void final_Path()
{
int steps1=0,steps2=0;
for(int i=0; i<number_of_dirt; i++)
{
firstpath.dirtX[i] = paths[0].dirtX[i];
firstpath.dirtY[i] = paths[0].dirtY[i];
}
for(int i=1; i<number_of_paths; i++)
{
for(int j=0; j<number_of_dirt; j++)
{
steps1 += Math.abs(botX-firstpath.dirtX[j]);
steps1 += Math.abs(botY-firstpath.dirtY[j]);
steps2 += Math.abs(botX-paths[i].dirtX[j]);
steps2 += Math.abs(botY-paths[i].dirtY[j]);
}
if(steps1>steps2)
{
for(int j=0; j<number_of_dirt; j++)
{
firstpath.dirtX[j] = paths[i].dirtX[j];
firstpath.dirtY[j] = paths[i].dirtY[j];
}
}
steps1=0; steps2=0;
}
}
private static class Path{
int dirtX[];
int dirtY[];
}
}
如果您复制粘贴代码,您将在第93行遇到NullPointerException。 声明 - &gt; paths [i] .dirtX = new int [number_of_dirt];