java - Boolean Array NullPointerException -
This question is already an answer here
- 3 answers
I create one The program that is trying to generate two major numbers between the two given integers (A and B) for the T cases, I am implementing the prime living of the ereothinithy. My code is below I have read that the code mentioned in the code Line NullPointerException me a result of not start Boolean array. However, I thought I did this here:
boolean [] n = new boolean [b1];
Therefore, I'm confused as to why this exception is being thrown. I do not know any other possible reason why my code is wrong with
My error and input:
1 1 5 PrimeNumberFinder.main (Exception in formula on PrimeNumberFinder " Main "java.lang.NullPointerException .Java: 27)
My code:
class PrimeNumberFinder {public static void (string [] args) {scanner Scheduled Caste = new scanner (in system.); // Read the number of cases of expected problem from the input int t; = Sc.nextInt (); // Read the entire search for prime between each item (Ent I = 1; i & lt; = T; i ++) {// Par A = sc.nextInt (); Int B = sc.nextInt (); // Stitch Boolean [] N = According to the new Boolean [B1], create boolean array to store primes; // Set all values to true (int j = 2; j & lt; = b; j ++) {n [j] = true; } // test for the prime number for all the elements of 'n' (integer k = 2; k * k; l; b = k; k ++) {if (n [k]) {(int l = For Kashmir, L * K & lt; = B, L ++] {N * L * K] = Wrong; (A, B) for print (Int M = A, M & LT; = B; M ++) {// This is the line (below) for which error appears. : NullPointerException if (n [m]) {System.out.println (m); } And {continue; }} System.out.println (""); }}
You have created an array of Boolean
objects , But these are all started on null
you obviously start most elements on true
, but only starting with index 2
We do.
The line with NPE is assuming:
if (n [m]) { m
starts with a
which appears 1
based on the input given by you. But it never started. The number 1
is neither principal nor composite, so you should manage this matter separately.
Comments
Post a Comment