From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 509fcb8fb1,d10913d8d96a057b,start X-Google-Attributes: gid509fcb8fb1,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!feeder.news-service.com!news.mixmin.net!aioe.org!not-for-mail From: "John B. Matthews" Newsgroups: comp.lang.ada,comp.lang.java.security Subject: Re: Java.Lang.NullPointerException -- Array Problem -- Couldn't fixed Followup-To: comp.lang.java.security Date: Thu, 25 Feb 2010 12:52:08 -0500 Organization: The Wasteland Message-ID: References: <735e9720-4b06-46bb-8602-e04590473f57@z19g2000yqk.googlegroups.com> NNTP-Posting-Host: LQJtZWzu+iKlBROuDg+IUg.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org X-Notice: Filtered by postfilter v. 0.8.2 User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Xref: g2news1.google.com comp.lang.ada:9324 comp.lang.java.security:276 Date: 2010-02-25T12:52:08-05:00 List-Id: In article <735e9720-4b06-46bb-8602-e04590473f57@z19g2000yqk.googlegroups.com>, "Mr.Spark" 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