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
1
vote
0 answers

Trying to open new activities from NavigationDrawer, but it says unreachable statement

unreachable statement because of the switch statement on nv = (NavigationView) findViewById(R.id.nav) nv = (NavigationView) findViewById(R.id.nav); nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()…
1
vote
5 answers

Selenium UnreachableBrowserException - Java

System.setProperty("webdriver.chrome.driver","D:/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.navigate().to("https://link"); driver.findElement(By.cssSelector("#username")).sendKeys("id"); …
1
vote
0 answers

Why do I get this unreachable error when I try to get the value from JSpinner?

Can Someone help me? I keep getting this error but don't know what to do. Thanks!!! FrogsJSpinner.addChangeListener( new ChangeListener() { @Override public void stateChanged(ChangeEvent ce) { throw new…
1
vote
4 answers

if (boolean)unreachable statement in Java

this is for a intro programming class i am taking. I have created a Instance method to add a newValue to the totals. It has two parameters in the method: (a letter identifying the amount type, and amount) I was successful on the first…
Luke8h
  • 13
  • 1
  • 6
1
vote
1 answer

Java "add return statement" error

public class1 foo ( class1 t) { if ( object == null ) return t; else foo(t.childObject); } Java keeps telling me that there is no return statement. I can understand what is wrong here, but I cannot fix it without removing the recursion,…
1
vote
2 answers

Compilation issue: Unreachable statement

Good evening all, I am running into a compilation issue with some code for an introductory java class. The application at hand creates a calculator. When trying to compile, I am receiving an error stating that I have an "unreachable statement",…
1
vote
7 answers

Unreachable Statement in Java

I am working in BlueJ for my university course, and I have been set a basic assignment where we need the user to input certain information about a DVD, such as director, name, running time, etc. I am coding in Java, and used Genio too. The…
Megan Sime
  • 1,257
  • 2
  • 16
  • 25
1
vote
4 answers

Java unreachable statement caused by different statement levels

I was trying to get an infinite addition calculation .When I compile this code I get unreachable statement that I believe it been caused by different statement levels . import java.util.Scanner; public class infiAdd { public static void main…
Majed
  • 23
  • 5
1
vote
1 answer

Llvm Remove Terminator Instruction

I want to remove an UnreachableInst since a previous transformation has made it reachable. However, calling eraseFromParent() gives me a malformed BasicBlock since the UnreachableInst is the terminator of its BasicBlock. How do I fix the BasicBlock…
Aerion
  • 63
  • 2
  • 6
1
vote
6 answers

Return in if statement causing unreachable code error

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: …
golddove
  • 1,165
  • 2
  • 14
  • 32
1
vote
6 answers

Searching an array list for a specific record in java

Im writing a method to return a specific record in an array however it throws up two errors and Im not sure how to fix it. Can anyone explain what I'm doing wrong? public String find(String searchName) { // ERROR - MISSING RETURN STATEMENT …
Chris Headleand
  • 6,003
  • 16
  • 51
  • 69
0
votes
1 answer

Question regarding unreachable statement errors

Given this snippet of code, containing a class TestClass with its method loopTest(), which is our main focus: public class TestClass{ public void loopTest(){ int x = 0; loop: for (int i = 1; i < 5; i++){ for (int j = 1; j <…
Mario Mateaș
  • 638
  • 7
  • 25
0
votes
1 answer

Unreachable statement in fragemnt

I am not getting the problem that why it is showing in the fragment: this is the code. The problem is occurring due to return statement we are using in inflator. public class Information extends Fragment { private RecyclerView…
0
votes
0 answers

Unreachable statement and break code connecting to other blocks of code

Can someone please explain to me why this break code connects to another block of code and what I can do to resolve this? I'm kinda new to programming. I've tried rearranging it but it doesn't execute the other code. The error message says…
0
votes
0 answers

Why is this NOT unreachable in the if-statement?

In testOne: Why is the "else if" not unreachable (aNumber is final so the compiler should know it can't get past the "if" part, but it only gives a warning)? Since "else if" apparently is reachable, why then is the "else" part not unreachable…