1

I am trying to get an ordered list of vehicles using Native Query in Java.

I am using two methods, the first to get the vehicles, and the second method to order the list.

I get this error:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.set(AbstractList.java:132)
    at java.util.AbstractList$ListItr.set(AbstractList.java:426)
    at java.util.Collections.sort(Collections.java:159)
    at Exercici4.AzizElamraniOrdreMatricula(Exercici4.java:246)
    at Exercici4.main(Exercici4.java:45)

Here is my code:

private static final String BBDDFITXER = "fitxer";

public static void main(String[] args) 
{
    new File(BBDDFITXER).delete();
    ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), BBDDFITXER);

    try {
        Cotxe c3 = new Cotxe("B1432-hk", 10000, true);
        Cotxe c1 = new Cotxe("B54w2-hk", 566666, true);
        Cotxe c2 = new Cotxe("Bwc345-ABC", 13456, true);
        Cotxe c4 = new Cotxe("Ba432th", 10000, true);
        Moto m1 = new Moto("B5756474", 1000, 500);
        Moto m2 = new Moto("6575-YT", 2345454, 250);

        db.store(c3);
        db.store(c1);
        db.store(c2);
        db.store(c4);
        db.store(m1);
        db.store(m2);   

        List<Vehicle> vehicles = AzizElamraniOrdreMatricula(db);
        consultaVehicles(vehicles);

    }finally{
        db.close();
    }
}

public static List<Vehicle> AzizElamraniNQ3(ObjectContainer db) 
{
    List<Vehicle> vehicles = db.query(new Predicate<Vehicle>() 
    {
     public boolean match(Vehicle vehicle) {
        return vehicle.getKm() < 50000
                && vehicle.getMatricula().startsWith("B");
         }
    });
    return vehicles;
}

 public static void consultaVehicles(List<Vehicle> vehicles) {
     System.out.println("total: " + vehicles.size());
     for (Vehicle v : vehicles) {
        System.out.println(v.infoVehicle());
     }
 }

 public static List<Vehicle> AzizElamraniOrdreMatricula(ObjectContainer db) {

     List<Vehicle> vehiclesdes = AzizElamraniNQ3(db);
     Collections.sort(vehiclesdes);

     return vehiclesdes;
 }

What am I doing wrong?

tbodt
  • 16,609
  • 6
  • 58
  • 83
user3325719
  • 75
  • 4
  • 14
  • 6
    Post your stacktrace. – Luiggi Mendoza Feb 27 '14 at 16:22
  • 1
    And try to make an effort to format your code/question.. – Mike Dinescu Feb 27 '14 at 16:23
  • 4
    Welcome to SO. Please read the [FAQ] and [Ask] for tips on writing good questions. You have omitted the most important details, namely the COMPLETE stacktrace and identification of the statement that threw the exception. Until you provide this information nobody can possibly help. – Jim Garrison Feb 27 '14 at 16:26
  • I bet it has something to do with `Collections.sort` and your database, but... more details! – Smutje Feb 27 '14 at 16:51

2 Answers2

1

The list that you get from db.query is not a modifiable list, and Collections.sort modifies the list to make it sorted. To fix this, you can create a new list and sort that instead. Your method AzizElamraniOrdreMatricula would then look like this:

public static List<Vehicle> AzizElamraniOrdreMatricula(ObjectContainer db) {
    List<Vehicle> vehiclesdes = new ArrayList<Vehicle>(AzizElamraniNQ3(db));
    Collections.sort(vehiclesdes);

    return vehiclesdes;
}
tbodt
  • 16,609
  • 6
  • 58
  • 83
1

Yes that's correct db.query is returning fixed list. Please see example below and it will throw java.lang.UnsupportedOperationException:

public class UnsupportedExceptionDemo {

public static void main(String[] args) {

String[] valStrings = { "Java", "Unsupported", "Test" };

List<String> list = Arrays.asList(valStrings);

for (Iterator<String> iterator = list.iterator(); 
        iterator.hasNext();) {
    String string = iterator.next();
    iterator.remove();
}

}

}

Reason: When call Arrays.asList(String… a) method it returns fixed-size list backed by specified array so when you try to modify the list i.e. add or remove value from it will throw UnsupportedOperationException. To fix it use LinkedList

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
user3468976
  • 629
  • 7
  • 4