Tuesday, November 26, 2013

Published 11/26/2013 by with 0 comment

java.lang.UnsupportedOperationException at java.util.AbstractList.remove

When you create a java.util.List object from an array using Arrays.asList method and after that if you try to clear or remove the elements from the list you'll get this run time error. The reason is that Arrays.asList method returns a fixed-size list backed by the specified array. See in the example code below: int[] myArray = {5,10,15,20}; List intList = Arrays.asList(myArray); ...
Read More
    email this       edit
Published 11/26/2013 by with 5 comments

Resource leak: 'scanner' is never closed

If you do not close the java.util.Scanner object after reading data, you can get an warning saying "Resource leak: 'scanner' is never closed". In the code below see that the Scanner object is closed in a finally block after reading all inputs using the Scanner object. int number1, number2; Scanner scanner = new Scanner(System.in); try { System.out.println("Ener the first number"); ...
Read More
    email this       edit
Published 11/26/2013 by with 0 comment

Dimensions expected after this token / '[' expected

Java error "Dimensions expected after this token" or " '[' expected " can occur while creating array elements or allocating memory using the keyword new. If the square brackets [] are not used properly, you can get this error message. See the code below: int myArray[]=new int{10,20,30,40}; Here this is a syntax error. You have to put square brackets [] after new int. To initialize an array...
Read More
    email this       edit
Published 11/26/2013 by with 0 comment

Cannot invoke length() on the array type

The error message "Cannot invoke length() on the array type" or "cannot find symbol ... method length()" is related with the use of array. Java array has a built in property length which is a variable, not method. If you put parenthesis () after the array property length, you'll get this error. See the example below: int[] number = {5,10,15}; for(int i=0; i<number.length(); i++) {    ...
Read More
    email this       edit

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