debugging - Java reflection after invoking method the method is not throwing exception -


so created invalidinputexeption exception class , used in setter methods throw exception if there not valid input.

public class kalsi.contestantinformation {      public void setfirstname(string firstname) throws invalidinputexeption {         checkinput(firstname, "please enter letter in first name");         this.firstname = firstname.replaceall("\\s", "").tolowercase();     }      public void setlastname(string lastname) throws invalidinputexeption {         checkinput(lastname, "please enter letter in last name");         this.lastname = lastname.replaceall("\\s", "").tolowercase();     }      public void setstreetnumber(string streetnumber) throws invalidinputexeption {         checkinput(streetnumber, "please enter numbers in street number");         this.streetnumber = streetnumber.replaceall("\\s", "").tolowercase();     }      public void setstreetname(string streetname) throws invalidinputexeption {         checkinput(streetname, "please enter letter in street name name");         this.streetname = streetname.replaceall("\\s", "").tolowercase();     }      public void checkinput(string s, string message) throws invalidinputexeption {         char[] array = s.tochararray();         (char c : array) {             if (!character.isletter(c)) {                 throw new invalidinputexeption(message);             }         }      }      public void checkinput(int i, string message) throws invalidinputexeption {         string s = + "";         char[] array = s.tochararray();         (char c : array) {             if (!character.isdigit(c)) {                 throw new invalidinputexeption(message);             }         }     } } 

and here main method in invoke setfirstname using reflection. note setfirstname not setter method. other methods in array of strings have names of setmethods.

public static void main(string[] args)         throws illegalaccessexception, illegalargumentexception, invocationtargetexception, nosuchmethodexception,         securityexception, classnotfoundexception, instantiationexception {     contestantinformation contestant1 = new contestantinformation();     string[] questions = { "what first name", "what last name", "what street name",             "what sreet number" };     string[] methods = { "setfirstname", "setlastname", "setstreetname", "setstreetnumber" };     (int = 0; < methods.length; i++) {         {             try {                 flag = false;                 system.out.println(questions[i]);                 string scannerinput = scanner.nextline();                 classcontestantinfo.getdeclaredmethod(methods[i], stringparameter).invoke(contestant1,                         scannerinput);             } catch (invocationtargetexception e) {                 if (e.getcause() instanceof invalidinputexeption) {                     system.out.println(e.getmessage());                 }             }         } while (flag);     } } 

the problem when input not string not re ask the question or give error message here output.

what first name 9 null last name

it outputs null , moves next question though supposed output "please enter letter in first name" , ask user input again.

here git clone url if want clone code program realityshowapplication in ics4u repository:

https://github.com/simar1998/ics4u.git 

you showing message of invocationtargetexception, not of invalidinputexception.

you want make sure question repeated setting repeat-flag true.

change catch clause body to:

if (e.getcause() instanceof invalidinputexeption) {     system.out.println(e.getcause().getmessage()); // <-- note: getcause() here     flag = true; // make sure question repeated } 

i suggest change flag inputerror, repeatflag or else more descriptive of function of flag.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -