This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"int cannot be deferenced"

 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
integer i is an instance variable of a class c

want to use textfield.setText(c.getI().toString()) to set the textfield to the value i (string representation of).

getting the error:

"int cannot be dereferenced"

any ideas?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing here because I don't know what is in your class definition, but it looks like getI() is a method in the class definition that returns the value of the instance variable i. So it's type is int. You can't call a method on an int. You want to use the class method valueOf in String to convert the int to a String.
 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pretty simple class definition..

the accessor method is:

public int getI(){
return i
}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the return type is an int. You can't call a method on an int.
 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so would u suggest writing the method in order to returna string representation of the integer?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to. Instead of calling toString() you can use the method valueOf in String.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deyna Cegielski:
integer i is an instance variable of a class c

want to use textfield.setText(c.getI().toString()) to set the textfield to the value i (string representation of).

getting the error:

"int cannot be dereferenced"

any ideas?




Hello Deyna , as Keith said use method valueOf in String or you can try this
class JR
{
public int getI()
{
return 8 ;
}

}

class test
{
public static void main(String arg[])
{
JR r = new JR();

System.out.print((r.getI()+""));
}
}


Hope this helps

cheers
[ April 18, 2006: Message edited by: faisal usmani ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic