Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

Monday, May 9, 2011

Published 5/09/2011 by with 0 comment

interface expected here

The code below leads to an error "interface expected here"

public interface MyInterface extends Thread
{
}

Here an interface is created (MyInterface) and it inherits(extends) the Thread class which is not possible at all. Why? Because an interface can inherit only another interface, not any class. As Thread is a class, it cannot be inherited by any interface.

The code below is correct as the interface inherits another interface Runnable



public interface MyInterface extends Runnable
{
}


A similar type of error is posted at  no interface expected here

Read More
    email this       edit
Published 5/09/2011 by with 0 comment

no interface expected here

Any Java code as below will lead an error "no interface expected here"

import java.io.Serializable;
public class Person extends Serializable
{
private String firstName;
private String lastName;
//...
}

See that, here Person is a class (public class Person) and it inherits an interface (extends Serializable). Note that a class can inherit another class only, not any interface. A class can implement one or more interface only. Serializable is an interface, not a class.

Actually the correct code of the above will be:


import java.io.Serializable;
public class Person implements Serializable
{
private String firstName;
private String lastName;
//...
}

Here it is notable that, only an interface can inherit another interface.

A similar type of error is posted at interface expected here
Read More
    email this       edit

Friday, August 28, 2009

Published 8/28/2009 by with 0 comment

cyclic inheritance involving ...

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.


public class Employee extends Person
{
private double salary;

public Employee(String firstName,String lastName,double salary)
{
super(firstName,lastName);
this.salary=salary;
}
}

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:

public class Person
{
private String firstName;
private String lastName;

public Person(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}

}

public class Employee extends Person
{
private double salary;

public Employee(String firstName,String lastName,double salary)
{
super(firstName,lastName);
this.salary=salary;
}

}
Read More
    email this       edit

Wednesday, August 19, 2009

Published 8/19/2009 by with 5 comments

class, interface, or enum expected

You see this error message because of any one of the followings:

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
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

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
public class Test
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

To solve this declare variables inside the class body as below.


public class Test
{
int variable; //variable must be declared inside the class body
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}


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
{
public void myMethod()
{
System.out.println("method inside the class body");
}
} //end of class

public void anotherMethod()
{
System.out.println("method outside of the class body");
}

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
{
public void myMethod();

}

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
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

In the above example Enum is incorrect as the E is in upper case, it will be lower case e.
Read More
    email this       edit

Wednesday, May 20, 2009

Published 5/20/2009 by with 40 comments

package system does not exist

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".
Read More
    email this       edit

Sunday, May 17, 2009

Published 5/17/2009 by with 2 comments

identifier expected

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.

Then the solution is to write such statements inside the appropriate methods.

See the example code below:
public class IdentifierExptected
{
int number1,number1,sum;


number1=10; // identifier expected
number2=20; // identifier expected
sum=number1+number2; // identifier expected

/*
The assignment statement must be written inside a method. Variables can also be assigned/ initialized during the declaration.
*/


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}


}

See the solution now:

public class IdentifierExptectedSolved
{
int number1=10,number2=20,sum;


public void sum()
{
sum=number1+number2;
}


public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}



}
Read More
    email this       edit

Monday, December 1, 2008

Published 12/01/2008 by with 1 comment

Exception in thread "main" java.lang.NoClassDefFoundError: [ClassName]

If 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]".

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.
Read More
    email this       edit

Sunday, November 30, 2008

Published 11/30/2008 by with 3 comments

class [ClassName] is public, should be declared in a file named [ClassName].java

We 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.

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.
Read More
    email this       edit

Tuesday, November 18, 2008

Published 11/18/2008 by with 1 comment

reached end of file while parsing

The error 'reached end of file while parsing' occurs because of curly braces } problem. You have given either more or less curly braces } than the required number of } braces.

See the code below:
public class ReachedEndOfFileWhileParsing
{
public static void main(String args[])
{
System.out.println("java-error-massages.blogspot.com");

}

Here two curly braces are started but only one is ended. We need to put another } after the display statement here like below:

public class ReachedEndOfFileWhileParsingSolution
{
public static void main(String args[])
{
System.out.println("java-error-massages.blogspot.com");
}// it is now given

}
Read More
    email this       edit

Tuesday, November 4, 2008

Published 11/04/2008 by with 0 comment

missing method body, or declare abstract

If we put a ; (semicolon) after the method header (i.e., before starting the method body) when we are defining a concrete method (which method has body) then we will see “missing method body, or declare abstract”. Another reason is if we don’t write the keyword ‘abstract’ for an abstract method (a method without body), we’ll also see this message.
To resolve the problem we must remove the ; (semicolon) from the header of the concrete method or if it is an abstract method the keyword ‘abstract’ must be written in the method header.

Here is an example of this error:
public abstract class MissingMethodBodyOrDeclareAbstract
{
public void display(String message);
{
System.out.println(message);
}
public void display();
}

Here, a ; (semicolon) is given after the method header of the first display method (with parameter). And for the second display method, keyword ‘abstract’ is not stated in the header and it doesn’t have any body also. Here it is to be noted that if a class contains an abstract method, the class must also be abstract.

The correct code is:

public abstract class MissingMethodBodyOrDeclareAbstractSolution
{
public void display(String message)
{
System.out.println(message);
}
public abstract void display();
}
Read More
    email this       edit
Published 11/04/2008 by with 0 comment

cannot find symbol / cannot resolve symbol

“cannot find symbol” or “cannot resolve symbol” is the most common error message which the programmers get from the compiler. However this error message may mean that the variable/method/class/object used are not declared/defined properly. In most cases, this happens because:

· The variable/object is not declared or the spelling of variable/object declaration & later use of that variable/object is not same.

· The method is not defined or the spelling of the method definition and method calling is not same or the number and/or type of parameters are wrongly given when calling the method or the method where it is defined is not imported properly (in case of class library methods, the package where the method is defined is not imported)

· If the message is shown for a class, the package of that class might not have been imported or the spelling is wrong.

The solution of that problem is to check the spelling at first, Java programmers see this message most often just because of the wrong spelling( be careful about the case also, as Java is a case sensitive language). If the spelling is correct check whether the required package is imported.

Here is an example of this error :

public class CannotFindSymbol
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print(“Enter the quantity: ”);
quntity=imput.nextInteger();
System.out.println(“Your number is ”+quantity);
}
}


In the above code, there are many lines where we will get this error message.

At first it will say Scanner cannot be resolved/found because the package of this class java.util is not imported.

Also problem in ‘quntity’ as the variable is declared as ‘quantity’

Error in ‘imput’ as the object is created as ‘input’

If you correct all the errors it will show error message for nextIntger as the actual method name is nextInt in class Scanner

Now the correct code is:

import java.util.*;
public class CannotFindSymbolSolution
{
public static void main(String args[])
{
int quantity;
Scanner input=new Scanner(System.in);
System.out.print("Enter the quantity: ");
quantity=input.nextInt();
System.out.println("Your number is "+quantity);
}
}
Read More
    email this       edit