문제

The following code:

public static void print(ListNode p)
{
    System.out.print("[");
    if(p==null);
    {
        System.out.println("]");
        return;
    }
    ListNode k = p.getNext();
    //more code
}

causes the following compile error:

 ----jGRASP exec: javac -g Josephus_5_Rajolu.java

Josephus_5_Rajolu.java:53: error: unreachable statement
            ListNode k = p.getNext();
                     ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

Why is that happening? I am only returning if p = null. I want to do my other code if p!=null. Why is it unreachable?

도움이 되었습니까?

해결책

There's a semicolon after your if statement.

다른 팁

there is ; after if statement remove it it will work :)

if(p==null); <-- remove the semicolon at the end

maybe the ';' at the:
if(p==null);

remove the semicolon after your if

Where are you instantiating ListNode K?

It looks like to me that the issue is ListNode k is not being instantiated.

I think you need something like this. ListNode k = new ListNode();

Are you creating this within your class?

Also you have a ; after if(p==null); That needs to be removed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top