在线程" main"中获取运行时异常java.lang.NullPointerException请帮忙解决同样的问题

时间:2017-12-12 06:55:40

标签: java exception-handling nullpointerexception

我是编程领域的新手,所以在尝试运行以下代码时,我在第19和32行获得了java.lang.NullPointerException

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static int binary[];
    static int i = 0;

    static void DecToBin(int num)
    {
        while (num > 0) {

            **binary[i++] = num % 2;**
            num = num / 2;

        }

            for (i = i - 1; i >= 0; i--) {
                System.out.println(binary[i]);
            }
    }

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a decimal number");
        int n=sc.nextInt();
        **DecToBin(n);**

    }
}

输出:

Enter a decimal number
4
Exception in thread "main" java.lang.NullPointerException
at Solution.DecToBin(Solution.java:19)
at Solution.main(Solution.java:32)

0 个答案:

没有答案