This site requires JavaScript, please enable it in your browser!
Greenfoot back
Alex1s
Alex1s wrote ...

2014/12/6

Run a method from another object

Alex1s Alex1s

2014/12/6

#
Hello everyone, first I want to say that i was reading the HowTo about this, my english is not perfect... I was able to make it work in school but somehow now that I am trying it at home I cant make it work again :/ Greenfoot does not give me any error message I want that the class Mine runs the method lebenVerringern(); from a Object of the class Robita. In Mine i have this code:
Robita robita = new Robita();

void lebenVerringern()
{
    robita.lebenVerringern();
}
And in Robita i simply have this:
int lebenRobita = 20;

public void lebenVerringern()
{
    lebenRobita = lebenRobita-5;;
}
The method lebenVerringern(); works if i run it directly from Robita. I hope its no problem for you that the names are german and that i was posting the code the right way Thats all, I would appreciate any help :)
davmac davmac

2014/12/6

#
I think the problem is that you create a new Robita object, instead of calling the method on an object that already exists. You first need to get a reference to the correct Robita object, and call the method on that.
danpost danpost

2014/12/6

#
The problem is that when running the method from the different classes, you are running the method on two different Robita objects. When you run it in the Robita class, it runs on the object you expect it to and you see the results; but, when you run it from the Mine class, which creates its own Robita object to run the method on, you do not see the results. To see the results, run it a couple of times from the Mine object and then inspect the Mine object and the Robita object it contains. Then inspect the Robita object in your world directly. They are not the same object and do not contain the same value for 'lebenRobita'. The 'robita' field in your Mine class needs to reference the Robita object that is in the world, not a different one. Do you have a reference in one of your classes to the Robita object that is in the world? Do you know how to acquire a reference if you do not already have one? (I believe that is what the tutorial explains how to do). Or, am I mistaken as to your issue altogether?
Alex1s Alex1s

2014/12/6

#
@danpost I understand what you mean. The method is beeing executed in another Robita object, not the one I see on my screen. So I inspected the Mine Object (for the first time, I always inspected the Robita Object) and I get a error message. So now I somehow need to reference to the Robita object that I have already in the world? How do I do this? I am very new to Greenfoot and I dont understand the tutorial so well... Maybe you can give me some example code to understand?
Error message wrote...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at bluej.debugmgr.inspector.ObjectInspector.listElementSelected(ObjectInspector.java:413) at bluej.debugmgr.inspector.Inspector.valueChanged(Inspector.java:408) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415) at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:459) at javax.swing.JTable.setRowSelectionInterval(JTable.java:2166) at bluej.debugmgr.inspector.Inspector.update(Inspector.java:325) at bluej.debugmgr.inspector.ObjectInspector.<init>(ObjectInspector.java:139) at greenfoot.gui.inspector.GreenfootObjectInspector.<init>(GreenfootObjectInspector.java:49) at greenfoot.gui.GreenfootInspectorManager.getInspectorInstance(GreenfootInspectorManager.java:78) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$2.actionPerformed(WorldHandlerDelegateIDE.java:199) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.AbstractButton.doClick(AbstractButton.java:376) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833) at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:157) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877) at java.awt.Component.processMouseEvent(Component.java:6516) at javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at java.awt.Component.processEvent(Component.java:6281) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4872) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4698) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
danpost danpost

2014/12/6

#
The tutorial is pretty well laid out. If you substitute Counter object with Robita object, you should have no problems making your way through it.
Alex1s Alex1s

2014/12/6

#
Now I did it exactly as the tutorial, and it doesn´t work too. Same as before, no error message but the method does not seem ot be executed in the object i want to. The inspect error was caused by the "Robita robita = new Robita(); " in the Mine class by the way. Probably I have a very small stupid mistake in my code, but anyhow, i give up. I dont seem to get anythere. I thank you for taking the time trying to help me :)
danpost danpost

2014/12/6

#
You should have in your world class, lines similar to the following:
// instance field
private Robita robita;
// in constructor
robita = new Robita();
addObject(robita, /* wherever */);
// get method
public Robita getRobita()
{
    return robita;
}
The Mine class would then use a line similar to the following (I will rename your world 'MyWorld'):
((MyWorld)getWorld).getRobita().lebenVerringern();
Alex1s Alex1s

2014/12/6

#
Ok I am stupid. I wrote
addObject(new Robita(),5,5);
instead of
addObject(robita,5,5);
So the "robita" never was added to the world, i just saw this "new Robita()". I have one more question. Why is it
((MyWorld)getWorld).getRobita().lebenVerringern();  
? Why doesnt
getWorld().getRobita().lebenVerringern();  
work? I am asking because I thought that the getWorld method would give the World (in my case RoboterWelt) back that the Actor is in. So why do I have to write the world down by myself? Thank you
danpost danpost

2014/12/6

#
'getWorld' is a method of the World class, created long before you wrote your subclass of it. It can only return a World object (which can be from any subclass of World). It cannot return your world specifically as a 'RoboterWelt' world object; so, you must inform the compiler that it is indeed one so it knows to look in that class for the method.
You need to login to post a reply.