王朝网络
分享
 
 
 

Java Methods-Common Syntax Error Messages

王朝厨房·作者佚名  2007-01-05
宽屏版  字体: |||超大  

Java Methods

Common Syntax Error Messages

Exception in thread "main" java.lang.NoClassDefFoundError -- wrong name

Exception in thread "main" java.lang.NoClassDefFoundError -- /java

Exception in thread "main" java.lang.NoClassDefFoundError -- /class

Exception in thread "main" java.lang.NoSuchMethodError: main

class is public, should be declared in a file named

cannot return a value from method whose result type is void

non-static method cannot be referenced from a static context

cannot resolve symbol -- class

cannot resolve symbol -- method

cannot resolve symbol -- variable

'}' expected

'class' or 'interface' expected

illegal character

<identifier> expected

'(' or '[' expected

variable might not have been initialized

unclosed string literal

missing return statement

';' expected

incompatible types

'[' expected

array required, but java.lang.String found

possible loss of precision

'.class' expected

attempting to assign weaker access privileges C:\mywork>java hello Exception in thread "main" java.lang.NoClassDefFoundError: hello (wrong name: Hello)This run-time error (exception) may happen when you mistype a lower case letter for upper case. Normally a class name (e.g., Hello) starts with an upper case letter and the file name should be the same. SDK 1.3 under Windows will compile a file hello.java that defines a class Hello, but when you try to run it as above, it reports an exception. C:\mywork>java Hellois correct. C:\mywork>java Hello.java Exception in thread "main" java.lang.NoClassDefFoundError: Hello/javaor C:\mywork>java Hello.class Exception in thread "main" java.lang.NoClassDefFoundError: Hello/classThe command to run the Java interpreter should use the class name but should not include any extension, neither .java nor .class. An extension in the file name confuses the interpreter about the location of the class (the extension is interpreted as a subfolder).C:\mywork>java Hello Exception in thread "main" java.lang.NoSuchMethodError: mainThis exception may be reported when the main method is missing or its signature is incorrect. The correct signature is public static void main (String[] args)Possible mistakes: private static void main (String[] args)public void main (String[] args)public static int main (String[] args)public static void main (String args)C:\mywork>javac Test.java Test.java:1: class Hello is public, should be declared in a file named Hello.javapublic class Hello ^The source file name is different from the name of the class defined in the file. Here the file name is Test and the class name is Hello. They must be the same.Hello.java:8: cannot return a value from method whose result type is void return 0; ^In Java, main is void, not int, so return is not needed and you can't use return 0 in it.Hello.java:12: non-static method printMsg(java.lang.String) cannot be referenced from a static context printMsg(s); ^The keyword static is missing in the printMsg header: public void printMsg(String msg)should be: public static void printMsg(String msg)Since main is a static method and it calls printMsg with no "something-dot" prefix, printMsg is assumed to be another static method of the same class. Hello.java:7: cannot resolve symbolsymbol : class EasyReaderlocation: class Hello EasyReader console = new EasyReader(); ^Unless installed as a package and properly imported, files for classes used in the program (in this case EasyReader.java or EasyReader.class) should be available in the same folder as Hello.java. A cannot resolve class error may also show up when a library class is not imported or when data type is incorrect or misspelled. For example: private bool match(String word, int row, int col, int rowStep, int colStep)gives WordSearch.java:32: cannot resolve symbolsymbol : class boollocation: class WordSearch private bool match(String word, int row, int col, int rowStep, int colStep) ^It should be boolean. Hello.java:7: cannot resolve symbolsymbol : method PrintMsg (java.lang.String)location: class Hello PrintMsg(s); ^This error may occur when a method is called incorrectly: either its name is misspelled (or upper-lower case is misplaced), or a method is called with wrong types of arguments, or a method is called for a wrong type of object or a wrong class. For example, the same error, will be reported if you write System.println("Hello");instead of System.out.println("Hello");Another example: Hello.java:20: cannot resolve symbolsymbol : method println (java.lang.String,java.lang.String)location: class java.io.PrintStream System.out.println("You entered: ", msg); ^Here a comma is used instead of a + in the println call. This makes it a call with two arguments instead of one and no println method exists that takes two String arguments. Hello.java:18: cannot resolve symbolsymbol : variable lengthlocation: class java.lang.String if (msg.length != 0) ^A very common error, cannot resolve symbol may result from an undeclared variable or a misspelled local variable or field name, or missing parentheses in a method call. Here it should be msg.length().Hello.java:8: '}' expected } ^An extra opening brace or a missing closing brace may produce several errors, including Hello.java:13: illegal start of expression public static void printMsg(String msg) ^Hello.java:16: ';' expected } ^and finally Hello.java:17: '}' expected} ^which may be reported at the end of the file. Hello.java:29: 'class' or 'interface' expected}^This error often results from an extra closing brace (or a missing opening brace).Hello.java:5: illegal character: 20 System.out.println(鬑ello World?; ^"Smart quote" characters accidentally left in the source file by a word processor instead of straight single or double quotes may cause this error. The same error is reported when the source file contains any non-ASCII character in the code (outside comments).Hello.java:3: <identifier> expected static x; ^<identifier> expected is a rather common error message. Here x is a variable, but the compiler thinks it is a class name. It is the data type designation that's actually missing. It should be: static <someType> x;The same happens here: private myRows, myCols;It gives an error: WordSearch.java:4: <identifier> expected private myRows, myCols; ^thinking that myRows is a data type. Same here: public static void printMsg(msg) { ... }- a missing type designator (e.g., String) in a method's header produces four rather obscure errors: Hello.java:19: <identifier> expected public static void printMsg(msg) ^Hello.java:29: ')' expected } ^Hello.java:19: cannot resolve symbolsymbol : class msglocation: class Hello public static void printMsg(msg) ^Hello.java:19: missing method body, or declare abstract public static void printMsg(msg) ^4 errorsIt should be: public static void printMsg(String msg)Hello.java:7: '(' or '[' expected EasyReader inp = new EasyReader; ^Should be: EasyReader inp = new EasyReader();Hello.java:9: variable inp might not have been initialized s = inp.readLine(); ^This error happens if you use a local variable before initializing it. EasyReader inp;declares a variable but you need to initialize it with new. Hello.java:8: ')' expected System.out.print(Enter a message: "); ^Hello.java:8: unclosed string literal System.out.print(Enter a message: "); ^Hello.java:8: cannot resolve symbolsymbol : variable Enterlocation: class Hello System.out.print(Enter a message: "); ^3 errorsA missing opening double quote in a literal string produces these three errors.Hello.java:17: missing return statement { ^A method, other than void, must return a value.Hello.java:23: ';' expected System.out.println("Message: " + msg) ^A few compiler error messages are actually self-explanatory.WordSearch.java:18: incompatible typesfound : intrequired: boolean if (i = n) ^It is supposed to be if (i == n)Single = makes it assignment operator. It returns an int value that can't be tested in if. Similarly, return row = 0 && row < myRows && col >= 0 && col < myCols;gives two errors: WordSearch.java:29: incompatible typesfound : intrequired: boolean return row = 0 && row < myRows && col >= 0 && col < myCols; ^WordSearch.java:29: operator && cannot be applied to int,boolean return row = 0 && row < myRows && col >= 0 && col < myCols; ^2 errorsAn extraneous space between ! and = in an != operator may give several errors, including "incompatible types": Hello.java:10: ')' expected if (s ! = null) ^Hello.java:14: illegal start of expression } ^Hello.java:13: ';' expected } ^Hello.java:10: incompatible typesfound : java.lang.Stringrequired: boolean if (s ! = null) ^4 errorsAnother situation with "incompatible types" is when a literal string is used in place of a char constant or vice-versa. For example: WordSearch.java:21: incompatible typesfound : java.lang.Stringrequired: char grid[r][c] = "*"; ^Should be grid[r][c] = '*';WordSearch.java:10: '[' expected grid = new char(rows, cols); ^An array should be created using brackets, not parentheses.WordSearch.java:20: array required, but java.lang.String found grid[r][c] = Character.toUpperCase(letters[i]); ^Use charAt(i) method, not [i] with strings.Hello.java:7: possible loss of precisionfound : doublerequired: int x = 3.5; ^This happens when a double value is assigned to an int variable.Hello.java:8: '.class' expected double y = double(x); ^Hello.java:8: unexpected typerequired: valuefound : class double y = double(x); ^2 errorsIncorrect cast syntax causes this error. Should be double y = (double)x;Test.java:5: actionPerformed(java.awt.event.ActionEvent) in Testcannot implement actionPerformed(java.awt.event.ActionEvent) injava.awt.event.ActionListener;attempting to assign weaker access privileges; was publicpublic class Test extends JApplet ^1 errorThis error is reported when the keyword public is missing in the actionPerformed method's header.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有