Monday, May 9, 2011

Published 5/09/2011 by with 1 comment

interface methods cannot have body

Java error "interface methods cannot have body" occurs when body of the method within an interface is defined. See the code below: public interface MyInterface { public void myMetod() { } } Here the method myMethod has body starting at the symbol { and ending at }. An interface can have abstract methods (method without any body). The class that implements the interface defines the body of the...
Read More
    email this       edit
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...
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...
Read More
    email this       edit

Thursday, May 5, 2011

Published 5/05/2011 by with 2 comments

invalid method declaration; return type required

The Java error "invalid method declaration; return type required" can occur for any of the following reasons: 1. You are trying to define a constructor (of course without any return type; not even void)  but the constructor name is not given same as the class name. See the wrong code below: public class Employee {  private int id;  private String name;  public employee(int id,String...
Read More
    email this       edit

Tuesday, May 3, 2011

Published 5/03/2011 by with 3 comments

recursive constructor invocation

If a constructor calls itself, then the error message "recursive constructor invocation" is shown. It may happen when we overload constructors and call the wrong constructor (itself) accidentally. See the wrong code below: public class Employee {   private int id;   private String name;   public Employee(int id,String name)   {     this.id = id;     this.name...
Read More
    email this       edit

Friday, April 15, 2011

Published 4/15/2011 by with 0 comment

java.sql.SQLException: Access denied for user

The full error message may be : java.sql.SQLException: Access denied for user 'root'@'localhost' (using password : YES) or java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO) When we try to connect to a database through JDBC with wrong username or password - we see this error message. See the sample code below: Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName","root","mypassword"); In...
Read More
    email this       edit
Published 4/15/2011 by with 0 comment

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database

Trying to connect to a MySQL server from Java? But seeing the error "Unknown database"? The reason is that the database does not exist in the MySQL server. You might have accidentally misspelled the database name in the JDBC connection url. See the code below: Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sales","username","password"); Here sales is the database...
Read More
    email this       edit
Published 4/15/2011 by with 0 comment

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

This error message may include the following text also: "The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server." When we try to connect to a MySQL server "Communications link failure" error may occur for any of the following errors: i) MySQL server is not running. Check for this and run the sever if it is stopped. ii)...
Read More
    email this       edit
Published 4/15/2011 by with 2 comments

ClassNotFoundException: com.mysql.jdbc.Driver

This error occurs when the required JDBC driver file is not in the CLASSPATH. Also, if the driver name is not correct ClassNotFoundException occurs. To solve this problem, at first check the driver name; if the driver name is correct, do one of the followings as per requirement. i) Add an entry for the driver file (mysql-connector-java-5.1.10.jar, for example) in the CLASSPATH variable. or ii)...
Read More
    email this       edit

Sunday, April 10, 2011

Published 4/10/2011 by with 1 comment

call to super must be first statement in constructor

The error message is very clear actually: i) We can call super in the constructor of a subclass ii) If we call super in the constructor of a subclass, it must be the first statement in that constructor; i.e, before writing any other statement we call to super must be made. It is mentionable that, super(parameters) calls the constructor of the super class. See the code below: The super class... public...
Read More
    email this       edit