java - (SOLVED) Calling A Public Method Causes NullPointerException - Android -


ahm, hey everyone, want ask problem in program creating. here snippet codes

playactivity.class

//more codes here  public void stflabel(int numface, context ct) {      try {         if(numface > 0)             facelebel.settext("face hint : see human");         else             facelebel.settext("face hint : you?");     }     catch(nullpointerexception e)     {         e.printstacktrace();             log.d(tag, "stflabel has error");         }     } //more codes here 

*camerapreview.class*

//more codes ... playactivity pact = new playactivity(); ... //more codes .. public void pausy(int numface) { pact.stflabel(numface, mcontext); } 

logcat

04-05 16:11:26.150: d/facedetection(27929): face detected: 1 face 1 location x: 65y: -1 04-05 16:11:26.150: w/system.err(27929): java.lang.nullpointerexception 04-05 16:11:26.150: w/system.err(27929):    @ com.delihente.faceplay.playactivity.stflabel(playactivity.java:90) 04-05 16:11:26.150: w/system.err(27929):    @ com.delihente.faceplay.camerapreview.pausy(camerapreview.java:62) 04-05 16:11:26.150: w/system.err(27929):    @ com.delihente.faceplay.camerapreview$1.onfacedetection(camerapreview.java:53) 04-05 16:11:26.150: w/system.err(27929):    @ android.hardware.camera$eventhandler.handlemessage(camera.java) 04-05 16:11:26.150: w/system.err(27929):    @ android.os.handler.dispatchmessage(handler.java) 04-05 16:11:26.150: w/system.err(27929):    @ android.os.looper.loop(looper.java) 04-05 16:11:26.150: w/system.err(27929):    @ android.app.activitythread.main(activitythread.java) 04-05 16:11:26.150: w/system.err(27929):    @ java.lang.reflect.method.invokenative(native method) 04-05 16:11:26.150: w/system.err(27929):    @ java.lang.reflect.method.invoke(method.java:511) 04-05 16:11:26.150: w/system.err(27929):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java) 04-05 16:11:26.150: w/system.err(27929):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java) 04-05 16:11:26.150: w/system.err(27929):    @ dalvik.system.nativestart.main(native method) 04-05 16:11:26.150: d/(27929): stflabel has error 

more info :

playacvitiy.class extends activity (main class) camerapreview.class - camera preview class provided google android development facelebel = textview on playacvitiy.class mcontext = playactivity.class context 

i want know causing nullpointerexception (a fix better), have read 3 pages of google search problem none of has helped me. , also, please don't mention change method static because cannot because changes textview label when camerapreview.class detects/undetects faces.. in advance!

edit : have read many related questions here, none of them seem help

edit 2 : problem solved, all, specially prafulbhatnagar, have learned using method in main activity needs reference context. everyone!

in android should never create activity object on own..

when launch application app tray system creates object launcher activity , launch it.. use startactivity() if want go 1 screen another, here again system creates object of activity..

so in general system creates object of activity , calls life cycle method oncreate(), onstart() etc.. , use these lifecycle callback initialize/de-initialize state of activity..

the problem in code trying create activity object on own; there 2 instances 1 created system state has been initialized , 1 created code state on class has not been initialize since there no call lifecycle method hence nullpointerexception.

you can use following code fix error:

((playactivity)mcontext).stflabel(numface, mcontext);... assuming mcontext stores reference playactivity

hope helps..


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -