java - NullPointerException happens on onClick method -
i created class validates email format.
public class emailvalidator { private pattern pattern; private matcher matcher; // regular expression email validation private static final string email_pattern = "^[_a-za-z0-9-\\+]+(\\.[_a-za-z0-9-]+)*@[a-za-z0-9-]+(\\.[a-za-z0-9]+)*(\\.[a-za-z]{2,})$"; public emailvalidator() { pattern = pattern.compile(email_pattern); } public boolean validate(final string email) { matcher = pattern.matcher(email); return matcher.matches(); } } by using above class, want implement code executes validation , sends out toast if email not valid format. created code follows.
registeractivity.java
(1) variable
emailvalidator validator; edittext etemail; (2) onclick method
string email = etemail.gettext().tostring(); **if(validator.validate(email) == false)** { toast.maketext(getapplicationcontext(), "wrong email format.", toast.length_long).show(); the code far seems have no problem, when ran it threw nullpointerexception on part above. missing something?
if need know further code, please let me know can update soon. thank you!
update: here's logcat.
07-07 20:05:30.761 342-342/com.marshall.gruppo e/androidruntime﹕ fatal exception: main process: com.marshall.gruppo, pid: 342 java.lang.nullpointerexception @ com.marshall.gruppo.ui.registeractivity$1.onclick(registeractivity.java:101) @ android.view.view.performclick(view.java:4633) @ android.view.view$performclick.run(view.java:19270) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:146) @ android.app.activitythread.main(activitythread.java:5602) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) @ dalvik.system.nativestart.main(native method)
you have create object of emailvalidator class this
emailvalidator validator = new emailvalidator(); if(validator.validate(email) == false) { //do operation here }
Comments
Post a Comment