如何在此代码中处理java.lang.NullPointerException

时间:2023-01-19 21:36:12

This is my Code and it throws java.lang.NullPointerException I tried to handle it using other similar topics on this site but it wasn't useful.

这是我的代码,它抛出java.lang.NullPointerException我试图使用此网站上的其他类似主题处理它,但它没用。

import java.math.BigInteger;
import java.lang.Long;


public class Tamrin7 {

    public static  BigInteger ZERO;
    public static  BigInteger ONE;
    public static  BigInteger TEN;
    public static void main(String[] args){

        BigInteger a = ZERO ;
        BigInteger b = ZERO ;
        BigInteger increment = ONE ;
        int counter = 0 ;
        int thirteen = 13;
        BigInteger bigInt = new BigInteger(String.valueOf(thirteen));
        while(counter != 10000){
            b = a.add(inverse(a))   ;
            if (b.remainder(bigInt) == ZERO)
                ++counter;
            a = a.add(increment);
        }//end of while

        String finall = b.toString();

        System.out.printf("the value of the 10000th number is %s :" , finall );


    }//end of main
    public static BigInteger inverse (BigInteger c){
        BigInteger inversedNum = ZERO ; 
        while (c != ZERO){
            inversedNum = inversedNum.multiply(TEN).add(c.remainder(TEN));
            c = c.divide(TEN);
        }//end of while
        return inversedNum ;
    }
}//end of class

4 个解决方案

#1


0  

You have several problems here.

你有几个问题。

public static  BigInteger ZERO;
public static  BigInteger ONE;
public static  BigInteger TEN;

These are never initialized with values, that's why you get the null pointer in the first place. If you want this to work, you should create objects with or provide a reference to existing ones.

这些永远不会用值初始化,这就是为什么你首先得到空指针。如果您希望这样做,您应该创建对象或提供对现有对象的引用。

Like so:

public static final BigInteger ZERO = BigInteger.ZERO;
public static final BigInteger ONE = BigInteger.ONE;
public static final BigInteger TEN = BigInteger.TEN;

But you should just use BigInteger.ZERO instead of re-declaring them in your code.

但是你应该只使用BigInteger.ZERO而不是在你的代码中重新声明它们。

Also, dont do this:

另外,不要这样做:

 BigInteger bigInt = new BigInteger(String.valueOf(thirteen));

BigInteger has a factory method, that can consume plain int, without having it to be casted to String.

BigInteger有一个工厂方法,它可以使用plain int,而不必将它转换为String。

Like so:

 BigInteger bigInt = BigInteger.valueOf(thirteen);

#2


3  

Where do you initialise these:

你在哪里初始化这些:

public static  BigInteger ZERO;
public static  BigInteger ONE;
public static  BigInteger TEN;

I don't think you do:

我不认为你这样做:

public static  BigInteger ZERO = BigInteger.ZERO;
public static  BigInteger ONE = BigInteger.ONE;
public static  BigInteger TEN = BigInteger.TEN;  

Or, if you're into import static then:

或者,如果您进入import static,那么:

import static java.math.BigInteger.ZERO;
import static java.math.BigInteger.ONE;
import static java.math.BigInteger.TEN;

And delete your declarations.

并删除您的声明。

Also this:

new BigInteger(String.valueOf(thirteen));

Makes a cry a little:

哭一点:

BigInteger.valueOf(thirteen);

Will do just fine.

会做得很好。

#3


0  

There:

public static  BigInteger ZERO;
public static  BigInteger ONE;
public static  BigInteger TEN;

look like they need initialising. e.g.

看起来他们需要初始化。例如

public static  BigInteger ZERO = BigInteger.ZERO;

otherwise they're simply declared object references and consequently null.

否则它们只是声明对象引用,因此为null。

#4


-1  

You need to initialize ZERO, ONE and TEN.

你需要初始化ZERO,ONE和TEN。

I think you wanted to do something like this:

我想你想做这样的事情:

public static final BigInteger ZERO = BigInteger.ZERO;
public static final BigInteger ONE = BigInteger.ONE;
public static final BigInteger TEN = BigInteger.TEN;

You don't need to declare them as constants though, you can just them.

您不需要将它们声明为常量,您可以只使用它们。

#1


0  

You have several problems here.

你有几个问题。

public static  BigInteger ZERO;
public static  BigInteger ONE;
public static  BigInteger TEN;

These are never initialized with values, that's why you get the null pointer in the first place. If you want this to work, you should create objects with or provide a reference to existing ones.

这些永远不会用值初始化,这就是为什么你首先得到空指针。如果您希望这样做,您应该创建对象或提供对现有对象的引用。

Like so:

public static final BigInteger ZERO = BigInteger.ZERO;
public static final BigInteger ONE = BigInteger.ONE;
public static final BigInteger TEN = BigInteger.TEN;

But you should just use BigInteger.ZERO instead of re-declaring them in your code.

但是你应该只使用BigInteger.ZERO而不是在你的代码中重新声明它们。

Also, dont do this:

另外,不要这样做:

 BigInteger bigInt = new BigInteger(String.valueOf(thirteen));

BigInteger has a factory method, that can consume plain int, without having it to be casted to String.

BigInteger有一个工厂方法,它可以使用plain int,而不必将它转换为String。

Like so:

 BigInteger bigInt = BigInteger.valueOf(thirteen);

#2


3  

Where do you initialise these:

你在哪里初始化这些:

public static  BigInteger ZERO;
public static  BigInteger ONE;
public static  BigInteger TEN;

I don't think you do:

我不认为你这样做:

public static  BigInteger ZERO = BigInteger.ZERO;
public static  BigInteger ONE = BigInteger.ONE;
public static  BigInteger TEN = BigInteger.TEN;  

Or, if you're into import static then:

或者,如果您进入import static,那么:

import static java.math.BigInteger.ZERO;
import static java.math.BigInteger.ONE;
import static java.math.BigInteger.TEN;

And delete your declarations.

并删除您的声明。

Also this:

new BigInteger(String.valueOf(thirteen));

Makes a cry a little:

哭一点:

BigInteger.valueOf(thirteen);

Will do just fine.

会做得很好。

#3


0  

There:

public static  BigInteger ZERO;
public static  BigInteger ONE;
public static  BigInteger TEN;

look like they need initialising. e.g.

看起来他们需要初始化。例如

public static  BigInteger ZERO = BigInteger.ZERO;

otherwise they're simply declared object references and consequently null.

否则它们只是声明对象引用,因此为null。

#4


-1  

You need to initialize ZERO, ONE and TEN.

你需要初始化ZERO,ONE和TEN。

I think you wanted to do something like this:

我想你想做这样的事情:

public static final BigInteger ZERO = BigInteger.ZERO;
public static final BigInteger ONE = BigInteger.ONE;
public static final BigInteger TEN = BigInteger.TEN;

You don't need to declare them as constants though, you can just them.

您不需要将它们声明为常量,您可以只使用它们。