編譯器錯誤
編譯器錯誤信息是在Java軟件代碼通過編譯器運行時創建的.一定要記住,編譯器可能會為一個錯誤拋出許多錯誤信息.因此,修復第一個錯誤並重新編譯,可以解決很多問題。
1. “… expected”
缺少分號";"或右括號")"
private static double volume(String solidom, double alturam, double areaBasem, double raiom) {
double vol;
if (solidom.equalsIgnoreCase("esfera"){
vol=(4.0/3)*Math.pi*Math.pow(raiom,3);
}
else {
if (solidom.equalsIgnoreCase("cilindro") {
vol=Math.pi*Math.pow(raiom,2)*alturam;
}else {
vol=(1.0/3)*Math.pi*Math.pow(raiom,2)*alturam;
}
}
return vol;
}
2. “unclosed string literal”
字符串不以引號結束
public abstract class NFLPlayersReference {
private static Runningback[] nflplayersreference;
private static Quarterback[] players;
private static WideReceiver[] nflplayers;
public static void main(String args[]){
Runningback r = new Runningback("Thomlinsion");
Quarterback q = new Quarterback("Tom Brady");
WideReceiver w = new WideReceiver("Steve Smith");
NFLPlayersReference[] NFLPlayersReference;
Run();// {
NFLPlayersReference = new NFLPlayersReference [3];
nflplayersreference[0] = r;
players[1] = q;
nflplayers[2] = w;
for ( int i = 0; i < nflplayersreference.length; i++ ) {
System.out.println("My name is " + " nflplayersreference[i].getName());
nflplayersreference[i].run();
nflplayersreference[i].run();
nflplayersreference[i].run();
System.out.println("NFL offensive threats have great running abilities!");
}
}
private static void Run() {
System.out.println("Not yet implemented");
}
}
3. “illegal start of an expression”
產生原因很多,錯誤信息幫助不大
} // ADD IT HERE
public void newShape(String shape) {
switch (shape) {
case "Line":
Shape line = new Line(startX, startY, endX, endY);
shapes.add(line);
break;
case "Oval":
Shape oval = new Oval(startX, startY, endX, endY);
shapes.add(oval);
break;
case "Rectangle":
Shape rectangle = new Rectangle(startX, startY, endX, endY);
shapes.add(rectangle);
break;
default:
System.out.println("ERROR. Check logic.");
}
}
} // REMOVE IT FROM HERE
}
4. “cannot find symbol”
- 標識符在聲明時的拼寫可能與在代碼中使用時不同
- 變量未被聲明
- 變量在聲明的范圍之外使用
- 沒有導入類
5. “public class XXX should be in file”
類名和文件名不匹配
package javaapplication3;
public class Robot {
int xlocation;
int ylocation;
String name;
static int ccount = 0;
public Robot(int xxlocation, int yylocation, String nname) {
xlocation = xxlocation;
ylocation = yylocation;
name = nname;
ccount++;
}
}
public class JavaApplication1 {
public static void main(String[] args) {
robot firstRobot = new Robot(34,51,"yossi");
System.out.println("numebr of robots is now " + Robot.ccount);
}
}
6. “incompatible types”
類型不一致
test.java:78: error: incompatible types
return stringBuilder.toString();
required: int
found: String
1 error
7. “invalid method declaration; return type required”
沒有聲明返回類型
public class Circle
{
private double radius;
public CircleR(double r)
{
radius = r;
}
public diameter()
{
double d = radius * 2;
return d;
}
}
8. “method
in class
cannot be applied to given types”
入參類型錯誤
RandomNumbers.java:9: error: method generateNumbers in class RandomNumbers cannot be applied to given types;
generateNumbers();
required: int[]
found:generateNumbers();
reason: actual and formal argument lists differ in length
9. “missing return statement”
沒有寫返回語句
public String[] OpenFile() throws IOException {
Map<String, Double> map = new HashMap();
FileReader fr = new FileReader("money.txt");
BufferedReader br = new BufferedReader(fr);
try{
while (br.ready()){
String str = br.readLine();
String[] list = str.split(" ");
System.out.println(list);
}
}catch (IOException e){
System.err.println("Error - IOException!");
}
}
10. “possible loss of precision”
精度丟失,如
- 將實數賦值給 int 類型的變量
- 將 double 類型數據賦值給 int 類型的變量
11. “reached end of file while parsing”
缺少"}"
public class mod_MyMod extends BaseMod
public String Version()
{
return "1.2_02";
}
public void AddRecipes(CraftingManager recipes)
{
recipes.addRecipe(new ItemStack(Item.diamond), new Object[] {
"#", Character.valueOf('#'), Block.dirt
});
}
12. “unreachable statement”
存在語句執行不到
for(;;){
break;
... // unreachable statement
}
int i=1;
if(i==1)
...
else
... // dead code
13. “variable
might not have been initialized”
變量未初始化
int x;
if (condition) {
x = 5;
}
System.out.println(x); // x may not have been initialized
14. “Operator .. cannot be applied to
”
使用了未定義的類型
operator < cannot be applied to java.lang.Object,java.lang.Object
15. “inconvertible types”
類型無法強轉,如: 布爾值無法轉為整型
TypeInvocationConversionTest.java:12: inconvertible types found : java.util.ArrayList<java.lang.Class<? extends TypeInvocationConversionTest.Interface1>>
required: java.util.ArrayList<java.lang.Class<?>>
lessRestrictiveClassList = (ArrayList<Class<?>>) classList;
16. “missing return value”
沒有出參或出參類型不正確
17. “cannot return a value from method whose result type is void”
返回值類型為 void,但是有返回值
public static void move()
{
System.out.println("What do you want to do?");
Scanner scan = new Scanner(System.in);
int userMove = scan.nextInt();
return userMove;
}
18. “non-static variable . . . cannot be referenced from a static context”
靜態方法引用非靜態變量
public class StaticTest {
private int count=0;
public static void main(String args[]) throws IOException {
count++; //compiler error: non-static variable count cannot be referenced from a static context
}
}
19. “non-static method . . . cannot be referenced from a static context”
class Sample
{
private int age;
public void setAge(int a)
{
age=a;
}
public int getAge()
{
return age;
}
public static void main(String args[])
{
System.out.println(“Age is:”+ getAge());
}
}
20. “(array)
not initialized”
聲明了數組,但沒有初始化
AClass[] array = {object1, object2}
As is:
AClass[] array = new AClass[2];
…
array[0] = object1;
array[1] = object2;
But not:
AClass[] array;
…
array = {object1, object2};