• Post Reply Bookmark Topic Watch Topic
  • New Topic

int cannot be dereferenced error  RSS feed

 
Collete Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this error has been written numerous times, but I am still confused about why I received the error. I am very new to Java, this is my first and last programming class. This is the first time I have seen this error and lost. I know it has something to do with the int and string. I am confused all together how it works. Code is suppose to accept an integer from user, go thru the array and find the element of that array and display in a textfield. It also has to handle 2 specific exceptions. Can someone please tell me what it is suppose to look like. Thank you..

int cannot be dereferenced-line 53
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Collete, and welcome to the Ranch!

The error you're getting, in line 17 of the code excerpt you posted, is because you're trying to call the toString() method on an int (since that's what's stored in the array), and int is a primitive type. There are several ways you could convert the int to a String: for example, you could concat it with an empty string to have it implicitly converted, or you could use the static Integer.toString() method to explicitly convert it .
 
Collete Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read up on on what it means to "concat it with an empty string" and "static Integer.toString() method" but I'm sorry I am still confused. Originally I had major help with that line but it looks like it did not work. Are you saying this line:

valueField.setText(randomNumber[Integer.parseInt(inputString)].toString());

needs to changed to one of the options? Been trying for 2 weeks to get this working and I am burned out majorly and have gone brain dead. Is it possible for you to show me if not maybe a example.

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer the toString version, but opinions vary.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate the "" + i solution. That creates a new StringBuilder, appends the empty String, appends i and calls toString on the StringBuilder. Appending i increases the StringBuilder capacity if needed, then calls Integer.getChars. Integer.toString creates a new char[] of exactly the right size, then calls Integer.getChars. So both call the same method, but Integer.toString has less overhead.

For those interested, String.valueOf(i) would just call Integer.toString(i, 10) which would call Integer.toString(i), so that's more or less the same.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collete ,

Don't let those guys scare you. For a beginner who is not doing anything terribly complicated where performance is an issue, "" + i works just fine. At this stage in your programming career, don't worry about anything else.

If you keep going and start digging in to writing code you may want to come back and re-visit what these guys are saying, but seriously, for now, don't worry about it.

Sorry guys!!!
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Collete,

Sorry that my earlier reply was kind of cryptic. You've got several different actions being performed in your line of code, and it may make things clearer if you broke it down into separate statements. The original line:

might be reworked as:
That's kind of overkill, but you get the idea. Line 3 in the new code is the step of the process that was generating the error in your original line. Look that over so it's clear to you what's going on, and then try incorporating it into your code using your own naming conventions and style.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . Don't let those guys scare you. . . .
Fat chance! They scare me often enough, so the beginners must be terrified of them.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I hate the "" + i solution. That creates a new StringBuilder, appends the empty String, appends i and calls toString on the StringBuilder. Appending i increases the StringBuilder capacity if needed, then calls Integer.getChars. Integer.toString creates a new char[] of exactly the right size, then calls Integer.getChars. So both call the same method, but Integer.toString has less overhead.

For those interested, String.valueOf(i) would just call Integer.toString(i, 10) which would call Integer.toString(i), so that's more or less the same.



never knew this!!! and i was beginning to think that i know String!!!
 
Collete Williams
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you sooo much it worked great and I was able to chalk one off of the to do list..Now off to the traffic light program
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collete Williams wrote: . . . Now off to the traffic light program
You need to divide all your problems into small pieces. Start with the red light problem the green light problem and the yellow light problem
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd start with green. Red is going to hold you up way too long
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!