* Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed
@ 2010-02-25 1:04 Mr.Spark
2010-02-25 1:20 ` Jeffrey R. Carter
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Mr.Spark @ 2010-02-25 1:04 UTC (permalink / raw)
Hi Guys, I could not solve this problem, when I tried to throw an
exception the code will not be executed to see
the results.
I think the problem is with the array .
The Code :
import java.util.Random;
public class RunAlgorithm {
static int n = 3; // Number of processes
public static void main(String[] args) {
int random_id;
int Lowest_id;
Random rn = new Random();
SecurityModule[] s = new SecurityModule[n];
// Initializing Security Modules
for (int i = 0; i < n; i++) {
s[i].ID = 0;
s[i].State = false;
}
// Generating Random ID numbers for the Security Modules
for (int i = 0; i < n; i++) {
random_id = rn.nextInt(99) + 1;
s[i].ID = random_id; -- the
problem is in this line
log("Random ID = " + random_id);
}
// To get the lowest ID
Lowest_id = GetLowestID(s);
System.out.println(" The Lowest ID is: " + Lowest_id);
// The initiator with the lowest ID will generate a random number K
> 1
int k = rn.nextInt(9) + 2;
log("Round = " + k);
// Generating a random number i in {1,2,3,...n}
int i = rn.nextInt(3) + 1;
log("i = " + i);
}
public static int GetLowestID(SecurityModule s[]) {
int LowestID = s[0].ID;
for (int i = 1, limit = s.length; i < limit; ++i) {
if (s[i].ID < LowestID)
LowestID = s[i].ID;
}
return LowestID;
}
}
I hope that I can find a solution for this problem.
Thanks guys ..
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed
2010-02-25 1:04 Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed Mr.Spark
@ 2010-02-25 1:20 ` Jeffrey R. Carter
2010-02-25 1:20 ` (see below)
2010-02-25 17:52 ` John B. Matthews
2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey R. Carter @ 2010-02-25 1:20 UTC (permalink / raw)
Here's the problem:
> import java.util.Random;
You're using Java instead of Ada.
--
Jeff Carter
"Don't knock masturbation. It's sex with someone I love."
Annie Hall
45
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed
2010-02-25 1:04 Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed Mr.Spark
2010-02-25 1:20 ` Jeffrey R. Carter
@ 2010-02-25 1:20 ` (see below)
2010-02-25 17:52 ` John B. Matthews
2 siblings, 0 replies; 4+ messages in thread
From: (see below) @ 2010-02-25 1:20 UTC (permalink / raw)
On 25/02/2010 01:04, in article
735e9720-4b06-46bb-8602-e04590473f57@z19g2000yqk.googlegroups.com,
"Mr.Spark" <malmanea@gmail.com> wrote:
> I hope that I can find a solution for this problem.
>
> Thanks guys ..
I am resisting. I AM resisting ...
--
Bill Findlay
<surname><forename> chez blueyonder.co.uk
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed
2010-02-25 1:04 Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed Mr.Spark
2010-02-25 1:20 ` Jeffrey R. Carter
2010-02-25 1:20 ` (see below)
@ 2010-02-25 17:52 ` John B. Matthews
2 siblings, 0 replies; 4+ messages in thread
From: John B. Matthews @ 2010-02-25 17:52 UTC (permalink / raw)
In article
<735e9720-4b06-46bb-8602-e04590473f57@z19g2000yqk.googlegroups.com>,
"Mr.Spark" <malmanea@gmail.com> wrote:
> Hi Guys, I could not solve this problem, when I tried to throw an
> exception the code will not be executed to see the results.
> I think the problem is with the array.
Yes, the array had been created, but the elements were null.
I have guessed the missing pieces of your program, fixed the formatting
and altered your getLowestId method. Note that java.util.Random is
unsuitable for security. Followups to comp.lang.java.security.
[...]
import java.util.Random;
public class RunAlgorithm {
static int n = 3; // Number of processes
public static void main(String[] args) {
int randomId;
int lowestId;
Random rn = new Random();
SecurityModule[] s = new SecurityModule[n];
// Initializing Security Modules
for (int i = 0; i < n; i++) {
s[i] = new SecurityModule();
}
// Generating Random ID numbers for the Security Modules
for (SecurityModule sm : s) {
randomId = rn.nextInt(99) + 1;
sm.id = randomId; // the problem is in this line
log("Random ID = " + randomId);
}
// To get the lowest ID
lowestId = getLowestId(s);
log("Lowest ID is: " + lowestId);
// The initiator with the lowest ID will
// generate a random number K > 1
int k = rn.nextInt(9) + 2;
log("Round = " + k);
// Generating a random number i in {1,2,3,...n}
int i = rn.nextInt(3) + 1;
log("i = " + i);
}
private static int getLowestId(SecurityModule s[]) {
int lowestId = Integer.MAX_VALUE;
for (SecurityModule sm : s) {
if (sm.id < lowestId) {
lowestId = sm.id;
}
}
return lowestId;
}
private static void log(String s) {
System.out.println(s);
}
private static class SecurityModule {
int id;
boolean state;
}
}
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2010-02-25 17:52 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-25 1:04 Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed Mr.Spark
2010-02-25 1:20 ` Jeffrey R. Carter
2010-02-25 1:20 ` (see below)
2010-02-25 17:52 ` John B. Matthews
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox