質問

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