• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding null to list with ? extends type

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a little code of my own I was experimenting with, based on the WhizLabs simulator:

import java.util.*;
class Test {
public static void main( String[] args ) {
Object[] array = {1,2,3,4,5};
List<? extends Object> list = new ArrayList(Arrays.asList()); // line A
// List<? extends Object> list = Arrays.asList(); // line B
list.add(null);
System.out.println(list);
}
}

Question: Why does line A work, but if you comment out line A and put in line B, a runtime exception unsupported operation occurs??
[ January 26, 2007: Message edited by: jan ter avest ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is nothing to do with generics and nothing to do with null

If you look at the documentation of the API for java.util.Arrays.asList you will see:

This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

List stooges = Arrays.asList("Larry", "Moe", "Curly");



Now can you guess why you get a unsupported operation exception?
 
jan ter avest
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic