我正在尝试制定一个计划,它使用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