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