public class Person extends Employee{private String firstName;private String lastName;
public Person(String firstName,String lastName){this.firstName=firstName;this.lastName=lastName;}
}
Person is a class which inherits Employee and see the code below where Employee also inherit the Class Person.
In the above code we will see the error message "cyclic inheritance involving Person public class Person extends Employee" Actually Person should be the super class here and Employee is the subclass. But here the relationsship is such that Employee is the subclass of Person, again Person is the subclass of Employee. It's a cyclic inheritance which is not possible. The solution of this problem is identifying the proper relationship i.e, finding out the super class and the subclass. In our case Person is the super class, so it should not try to inherit Employee and Employee is the subclass which will inherit Person. Correction is ommiting "extends Employee" from the Person class signature as below:
Wednesday, August 19, 2009You see this error message because of any one of the followings:
Read More
1. You misspelled the keyword class (fully lower case) or you did not write it at all for writing your class. public Class Test //actual keyword is class In the above example Class (C is in upper case) is written instead of class (fully lower case). 2. Remember you cannot declare variables outside of class body like below. int variable; //variable must be declared inside the class body To solve this declare variables inside the class body as below. public class Test 3. All methods are also defined within the method body. If you define any method outside of the class body you will see this error message. public class Test In the above code, the method anotherMethod is defined after the end of the class. So this error occurred. Most often it occurs just because of incorrect use of {} (curly braces). Check all { are closed with } in the proper place. 4. If you are defining an interface, check you spelled it correctly in fully lowercase. public Interface Test //actual keyword is interface In the above code, keyword interface is misspelled with upper I. 5. Same way if you define an enum spell the keyword enum correctly. public Enum Day //actual keyword is enum In the above example Enum is incorrect as the E is in upper case, it will be lower case e. Wednesday, May 20, 2009
Very simple error ‘package system does not exist’- most probably you have used ‘err’ or ‘in’ or ‘out’ field of the ‘System’ class of ‘java.lang’ package but you have given lower case "s" instead of upper case "S" in the class name System. Remember Java is case sensitive.
See the below code: system.out.println("package system does not exist error"); // here s is in lower case But in the following code: System.out.println("package system does not exist error solved");//S is in upper case Though System is not a package, as you have written system.out.println – three parts here (system, out and println) a package like statement, Java compiler considers it as package but as this package doesn’t exist, the error message is shown. So the solution is just change the lower case "s" of system to upper case "S". Sunday, May 17, 2009If you try to access a non static member of a class from a static context, you will see ‘non-static method … cannot be referenced from a static context’. For example, you might have tried to call a non static method from a static method. Bear in mind, you can access a static member (variables, methods etc) from both static and non-static context but cannot access non-static member from a static context.
Read More
See the following example code: public class NonStaticMethodCannotBeReferencedFromAStaticContextFor the solution you can: i) declare the non-static member as static and access them from the static context (not always; be careful, this may create other problems) ii) Create an instance of the class where the non-static members are and then reference the non-static members by the created instance or object. Browse http://javatech-stuff.blogspot.com/2007/11/differences-between-java-terms.html for understanding the differences between static and non-static member. Now see the solution below: public class NonStaticMethodCannotBeReferencedFromAStaticContextSolved If a method is defined inside another method we may see ‘illegal start of expression’ error message. Remember that, we cannot define a method inside another method; we just can call a method inside another method. Sometimes it occurs just because of improper opening and closing of curly braces ( { or } ).
Read More
See the following example code: public class IllegalStartOfExpression Now see the solution: public class IllegalStartOfExpression This error message is shown when the statements are not written in proper place. Most often, mistakenly, we may write the processing task (for example assigning value to a variable; writing a loop etc.) outside of any method. In this situation we may see this type of error message.
Read More
Then the solution is to write such statements inside the appropriate methods. See the example code below: public class IdentifierExptectedSee the solution now: public class IdentifierExptectedSolved Monday, December 1, 2008If a class file does not exist or the class is in such a package which is not in the CLASSPATH but you are trying to run that class, then you may see "Exception in thread "main" java.lang.NoClassDefFoundError: [ClassName]".
Read More
Sometimes, by mistake, we(the students) try to execute a Java program without compiling it successfully. If a Java class is not compiled successfully, no class file (a file with extension class) is generated. So we need to compile the Java program at first, then it should be run. The class might be in another directory or package. The class must be in the CLASSPATH for the successful running. In another situation along with the above error message you may see "CMD.EXE was started with the above path as the current directory. UNC paths are not supported. Defaulting to Windows directory" and "CMD does not support UNC paths as current directories". You may see this when you run a program by a batch file or by using TextPad etc. This error means you are running a class which is located in a remote machine and you have given the address of the class like \\RemotePC\directory\ClassName. You should bring the class to your PC (if required) to run it. Or if you want to call a class from the remote PC you may use RMI. Sunday, November 30, 2008We get error message "class [ClassName] is public, should be declared in a file named [ClassName].java" if the file name of the Java source program is not same as the public class name.
Read More
Solution is to rename the file or the class name to make them same. If a public class name is ABC then the file name of that class must be ABC.java. So always copy the class name and paste it when naming the file, then we will not face this error anymore. Tuesday, November 18, 2008'Exception in thread "main" java.lang.NoSuchMethodError: main' is a run time error message. If the main method is not properly defined, we may see this error message. You know if you want to execute/run a program that program must have a main method of the following signature:
Read More
public static void main(String args[]) Generally we do the following mistakes in this regard: 1. May be, you haven't defined the main method at all. Define it at first then run it. 2. The method name is not correctly written. The spelling will be exactly in lower case 'main', if we write 'Main' - it will be an error. For example: public class NoSuchMethodErrorMain 3. The keyword 'static' is omitted. The main method must be static. For example, public class NoSuchMethodErrorMain 4. The parameter is not declared. Java main method must have a String array parameter. Sometimes, we forget to put [] after the data type or the parameter name (args, here). For example: public class NoSuchMethodErrorMain OR public class NoSuchMethodErrorMain The correct code will be: public class NoSuchMethodErrorMainSolution If we declare a variable in a method more than once than we see '
Read More
If we compile the following code we will see 'country is already defined in myMethod()' public class AlreadyDefinedSolution Now the solution is to remove the declaration 'String' from the assignment statement. public class AlreadyDefinedSolution
Subscribe to:
Posts (Atom)
|