我是编程领域的新手,所以在尝试运行以下代码时,我在第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)