Я пытаюсь проверить, является ли свойство нулевым, но я получаю исключение нулевого указателя. Каков правильный способ сделать это?
private void recursePrintList(myNode head)
{
if(head.next == null) //NullPointerException on this line
{
System.out.println(head.value);
}
else
{
System.out.println(head.value);
recursePrintList(head.next);
}
}
Вы должны проверить, является ли head
нулевой в начале вашего метода. Также вы можете упростить рекурсивную печать:
private void recursePrintList(myNode head) {
if (head != null) {
System.out.println(head.value);
recursePrintList(head.next);
}
}
Вы должны проверить значение головы, как если бы нуль не был равен голове, а затем выполнял операции.
ivate void recursePrintList(myNode head) { if ( null != head) { System.out.println(head.value); recursePrintList(head.next); } }
head
равен null, тоhead.next
сгенерируетhead.next
NullPointerException