Questions tagged [unreachable-statement]

Use this tag when your compiler is giving you "Unreachable code detected", "Unreachable statement" warning / error message for your code in question and you don't understand the reason.

Unreachable statements are codes that compilers detect as not reachable. There could be variety of situations when this happens.

Having a return statement in the middle of the code. The code that follows are considered as not reachable.

C# example:

string GetName()
{
   string s = "abc";
   return s; 
   s = s + "d"; // Warning CS0162: Unreachable code detected
   return s;
}

Another, would be having a variable defined with certain value and then check the variable for different value to do some operations.

while (false)
{
   Console.WriteLine("unreachable"); // Warning CS0162: Unreachable code detected
}

Java example:

public String getName()
{
   String s = "abc";
   return s; 
   s = s + "d"; // Unreachable statement
   return s;
}

Compilers generally throw Warning message for these type of issues. But it depends on compiler to compiler. For instance .NET compiler may just throw warning but Java may throw it as error.

61 questions
0
votes
5 answers

should a return statement be the last instruction within a code block?

I really need help understanding what does an unreachable statement actually means in Java. I have the following below and when I try to compile I get an unreachable statement error. I have looked at some of the similar questions about unreachable…
0
votes
1 answer

JUnit covering unreachable code

I am given with a java code and i have to write test cases so that 100% coverage can be obtained. But, the code given is written in such a way that one of the "if" statement will never be true. We cannot change the code, still we have to achieve…
Vivek Vardhan
  • 1,118
  • 3
  • 21
  • 44
0
votes
5 answers

Why is this an unreachable statement?

Towards the bottom of this code I am getting an "unreachable statement" error. I have tried a few things, but cannot figure out why this is happening. The error is towards the bottom of the code (I have commented with // where the error is) Please…
Joshua Baker
  • 401
  • 1
  • 7
  • 15
-1
votes
1 answer

Unreachable statement after conditional

The following code gives the error: Unreachable statement for current= current.getmNextNode(); How can it be solved? public int indexOf(E element) { Node current = head; for (int i = 0; i < size; i++) { if…
Natalie_94
  • 79
  • 3
  • 12
-1
votes
3 answers

Java error: unreachable statement when create new linked list node

I wrote a program for merge two linkedlist so I create a dummyHead first. But the compiler returns me an error: unreachable statement. I searched it on Internet but still don't understand why is it. The code is : /** * class ListNode { * public…
Meilan
  • 434
  • 6
  • 17
-1
votes
1 answer

Java Error unreachable statement

Im trying to create a class that has a base object. The base object will be used to create a few objects and be made to 'fight' in another class based on strength and powerups. I have got this error when compiling 'Error, unreachable statement' and…
Manderson
  • 13
  • 4
-1
votes
1 answer

Unreachable statements

I'm studying for the OCA exam and I do not understand why the last line in parseFloat() is unreachable, while the last line in go() is not. Except for the return types, I do not see much of a difference. public float parseFloat(String s) { …
Helenesh
  • 3,999
  • 2
  • 21
  • 36
-2
votes
2 answers

How is this an "unreachable statement"?

I am making a login page, and i would like it if "Successfull login" appears in the console if the username and password match the database. So i simply added a System.out.println(); in the if statement, but it gets the error "unreachable…
-2
votes
1 answer

unreachable statement on Android Studio?

i got this unreachable statement at Andoid studio i try to fix for the data structure but nothing happend, can anyone help me?. The problem is on this.income += ((Income) this.li.get(i)).getJumlah(); this is my code public static int uang; private…
-2
votes
1 answer

Java's weird unreachable code error

I'm writing a program that identifies whether a String "xyz" is certralized in the input String or not. I create a variable that stores the position of "xyz" with a for loop, and then compare it to the number of chars before and after, creating ints…
-2
votes
4 answers

How to avoid "unreachable statement" and "else without if" in Java?

So my code is meant to compare the y co-ordinate of the robot with the y co-ordinate of the target. I'm having problems making the function return anything when I add the print statements in. I have a feeling this has something to do with brackets,…
John Fog
  • 9
  • 2
  • 3
-4
votes
1 answer

An unreachable statement error

I don't know from where the error is coming "unreachable statemententer image description here"
Jerry Joy
  • 1
  • 1
-4
votes
1 answer

Why is this an unreachable statement? Java

The unreachable statement is: thisCustomer = findCustomer(theCustomerID);. I cannot figure out why. thisCustomer is an attribute of Customer object, findCustomer() is a method listed below, and theCustomerID is the parameter of the method this…
mvb
  • 27
  • 5
-5
votes
3 answers

Unreachable Statement in url

When I enter the database url, I got error Unreachable Statement String url= "http://www.example.com/php.php"; This is my code: @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable…
-5
votes
1 answer

Unreachable statement after return using getActivity in a fragment

I'm sorry if someone already asked this, but I keep getting an error with my app. I can't even run because I've got an error saying "Unreachable statement". Here is my code on my Fragment package com.example.dasilvadd.students; public class…