File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Cannot be resolved. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot be resolved." Watch "Cannot be resolved." New topic
Author

Cannot be resolved.

Dave Dunn
Greenhorn

Joined: Oct 13, 2005
Posts: 2
Righty i have 4 classes:



Hal:



State



and finally the first of my multple state classes to induce changes to hals vital statistics:



~My problem being i cant succesfully update the numbers from the sleepingState class. I'm a little lost, so if you kind folk know how to resolve the pHal is not resolved error in sleepingState.java, well i'd mighty thankful...

or a better way to go about coding this. Sorry for the long list of code, but i'm new to this schtick.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
It is a bit difficult to know from the long coding what you are trying to achieve, but it seems to me that you are trying to use the pHal object from the sleepingState class (BTW a class name should read SleepingState with two capital Ss).

BUT: You seem never to have declared a pHal object in the SleepingState class. If you set up pHal in SleepingState, it will be a different object from pHal in your other classes. I think you might find things work better if you move that coding to a different class.

BTW: I am not experienced in multi-threading, but you have a call to sleep in your main() method. What does that do?
Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
First, the "pHal is not resolved" error occurs because pHal is created within class Main, and so is not visible within class sleepingState. If you wanted a sleepingState object to be able to change the state of a Hal object, you would have to pass a reference to a Hal object (ie the variable pHal) to the sleepingState object.

More generally, though, why is sleepingState a class? All it does is (attempt to) change the state of a Hal instance. Shouldn't this be done by a method on the Hal class?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might reconsider whether sleepingState should be a class. It looks more like a method to me -- or maybe just a block of code within the switch/case statement of the State class. (Do you really want to create a new object every time?)

Anyway, your immediate problem is that the variable pHal isn't within the scope of sleepingState. There are a variety of ways to address this. For example, if sleepingState's code were actually a method inside the class where pHal is declared, then the variable would be within scope.

On the other hand, if you really do need to keep sleepingState as its own top-level class, then you basically need to pass (a copy of) your pHal reference to the sleepingState instance. For example, pHal could be a constructor argument (although this raises other issues that make me go back to the method vs. class argument).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dave Dunn
Greenhorn

Joined: Oct 13, 2005
Posts: 2
Ahh i see, so i can sack off sleepingState... and maybe call functions set in Hal...

so mebe have

public void decreaseThirst(){
this.setCurrentThirst(+4);
}

Something as simple as that... I'm trying to look into finite state machines, a riff on maybe the AI behind controlling simpleton NPC AI folk or maybe havent quite decided. Anyone done this, know any good coding practices.

Also anyone know much about Fuzzy state machines - to my understanding they are like FSM's but offer outcomes on a balance of varibles.

I dunno, just thinking allowed.

thanks agen
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Hmmm... Without really knowing what your objectives are, I'll offer this for consideration: An induceSleep method might start a new Thread that gradually increases/decreases energy, hunger, etc. depending on how long it runs (probably based on some logarithmic function).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot be resolved.