简体   繁体   中英

How to handle java.lang.NullPointerException in this code

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.

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

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 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.

There:

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

look like they need initialising . eg

public static  BigInteger ZERO = BigInteger.ZERO;

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

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.

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.

Like so:

 BigInteger bigInt = BigInteger.valueOf(thirteen);

You need to initialize ZERO, ONE and 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.

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