Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Designing a Card Game

Reply
Thread Tools

Designing a Card Game

 
 
Knute Johnson
Guest
Posts: n/a
 
      06-18-2008
Patricia Shanahan wrote:
> pek wrote:
> ...
>> At that time, I tested his suggestion and replied:
>> Interestingly, you cannot pass null when a method requires an
>> enumeration!
>>
>> In which he replied:
>> Sure you can!
>> [snip]
>> method((X)null);
>> [snip]
>>
>> I swear that when I first tested new Card(Rank.JOKER, null) eclipse
>> displayed a compile-time error. I can't remember what was the error,
>> but now I can't reproduce it! Now new Card(Rank.JOKER, null) doesn't
>> yell anything! Assuming Knute also had seen this error (since he
>> provided a solution instead of calling me names), what's happening?

> ...
>> So what's the big deal? Is it, or is it not a compile-time error when
>> passing null in a method that expects an Enum?

>
> It is not an error to pass null to a method that expects an enum.
>
> The key is the following sentence "The direct supertypes of the null
> type are all reference types other than the null type itself." in the
> JLS, 4.10.2 Subtyping among Class and Interface Types at
> http://java.sun.com/docs/books/jls/t...es.html#4.10.2
>
>
> According to 8.1 Class Declaration at
> http://java.sun.com/docs/books/jls/t...asses.html#8.1,
> an enum declaration specifies a new named reference type. Thus each enum
> type is a supertype of the null type.
>
> As a result, conversion from the null type to any enum type is a
> widening reference conversion, which is a permitted form of method
> invocation conversion. 8.9 Enums,
> http://java.sun.com/docs/books/jls/t...asses.html#8.9,
> contains various restrictions on enums, but none that prevents method
> invocation conversion of null to an enum type. Indeed, a coding example
> in that section contains:
>
> private Card(Rank rank, Suit suit) {
> if (rank == null || suit == null)
> throw new NullPointerException(rank + ", " + suit);
> this.rank = rank;
> this.suit = suit;
> }
>
> which would make no sense at all if null were not method invocation
> conversion to Rank and Suit, which are both enum types.
>
> There could have been some other problem in your original test. If you
> had prepared and posted an example at the time it could have been
> analyzed.
>
> Patricia


When I originally responded to pek's post I suggested casting the null
to the appropriate type because I had run into this somewhere before.
It think it occurred when I passed null to a JDialog constructor and got
a compiler warning. So when he asked his question that just came to
mind. I wonder if there was a compiler bug at some point or if I just
have an advanced case of CRS.

You know what I'll bet I know what my original problem was, the JDialog
constructor takes either a Frame, a Dialog or a Window as owner. The
compiler wouldn't know which without the cast. But maybe that is a bug,
I don't know.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
 
pek
Guest
Posts: n/a
 
      06-19-2008
On Jun 19, 1:23*am, Patricia Shanahan <(E-Mail Removed)> wrote:
> Knute Johnson wrote:
> > Patricia Shanahan wrote:
> >> pek wrote:
> >> ...
> >>> At that time, I tested his suggestion and replied:
> >>> Interestingly, you cannot pass null when a method requires an
> >>> enumeration!

>
> >>> In which he replied:
> >>> Sure you can!
> >>> [snip]
> >>> method((X)null);
> >>> [snip]

>
> >>> I swear that when I first tested new Card(Rank.JOKER, null) eclipse
> >>> displayed a compile-time error. I can't remember what was the error,
> >>> but now I can't reproduce it! Now new Card(Rank.JOKER, null) doesn't
> >>> yell anything! Assuming Knute also had seen this error (since he
> >>> provided a solution instead of calling me names), what's happening?
> >> ...
> >>> So what's the big deal? Is it, or is it not a compile-time error when
> >>> passing null in a method that expects an Enum?

>
> >> It is not an error to pass null to a method that expects an enum.

>
> >> The key is the following sentence "The direct supertypes of the null
> >> type are all reference types other than the null type itself." in the
> >> JLS, 4.10.2 Subtyping among Class and Interface Types at
> >>http://java.sun.com/docs/books/jls/t...ypesValues.htm....

>
> >> According to 8.1 Class Declaration at
> >>http://java.sun.com/docs/books/jls/t...asses.html#8.1,
> >> an enum declaration specifies a new named reference type. Thus each enum
> >> type is a supertype of the null type.

>
> >> As a result, conversion from the null type to any enum type is a
> >> widening reference conversion, which is a permitted form of method
> >> invocation conversion. 8.9 Enums,
> >>http://java.sun.com/docs/books/jls/t...asses.html#8.9,
> >> contains various restrictions on enums, but none that prevents method
> >> invocation conversion of null to an enum type. Indeed, a coding example
> >> in that section contains:

>
> >> private Card(Rank rank, Suit suit) {
> >> * if (rank == null || suit == null)
> >> * * throw new NullPointerException(rank + ", " + suit);
> >> * * * this.rank = rank;
> >> * * * this.suit = suit;
> >> }

>
> >> which would make no sense at all if null were not method invocation
> >> conversion to Rank and Suit, which are both enum types.

>
> >> There could have been some other problem in your original test. If you
> >> had prepared and posted an example at the time it could have been
> >> analyzed.

>
> >> Patricia

>
> > When I originally responded to pek's post I suggested casting the null
> > to the appropriate type because I had run into this somewhere before. It
> > think it occurred when I passed null to a JDialog constructor and got a
> > compiler warning. *So when he asked his question that just came to
> > mind. *I wonder if there was a compiler bug at some point or if I just
> > have an advanced case of CRS.

>
> > You know what I'll bet I know what my original problem was, the JDialog
> > constructor takes either a Frame, a Dialog or a Window as owner. *The
> > compiler wouldn't know which without the cast. *But maybe that is a bug,
> > I don't know.

>
> JDialog has separate constructors for Frame and Dialog as owner. Frame
> and Dialog are both immediate supertypes of the null type, so neither
> constructor would be the most specific. The JLS requires that to be
> treated as a compile-time error. Adding a cast to one of the types would
> resolve the ambiguity.
>
> It is possible that pek had a similar situation, with overloaded methods
> instead of constructors, so that adding a cast would work.
>
> Patricia


Yes, that crossed my mind when I first thought about it. But Card had
a single constructor (and still has).. Anyway, since passing null when
waiting for an enum cannot be a compile-time error, then probably it
was something else.

Thank you for your comments.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Game programming for kids: looking for open source 2D game developmentkit Max Kubierschky Python 10 03-31-2007 07:18 PM
I need help with card game game of war in GUI enviroment judith Java 0 11-01-2006 06:11 AM
Java Game- Game can't find it's graphics jar file BlackHawke Java 12 01-26-2004 06:14 AM



Advertisments