comp.lang.ada
 help / color / mirror / Atom feed
From: "John B. Matthews" <nospam@nospam.invalid>
Subject: Re: Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed
Date: Thu, 25 Feb 2010 12:52:08 -0500
Date: 2010-02-25T12:52:08-05:00	[thread overview]
Message-ID: <nospam-867E0F.12520825022010@news.aioe.org> (raw)
In-Reply-To: 735e9720-4b06-46bb-8602-e04590473f57@z19g2000yqk.googlegroups.com

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>



      parent reply	other threads:[~2010-02-25 17:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox