Friday, May 20, 2011

Published 5/20/2011 by with 2 comments

attempting to use incompatible return type

We may encounter this error message when we override a superclass method in the subclass with a wrong return type in the subclass method. To understand this, let's see the following two classes. public class SuperClass { public double sum(double a,double b) { return (a+b); } } public class Subclass extends SuperClass { public long sum(double a,double b) { return Math.round(a+b); } } In...
Read More
    email this       edit

Thursday, May 19, 2011

Published 5/19/2011 by with 5 comments

is not abstract and does not override abstract method

See the code below: import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MyFrame extends JFrame implements ActionListener{ private JButton exitButton = new JButton("Exit"); public MyFrame() { Container container = getContentPane(); container.setLayout(new...
Read More
    email this       edit
Published 5/19/2011 by with 2 comments

attempting to assign weaker access privileges

Java error "attempting to assign weaker access privileges" can happen when we override a superclass method in the subclass if the access modifier is not correctly chosen. See the sample code below: public class SuperClass { public void display() { System.out.println("this is super class"); } } public class Subclass extends SuperClass { private void display() { System.out.println("this...
Read More
    email this       edit
Published 5/19/2011 by with 4 comments

modifier private not allowed here

Java error "modifier private not allowed here" can occur for any of the following reasons: 1. An outer class declared as private like below: private class OuterClass {     //... }An outer class only be public or package private not private or protected. So you can write an outer class as below: public class OuterClass {    //... }or class OuterClass {    //... }This...
Read More
    email this       edit

Tuesday, May 17, 2011

Published 5/17/2011 by with 0 comment

No Persistence provider for EntityManager named

Are you using JPA and getting the error "Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named ..." You might have used an entity manager as the following code: public void persist(Object object) {         EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPU");         EntityManager...
Read More

    email this       edit
Published 5/17/2011 by with 1 comment

is not a known entity type

To understand the error, let us assume that we have the following classes: 1) An entity class package person.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.NamedQueries; import...
Read More

    email this       edit

Monday, May 9, 2011

Published 5/09/2011 by with 0 comment

Color parameter outside of expected range: Red Green Blue

Java error "Color parameter outside of expected range: Red Green Blue" can be encountered when we use the java.awt.Color class with wrong arguments for RGB (Red Green Blue). See the wrong code below: import javax.swing.JFrame; import java.awt.Container; import java.awt.Color; public class MyFrame extends JFrame { public MyFrame() { Container container = getContentPane(); container.setBackground(new...
Read More
    email this       edit
Published 5/09/2011 by with 1 comment

modifier static not allowed here

Java error "modifier static not allowed here" can occur for any of the following reasons: 1. Constructor is declared as static(!) A constructor cannot be static. In the wrong code below: public class Person { private String firstName; private String lastName; public static Person() { //... } } Here the constructor Person is declared as static. As the constructor must be non-static, the...
Read More
    email this       edit
Published 5/09/2011 by with 1 comment

illegal combination of modifiers: abstract and static

An abstract method cannot be static because the abstract method must be overridden i.e, body of the abstract method must be defined before invocation. See the wrong code below: public abstract class Employee { public static abstract double earnings(); // error } Here the abstract method earnings() is declared as static. An abstract method must be non-static. So the correct code is: public abstract...
Read More
    email this       edit
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