• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable cannot be dereferenced  RSS feed

 
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have a program computes for the total debate scores then averages them, and finally sort them by category that will be chosen later. It will prompt the user for (1) firstname, (2) lastname, (3) score for content, (4) score for delivery, (5) score for organization, and (6) rank. Unfortunately, I can only properly sort by firstname and lastname, thus, I cannot properly sort by content, delivery, organization, or rank. Everytime I include the compareTo() method in the content, delivery, organization, and rank blocks, it ends up in a compile error "int cannot be dereferenced." Please help me this. Oh boy, the other Java forum website just banned me from asking questions. I ask questions because I just don't know what to do but I do research but I can find the answer; so why ban me from asking questions? Anyway, here's my code:

 
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Such a large piece of code without any code formatting is hard to read. Please use the Code tags to format the code!
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:Such a large piece of code without any code formatting is hard to read. Please use the Code tags to format the code!

@Ryan: I've done it for you this time. Please read the UseCodeTags (←click) page carefully, because you now have another problem: namely, that your lines are too long. I suggest you try and break them up a bit, and don't use TABs for indenting.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Daguman wrote:Unfortunately, I can only properly sort by firstname and lastname, thus, I cannot properly sort by content, delivery, organization, or rank. ... Please help me this.

You can only use compareTo() to order in ONE way. If you need multiple orderings, use Comparators (java.util.Comparator).

A general point: Handing people a pile of code and expecting them to decipher your problem is not the best way to get quick (or good) answers.

I suggest you read the SSCCE page for details on how you can help us to help you. This page is also a great resource for getting the best out of forums like ours, as is our very own HowToAskQuestionsOnJavaRanch (←click).

Winston
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your mian method is way too long. Think about splitting it into smaller pieces.
Also, read this: MainIsAPain
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Daguman wrote:
I have a program computes for the total debate scores then averages them, and finally sort them by category that will be chosen later. It will prompt the user for (1) firstname, (2) lastname, (3) score for content, (4) score for delivery, (5) score for organization, and (6) rank. Unfortunately, I can only properly sort by firstname and lastname, thus, I cannot properly sort by content, delivery, organization, or rank. Everytime I include the compareTo() method in the content, delivery, organization, and rank blocks, it ends up in a compile error "int cannot be dereferenced." Please help me this. Oh boy, the other Java forum website just banned me from asking questions. I ask questions because I just don't know what to do but I do research but I can find the answer; so why ban me from asking questions? Anyway, here's my code:


The "int cannot be dereference" error is straightforward. You are trying to use an int variable like an object, which can't be done. The int variable holds a 32 bit signed value, and that is it. It doesn't have any methods. It doesn't have any instance variables... You can assign it; you can do math with it (use it in an expression); you can pass it around; but you can't dereference it, as it is not a reference variable.

Henry
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to use compareTo method on an int?
You can't do that.
If you have to use compareTo an option is to use Integer instead of int.
 
Ranch Hand
Posts: 221
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems everyone pointed you to the proper documents, so I can skip that. All are extremely helpful.

Now, regarding your compiler error. As Winston stated, 200 lines of code with no supporting documentation is quite a bit. Remember, most of the posters here are out working somewhere. They hang out here on their free time, and asking for people to debug large programs is a lot to ask. After reading the verbage on SSCCE, please provide us with a posting of the error message. The error message will provide a line number and other information that will make it easier (and therefore faster) to get a quality response. Also, since this seems to be a school assignment, you might to read the We are not a code mill page(s). Some very good advice in there.

Regards,
Robert

Just a thought (scarey, I know) -- No one here will chase you away for asking questions. Heck they let me stay here and even let me post questions from time to time.
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theres are the problematic lines in my code:



I always end up in compile error "int cannot be dereferenced." However, if I remove the compareTo() part of the code, it will compile but it will not do its function properly. Refer to the code below:

[Edit - added code tags and indentation - see UseCodeTags for details]
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this shorter code makes it more readable and understandable.
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a couple of people have mentioned, debateGroup[j-1].getContent() returns an int. ints don't have a compareTo() method. They don't have any methods, because they are primitive data types.

You can either use the Integer.compare() method to compare two ints, or you can simply write
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, "...cannot be dereferenced" is the general error you get when incorrectly trying to use any primitive type as a reference to an object - such as trying to call a method on it.
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir Matthew Brown I thank you so much!!! It worked! Wow! I hope I can be as good as you and many other expert Java programmers out there. Oh boy, my mind is kinda rusting again. I guess I'll read my book "Java Demystified" again or read the various Java tutorials on the net nowadays.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Are you trying to use compareTo method on an int?
You can't do that.
If you have to use compareTo an option is to use Integer instead of int.



Or you can use the Java operators designed to do comparison of int types .... ie. The ==, !=, >, >=, <, and <= operators.

[EDIT: which this conversation already led to, but I walk away from my computer before submitting, so missed it]

Henry
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Pawel Pawlowicz wrote:Are you trying to use compareTo method on an int?
You can't do that.
If you have to use compareTo an option is to use Integer instead of int.



Or you can use the Java operators designed to do comparison of int types .... ie. The ==, !=, >, >=, <, and <= operators.

[EDIT: which this conversation already led to, but I walk away from my computer before submitting, so missed it]

Henry

That's why I wrote: If you have to use compareTo.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!