• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

calling all objects!!

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to call a variable from another method that has a return statement, but for some reason i keep getting the same bug.
C:\Documents and Settings\Owner\Desktop\JavaPrograms\RobotRat1.java:288: cannot find symbol
symbol : variable spaces_to_move1
location: class RobotRat1
int spaces_to_move = spaces_to_move1.spaces_move();

and heres the code it goes with

public int spaces_move()
{
int spaces_to_move1 = 0;

try
{

spaces_to_move1 = Integer.parseInt(_textfield1.getText());
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid integer value!");
System.out.println("spaces to move is being set to 1.");
spaces_to_move1 = 1;
_textfield1.setText("1");


}
return spaces_to_move1;
}

private void move()
{
int spaces_to_move = spaces_to_move1.spaces_move();

any help would be much appriciated, no rush on this im just experimenting with code to see how I can alter this program to make it more efficient.
thanks in advance
Danny
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
spaces_to_move1 is an int and therfore does not have a method spaces_move()

On another note you might want to keep with standard naming conventions for your variables.

class/interface should be ClassName
method should be methodName()
variable shoud be variableName
static final variable shoud be VARIABLE_NAME

I doesn't effect the running of the program, but it effects the ability of others to read your code and you to read others code. Also when you post code please enclose it in code tags (there is a code button when you are posting). You can also go back and edit a post by clicking on the paper and pencil icon on your post.
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like spaces_to_move1 is an int declared inside of the method spaces_move(), and you're trying to access this variable from inside the move() method. However, the move() method doesn't know anything about it. You could access the variable spaces_to_move1 if it were declared outside of any methods as a member variable of RobotRat1, but you'll need to decide if that's what you want to do. If you need to pass info to other methods, you can pass it as a parameter to the method.

Another problem is that even if it were declared outside of both methods so that they could both see it, you're trying to call spaces_to_move1.spaces_move(), but that doesn't make sense, because spaces_to_move1 is an int, which is a primitive type. Since it's not an Object, it can't have methods or member variables, so it doesn't have the method spaces_move().
 
DANIEL GRESHAM
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


tried the new idea, didnt work, heres the new error message
thanks anyway I'll just dink around with it and see whats going on
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic