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>

Signature
Michael Powe michael@trollope.org Naugatuck CT USA
"If you don't like the news, go out and make some of your own."
-- Scoop Nisker, KSAN-FM, San Francisco
Dale King - 04 Jun 2006 04:18 GMT
> 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
[quoted text clipped - 23 lines]
> }
> }
I didn't look into it for the NPE but you are going about it all wrong
to generate a random coin toss. Use the java.util.Random class which has
a nextBoolean method that will work for generating random true or false
values.

Signature
Dale King
Michael Powe - 04 Jun 2006 13:44 GMT
>>>>> "Dale" == Dale King <"DaleWKing [at]gmail [dot] com"> writes:
Dale> Michael Powe wrote:
>> This is supposed to simulate flipping a coin and updating the
>> coin count "heads/tails" on a JSP. There's something wrong
Dale> I didn't look into it for the NPE but you are going about it
Dale> all wrong to generate a random coin toss. Use the
Dale> java.util.Random class which has a nextBoolean method that
Dale> will work for generating random true or false values.
Thanks, I'll look at that. I was really just copying what was in the
book and modifying it according to my own preferences.
I wish I knew what was causing the error. It isn't a big deal of
course, it's just a trivial JSP example. It's just annoying to not
understand where the error takes place.
Thanks.
mp

Signature
Michael Powe michael@trollope.org Naugatuck CT USA
"When a person behaves in keeping with his conscience, when he
tries to speak as a citizen even under conditions where
citizenship is degraded, it may not lead to anything, yet it might.
But what surely will not lead to anything is when a person calculates
whether it will lead to something or not." -- Vaclav Havel, 1989
Dale King - 04 Jun 2006 04:24 GMT
> 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.
> if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
> winner = "heads";
[quoted text clipped - 3 lines]
> winner = "none";
> }
In this if you are essentially throwing the coin twice (you call
getRandomNumber twice). You are only declaring it tails if you get 2
tails in a row. If you get tails followed by heads you say there was no
winner.
It would be a lot cleaner if you pulled the assignment to answer out of
the if statements.

Signature
Dale King
Red Orchid - 04 Jun 2006 05:22 GMT
Michael Powe <michael+gnus@trollope.org> wrote or quoted in
Message-ID: <871wu5lk3b.fsf@ellen.trollope.org>:
> if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
> winner = "heads";
[quoted text clipped - 3 lines]
> winner = "none";
> }
I think that the above code maybe be wrong.
Because ...
The above code calls 'getRandomNumber' twice for one flip.
I think that the following code is valid.
<code>
answer = .... // get random (1 or 2)
switch (answer) {
case 1:
.. // do something
break;
case 2:
.. // do something
break;
default:
assert false : "Impossible answer";
break;
}
Or
boolean answer;
...
answer = .... // get random (true or false) using Random.nextBoolean
</code>
Michael Powe - 04 Jun 2006 13:39 GMT
>>>>> "Red" == Red Orchid <windfollowcloud@yahoo.com> writes:
Red> Michael Powe <michael+gnus@trollope.org> wrote or quoted in
Red> Message-ID: <871wu5lk3b.fsf@ellen.trollope.org>:
>> if ((answer = wdjsp.RandomNumbers.getRandomNumber()) == 1){
>> winner = "heads"; } else if ((answer =
>> wdjsp.RandomNumbers.getRandomNumber()) == 2){ winner = "tails";
>> } else { winner = "none"; }
>>
Red> I think that the above code maybe be wrong.
Red> Because ... The above code calls 'getRandomNumber' twice for
Red> one flip. I think that the following code is valid.
Yes, thanks to you and Dale for pointing this out. Definitely a
blunder. I'll fix it.
Then I'll try again without the "none" category, which is how I was
protecting against the NPE.
Thanks.
mp

Signature
Michael Powe michael@trollope.org Naugatuck CT USA
'Unless we approve your idea, it will not be permitted, it will not be
allowed.' -- Hilary Rosen, President, RIAA
Mike Schilling - 04 Jun 2006 17:53 GMT
> Hello,
>
[quoted text clipped - 12 lines]
> But what is wrong with this code? What am I doing that generates a
> NPE if i don't include the "none" catcher?
The fact that winner is originally set to null, and without the "none"
catcher, there's no guarantee it gets set to anything else.