• Post Reply Bookmark Topic Watch Topic
  • New Topic

Int cannot be referenced issue  RSS feed

 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all:

I am currently writing a program for simulating an UNO card game and I am having some difficulty with writing a couple a methods in terms of dealing and shuffling the decks. My code is provided below:

To make it short and sweet... the problem the compiler has is in the randomDeal() method. The compiler says that an int cannot be dereferenced. I know it's something simple I am forgetting but I can't for the life of me figure it out. It has to be something simple that I am missing or forgetting. Does anyone have any ideas?

All your suggestions/help/improvements are greatly appreciated!

Thanks in advance everyone!
S.T.

My code:

 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact compiler message would be helpful. Can you post it please and indicate the line number the error refers to?
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure thing:

javac "Sim_Actions.java" (in directory: /media/sdl5384/TI10690800E/Users/SamuelDavid/Dropbox/Projects and curiosities/Uno)
Sim_Actions.java:20: int cannot be dereferenced
randomCard = this.randNumGen(0, randomCard.size());
^
1 error
Compilation failed.


Thanks in advance!
S.T.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That error is caused by trying to treat an int variable as if it's a reference variable - e.g. by calling a method on it (which you can't, because ints are primitive values).

The error message should tell you exactly which line it occurs on, which as Tim suggests is a big help.

Edit: I posted before I saw your follow up. OK, what type have you declared randomCard as? And does that type have a size() method?
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what do you folks suggest I do instead then?

Because I still need that random number value to deal out a random card from the deck.

Thank you!
S.T.
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually nevermind, I see the problem.

On line 16 you do this:

Then on line 20 you do this:

int is a primitive type and does not have a method size(). In compiler speak "be dereferenced" can be interpreted as "have methods called on it".
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG... i just realized what the problem is everyone.

In the method, I AM using it wrong.

Line 20 should read:


when it should REALLY say:



Welll dont i feel stupid...

Thanks everyone for your help!
S.T.
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent, I'm glad you got it sorted. And kudos for telling us what the problem was and how you fixed it, that is really valuable to the forum.

As a couple of points of style:
- You don't need the 'this' keyword when calling methods from within the same class
- Class names should not contain any non alphanumeric characters, so Sim_Actions should be SimActions. Both are valid Java but the latter is considered better style.
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions. I will keep that in mind.

I am always learning new things with programming, no matter how long I have been doing this
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For putting forward a well formed question, giving feedback on how your came to a solution, and even marking the forum thread as resolved.... I think that deserves a cow!
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
COW..abunga dude!
Thank You! XD
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!