java中常见错误
编译器错误
编译器错误信息是在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 " + unt);
}
}
6. “incompatible types”
类型不⼀致
test.java:78: error: incompatible types
String();
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("");
BufferedReader br = new BufferedReader(fr);
try{
while (br.ready()){
String str = br.readLine();
String[] list = str.split(" ");
System.out.println(list);
}
}catch (IOException e){
}
}
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 = Int();
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};