我正在嘗試制定一個計劃,它使用Scanner
類的用戶輸入。該計劃的重點是打印日程安排。在在makeSchedule
功能循環,出現了一個java.lang.NullPointerException
這行:for循環中的java.lang.nullpointer異常
System.out.print(blocks[numberSchedule[i][k] - 1] + "\t");
我測試了這行代碼的功能之外,它完美的工作沒有任何異常。
我不知道我在想什麼。
import java.util.Scanner;
public class Schedule
{
public int[][] scheduleArray;
public static String A, B, C, D, E, F, G, H;
public static String[] blocks;
public static void main(String[] args)
{
Scanner kboard = new Scanner(System.in);
int[][] scheduleArray = new int[][] { {1, 2, 3, 1, 2, 3, 1, 2},
{3, 1, 2, 3, 1, 2, 3, 4}, {4, 8, 6, 5, 7, 4, 8, 6},
{8, 7, 4, 6, 5, 8, 5, 7}, {6, 5, 7, 8, 4, 6, 7, 5}};
System.out.print("What is your A block class?");
String A = kboard.nextLine();
System.out.print("\n");
System.out.print("What is your B block class?");
String B = kboard.nextLine();
System.out.print("\n");
System.out.print("What is your C block class?");
String C = kboard.nextLine();
System.out.print("\n");
System.out.print("What is your D block class?");
String D = kboard.nextLine();
System.out.print("\n");
System.out.print("What is your E block class?");
String E = kboard.nextLine();
System.out.print("\n");
System.out.print("What is your F block class?");
String F = kboard.nextLine();
System.out.print("\n");
System.out.print("What is your G block class?");
String G = kboard.nextLine();
System.out.print("\n");
System.out.print("What is your H block class?");
String H = kboard.nextLine();
System.out.print("\n");
final String[] blocks = {A, B, C, D, E, F, G, H};
makeSchedule(scheduleArray);
}
public static void makeSchedule(int[][] numberSchedule)
{
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 8; k++)
{
if (k != 7)
{
System.out.print(blocks[numberSchedule[i][k] - 1] + "\t");
}
if (k == 7)
{
System.out.println(blocks[numberSchedule[i][k] - 1] + "\n");
}
}
}
}
}
千萬不要這樣'k <8',請使用長度屬性! – Yoda 2014-09-20 22:02:57
請花一些時間來學習如何使用IDE的調試器。 – OldProgrammer 2014-09-20 22:04:39
@OldProgrammer - 在這個例子中,調試器不是必須的。順便說一句,我得到-1,告訴他初始化塊數組,但沒有發生其他事情。 – 2014-09-20 22:07:16