2013-09-30 88 views
-1

以下内容是什么意思?eclipse中无法访问的代码

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unreachable Code  
at mycode.sample.main(sample.java:24) 

我希望能找到发生错误的那一行。我认为“24”是行,但我的项目中只有23行代码。

这里的项目代码

package mycode; 
import java.io.*; 

public class sample { 
    int first; 
    int second; 

    public sample (int fir,int sec) 
    { 
    fir = first; 
    sec = second; 
    } 

    public void add() 
    { 
    System.out.println(first+second);  
    } 

    public static void main(String[] args) throws IOException 
    { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
    int f = Integer.parseInt(reader.readLine()); 
    // int s = Integer.parseInt(reader.r eadLine()); 
    sample sample2 = new sample(f,100); 
    sample2.add(); 
    } 
} 

我想了解此错误消息。 在此先感谢。

+5

护理共享代码。 –

+0

代码无法访问;这意味着你可能在无限循环之后有一个声明。 –

+0

@JoshM。不必要。还可能有其他情况。 –

回答

4

第一条消息Exception in thread "main" java.lang.Error: Unresolved compilation problem:表示您的代码无法编译。你需要识别错误并修复它。现代IDEs如 Eclipse,Netbeans等标志编译错误。他们可以帮助您快速识别来源。

第二误差:

Unreachable Code 
at mycode.sample.main(sample.java:24 

意味着,在第24行的代码将永远不会被达到。

这里是无法访问的代码示例:

public void doSomething() { 
    if (true) { 
     return; 
    } 
    // All code below here is considered unreachable code 
    doSomething() 
} 
+0

但我的项目只有23行。 – wormwood

+0

我已经发布了代码。 – wormwood

4

试着改变你的构造,从:

public sample (int fir,int sec) 
{ 
    fir = first; 
    sec = second; 
} 

到:

public sample (int fir,int sec) 
{ 
    first = fir; 
    second = sec; 
} 
+0

是的,我刚刚做到了。 “无法访问的代码”消失了。但其余的依然存在。 – wormwood

+0

剩下的是什么?有更多的错误?我只是尝试了你的代码,它运行良好,我... – 2013-09-30 16:03:35

+0

没有不错的错误,只有“不可修复的代码消失”的行。像这样:线程“main”中的异常java.lang.Error:未解决的编译问题: \t at mycode.sample.main(sample.java:24) – wormwood