Java Swing actionPerformed -



Java Swing actionPerformed -

hi there i'm working in project had create cui game , convert gui, i'm in process of adding functionality buttons interact game having trouble.

this method i'm attempting access. it's in class called pet.

public double feedpet() { if(this.hunger < max_hunger) { this.hunger += food; if(this.hunger > max_hunger) { this.hunger = max_hunger; } system.out.println(this.petname + " enjoyed meal!"); system.out.println("hunger increased " + nutrient + " total of " + this.hunger); spacing(); } else { system.out.println(this.petname + " full!"); spacing(); } homecoming this.hunger; }

this class trying access method.

public gamepanel(petworld petworld, pet pet, game game) { initcomponents(); gencomponents(pet); } private void gencomponents(final pet pet) { this.setsize(600, 600); graphicspanel.setsize(600, 400); graphicspanel.setbackground(color.red); buttonpanel.setsize(600, 200); buttonpanel.setborder(borderfactory.createtitledborder("pet options")); jbutton feedbtn = new jbutton("feed"); jbutton playbtn = new jbutton("play"); jbutton sickbtn = new jbutton("medicine"); jbutton trainbtn = new jbutton("train"); jbutton sleepbtn = new jbutton("sleep"); //// error occurring here believe \\\\ feedbtn.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { pet.feedpet(); } }); buttonpanel.setlayout(new gridbaglayout()); gridbagconstraints gc = new gridbagconstraints(); //// pet options \\\\ gc.weightx = 0.5; gc.weighty = 0.5; gc.gridx = 0; gc.gridy = 1; buttonpanel.add(feedbtn, gc); gc.gridx = 1; gc.gridy = 1; buttonpanel.add(playbtn, gc); gc.gridx = 2; gc.gridy = 1; buttonpanel.add(sickbtn, gc); gc.gridx = 3; gc.gridy = 1; buttonpanel.add(trainbtn, gc); gc.gridx = 4; gc.gridy = 1; buttonpanel.add(sleepbtn, gc);

}

i've had create pet final, believe bad alter values in pet itself, eg happiness, hunger etc

the error getting upon pressing button

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ pdcmaingui.gamepanel$1.actionperformed(gamepanel.java:63) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2018) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2341) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.component.processmouseevent(component.java:6505) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3320) @ java.awt.component.processevent(component.java:6270) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4861) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4492) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422) @ java.awt.container.dispatcheventimpl(container.java:2273) @ java.awt.window.dispatcheventimpl(window.java:2719) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:735) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:694) @ java.awt.eventqueue$3.run(eventqueue.java:692) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87) @ java.awt.eventqueue$4.run(eventqueue.java:708) @ java.awt.eventqueue$4.run(eventqueue.java:706) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:705) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)

is there improve way implement i'm trying do? i'm guessing caused method trying alter variable values beingness set final in gui class? suggestions appreciated.

the problem declaration of action listener. while creating actionlistener() within gencomponents(final pet pet) procedure, not executes @ runtime.

but first, lets clear calling of variable final. means pet cannot reassigned different pet, doesn't mean can't access methods of pet may change, such ishungry(), setfed(boolean) etc.

second, getting pet scope. when define gencomponents(final pet pet) procedure, pet in scope java compiler has no problem @ compile time. @ runtime gencomponents(final pet pet) procedure gets executed, pet known, , during execution creates actionlistener, points variable location parameter. @ completion of gencomponents(...) procedure parameter goes out of scope. when actionlistener triggered pressing button, tries refer memory location of out-of-scope variable.

private void gencomponents(final pet pet) { .... feedbtn.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { pet.feedpet(); <-- pet refers parameter, out of scope when gets executed. } }); .... }

solution space: solve problem pet needs accessed somewhere in scope, , can storing pet private class variable.

public class gamepanel { private final pet pet; public gamepanel(petworld petworld, pet pet, game game) { this.pet = pet; initcomponents(); gencomponents(); <-- remove pet parameter } private void gencomponents() { .... feedbtn.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { pet.feedpet(); <-- pet refers class variable, still in scope. } }); .... } }

java swing user-interface actionlistener

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -