• Post Reply Bookmark Topic Watch Topic
  • New Topic

UnsupportedOperationException  RSS feed

 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am getting
1
7
4
1
7
4
null
null
null
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(Unknown Source)
at Testing.checking(Testing.java:31)
at Testing.main(Testing.java:14)

So, I don't understand why it is complaining about method. In API there exist this method I thought may be it does not like that my List is done through the array? what can Ido with that if I need to delete the element? and question2 it does not like my casting as well what is wrong with it ?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have a kind of List which does not support removals. If you look in the documentation, you find remove() is “optional”, so classes implementing List are permitted to throw such an Exception instead of removing.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, you can pass any value in an Object[] to System.out.println(); read the documentation and you will find out what happens. you don’t need that test for null pointers.
 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way, you can pass any value in an Object[] to System.out.println(); read the documentation and you will find out what happens. you don’t need that test for null pointers.

yes, I was trying to print all the value but it stopped running by throwing NullPointerException, so I included this "if - else " statement.
Thank you!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You appear to have a kind of List which does not support removals. If you look in the documentation, you find remove() is “optional”, so classes implementing List are permitted to throw such an Exception instead of removing.


And if Anissa reads the docs for the Arrays.asList() call she's using, she'll find it returns just such a List.
 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Campbell Ritchie wrote:You appear to have a kind of List which does not support removals. If you look in the documentation, you find remove() is “optional”, so classes implementing List are permitted to throw such an Exception instead of removing.


And if Anissa reads the docs for the Arrays.asList() call she's using, she'll find it returns just such a List.

Anissa read docs but she did not know about optional methods and she cannot use arrayList because the program fails even quicker=(
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anissa Pary wrote:
Jeff Verdegan wrote:
Campbell Ritchie wrote:You appear to have a kind of List which does not support removals. If you look in the documentation, you find remove() is “optional”, so classes implementing List are permitted to throw such an Exception instead of removing.


And if Anissa reads the docs for the Arrays.asList() call she's using, she'll find it returns just such a List.

Anissa read docs but she did not know about optional methods


That sentence seems self-contradictory.

and she cannot use arrayList because the program fails even quicker=(


If you show your current code and provide details about exactly how and where it fails, someone can help you. Remember, ItDoesntWorkIsUseless(⇐click) and you need to TellTheDetails.(←click)

Just at a guess though, you can create a modifiable ArrayList from an unmodifiable one.



Not saying that's necessarily a proper approach, but it may get you pointed in the right direction.
 
Anissa Pary
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Anissa Pary wrote:
Jeff Verdegan wrote:
Campbell Ritchie wrote:You appear to have a kind of List which does not support removals. If you look in the documentation, you find remove() is “optional”, so classes implementing List are permitted to throw such an Exception instead of removing.


And if Anissa reads the docs for the Arrays.asList() call she's using, she'll find it returns just such a List.

Anissa read docs but she did not know about optional methods


That sentence seems self-contradictory.

and she cannot use arrayList because the program fails even quicker=(


If you show your current code and provide details about exactly how and where it fails, someone can help you. Remember, ItDoesntWorkIsUseless(⇐click) and you need to TellTheDetails.(←click)

Just at a guess though, you can create a modifiable ArrayList from an unmodifiable one.



Not saying that's necessarily a proper approach, but it may get you pointed in the right direction.

So, what is contradictory about my sentence? I am new to java and never met optional methods I have read the API but skipped through (optional) cause I did not meet it and thanks to Campbell Ritchie I started to know that not all methods which are in the API are supported by classes.
and what I mean by failed when I did and I printed the values of the my list all of them were null.
However the code have the same values as my array, which was what I actually needed.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anissa Pary wrote:
and what I mean by failed when I did

and I printed the values of the my list all of them were null.
However the code

have the same values as my array, which was what I actually needed.


Not possible. Both ways produce a list with the same elements as the original. Either your cirularArray was full of nulls in the first case, or you didn't run that code, or you looked at a different list and saw the nulls. The first way does work.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anissa Pary wrote: . . . thanks to Campbell Ritchie I started to know that not all methods which are in the API are supported by classes. . . .
That isn’t what I said. I said it is allowed to throw that Exception rather than removing.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anissa Pary wrote: . . . I was trying to print all the value . . .
No, you weren’t. You though you were trying to print those values, but you were actually calling toString() on them. That is different. If you had passed them directly to System.out, you would not have suffered that Exception.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!