M
Michael Powe
Hello,
I was following along in a book about JSP and made up one of the
examples in NetBeans. I didn't like the way the author set up the
example, as a scriptlet (I don't like the code on the page), so I
modded it slightly, creating a "code behind" class and importing it
into the page. Nothing elaborate here, I'm just playing around.
This is supposed to simulate flipping a coin and updating the coin
count "heads/tails" on a JSP. There's something wrong with the way
I'm doing it, though, which causes the coin to periodically "land on
its edge," so to speak, so I had to put in some code to prevent a
NullPointerException from blowing up the page.
But what is wrong with this code? What am I doing that generates a
NPE if i don't include the "none" catcher?
Thanks.
mp
package wdjsp;
public class RandomNumbers {
public static int randomNumber = 0;
public static int getRandomNumber(){
double rand = generateRandomDouble();
randomNumber = generateRandomInt(rand);
return randomNumber;
}
private static double generateRandomDouble(){
return Math.random();
}
private static int generateRandomInt(double number){
return (int)Math.round(number + 1);
}
} // end class
8<=================================================>8
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="wdjsp.RandomNumbers"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Random Number Test</h1>
<%
String winner = null;
int answer = 0;
synchronized (session){
if (session.isNew()){
session.setAttribute("heads",new Integer(0));
session.setAttribute("tails",new Integer(0));
session.setAttribute("none", new Integer(0));
}
}
if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
winner = "heads";
} else if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 2){
winner = "tails";
} else {
winner = "none";
}
if (!winner.equals("none")){
int oldVal = ((Integer)session.getAttribute(winner)).intValue();
session.setAttribute(winner, new Integer(oldVal + 1));
}
%>
<table border="1" width="50%" cellpadding="1">
<thead>
<tr>
<th>heads</th>
<th>tails</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= session.getAttribute("heads")%></td>
<td><%= session.getAttribute("tails")%></td>
</tr>
</tbody>
</table>
<%
if (winner == "none"){
int noneCount = ((Integer)session.getAttribute("none")).intValue();
session.setAttribute("none", new Integer(noneCount+1));
out.println("None == " + (session.getAttribute("none")) + "<br />\n");
out.println("answer == " + answer);
}
%>
</body>
</html>
I was following along in a book about JSP and made up one of the
examples in NetBeans. I didn't like the way the author set up the
example, as a scriptlet (I don't like the code on the page), so I
modded it slightly, creating a "code behind" class and importing it
into the page. Nothing elaborate here, I'm just playing around.
This is supposed to simulate flipping a coin and updating the coin
count "heads/tails" on a JSP. There's something wrong with the way
I'm doing it, though, which causes the coin to periodically "land on
its edge," so to speak, so I had to put in some code to prevent a
NullPointerException from blowing up the page.
But what is wrong with this code? What am I doing that generates a
NPE if i don't include the "none" catcher?
Thanks.
mp
package wdjsp;
public class RandomNumbers {
public static int randomNumber = 0;
public static int getRandomNumber(){
double rand = generateRandomDouble();
randomNumber = generateRandomInt(rand);
return randomNumber;
}
private static double generateRandomDouble(){
return Math.random();
}
private static int generateRandomInt(double number){
return (int)Math.round(number + 1);
}
} // end class
8<=================================================>8
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="wdjsp.RandomNumbers"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Random Number Test</h1>
<%
String winner = null;
int answer = 0;
synchronized (session){
if (session.isNew()){
session.setAttribute("heads",new Integer(0));
session.setAttribute("tails",new Integer(0));
session.setAttribute("none", new Integer(0));
}
}
if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
winner = "heads";
} else if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 2){
winner = "tails";
} else {
winner = "none";
}
if (!winner.equals("none")){
int oldVal = ((Integer)session.getAttribute(winner)).intValue();
session.setAttribute(winner, new Integer(oldVal + 1));
}
%>
<table border="1" width="50%" cellpadding="1">
<thead>
<tr>
<th>heads</th>
<th>tails</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= session.getAttribute("heads")%></td>
<td><%= session.getAttribute("tails")%></td>
</tr>
</tbody>
</table>
<%
if (winner == "none"){
int noneCount = ((Integer)session.getAttribute("none")).intValue();
session.setAttribute("none", new Integer(noneCount+1));
out.println("None == " + (session.getAttribute("none")) + "<br />\n");
out.println("answer == " + answer);
}
%>
</body>
</html>