java - How to send info to a helper function in jsp? -
i creating game. user must login play game. once click login, should go game. in game random number generated, , 100 buttons. buttons have 1-100 values, selecting 1 of buttons, user guess random number. once button clicked should go helper class , compare results see if number clicked matched random number. appropriate message display based on results.
i have created helper class(gghelper.java):
public class gghelper extends httpservlet { static int randomnum=0; static string message = ""; public static int randomnum() { randomnum = (int) (math.random() * 100); system.out.println(randomnum); return randomnum; } public static int returnnum() { return randomnum; } public static string clicks(){ httpservletrequest request = null; int num = integer.parseint(request.getparameter("userentery")); if ( num== randomnum) { message = "you won"; } else if (num > randomnum) { message = "your guess high"; } else { message = "your guess low"; } return message; }
here jsp file(only body), page should display once logged in:
<body> <h1>welcome guessing game</h1> <form action=<%=message = gghelper.clicks()%>> <% (int x = 1; x < 101; x++) { %><input name="num" type = "submit" value="<%=x%>"><%=x%></button> <% } %> </form> <h2><%=message%></h2> </body>
this error once logged in: java.lang.nullpointerexception @ helper.gghelper.clicks(gghelper.java:43)
how fix error?
if read code carefully, see null
try use not null:
httpservletrequest request = null; int num = integer.parseint(request.getparameter("userentery")); // ^^^^^^^
Comments
Post a Comment