my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""int cannot be deferenced"" Watch ""int cannot be deferenced"" New topic
Author

"int cannot be deferenced"

Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Nov 24, 2004
Posts: 60
pretty simple class definition..

the accessor method is:

public int getI(){
return i
}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
So the return type is an int. You can't call a method on an int.
Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
so would u suggest writing the method in order to returna string representation of the integer?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You don't have to. Instead of calling toString() you can use the method valueOf in String.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: "int cannot be deferenced"