简体   繁体   中英

split method- null pointer exception generated

I've written a code to calculate the no of factors of a given list of elements. INPUT: test- no of test cases num- no of elements in 1 test case numarr- string in which the values(whose product's factors are to be found) is divide by spaces.

When input is:

  • 3
  • 3
  • 3 5 7
  • 3
  • 2 4 6
  • 2
  • 5 5 Ideally, the output should be
  • 8
  • 10
  • 3

But, Exception is:

 Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:31)


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int test = 0;

        Scanner scn = new Scanner(System.in);
        if (scn.hasNextLine())
            test = scn.nextInt();
        int op = 0;
        int[] out = new int[test];
        while ((test <= 100) && (test > 0)) {
            int num = 0;
            if (scn.hasNextLine())
                num = scn.nextInt();
            if (num <= 10) {
                String numarr = null;
                Scanner sc = new Scanner(System.in);
                if (sc.hasNextLine())
                    numarr = sc.nextLine();

                String splitt[] = null;
                if (numarr != null)

                splitt = numarr.split(" ");                           <--ERROR!!!
                if (splitt.length == num) {
                    double[] arr = new double[splitt.length];
                    int i = 0;
                    while (i < splitt.length) {
                        arr[i] = Double.parseDouble(splitt[i]);

                        ++i;
                    }

                    i = 0;
                    double prod = 1;
                    while (i < arr.length) {
                        prod *= arr[i];
                        ++i;
                    }

                    double[] factor = new double[100000];
                    int value = 0;
                    pfac(prod, factor);
                    for (i = 0; (i < factor.length) && (factor[i] != 0); ++i) {

                        value += 1;
                    }

                    out[op] = value;
                    op++;
                }
            }

            --test;
        }
        for (int i = 0; i < op; ++i) {
            System.out.println(out[i]);
        }

    }

    private static void pfac(double n, double[] factor) {
        int pos = 0;

        long max = (long) Math.sqrt(n);

        for (long i = 1; i <= max; ++i) {
            if (n % i == 0) {
                factor[pos] = i;
                pos += 1;
                if (n / i != i) {
                    factor[pos] = n / i;
                    pos += 1;
                }
            }
        }
    }

}

Think about what your code is doing:

if(numarr!=null)
    splitt=numarr.split(" ");
if(splitt.length==num)
{
...
}

If numarr is null you aren't doing the split, which means splitt is still null when you start using it.

Put the whole thing in {}.

if(numarr!=null)
{
    splitt=numarr.split(" ");
    if(splitt.length==num)
    {
    ...
    }
}

The line you indicate can't throw an NPE, since the preceding if statement protects that from ever happening. In the cases where numarr is null however, you will get an NPE on the next row:

if (splitt.length==num)

I would guess this is a case of you thinking the if statement is covering the next row too. It is good practice to always use curly braces in your if statements, to clearly mark where they end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM