質問

I am making a converter from base a to base b. It is saying that I have a nullpointerexception. I have no idea how to fix it really. I know that it probably has to do with going out of bounds with the arraylist, but im not sure. I am new to java so please don't make the answer too complicated. I understand that there is a library feature to convert bases, but my professor is having us write our own.

The nullpointerexception is where the stars are (** * **)

public class NumberBase {

    private static double d;

    private static int i;

    private static ArrayList <Character> c;

    private static double sum;

    private static ArrayList <Integer> result = new ArrayList <Integer>();

    public NumberBase(){
        i = 0;
        c = new ArrayList <Character>();
    }

    public static String convert(String input, int base_in, int base_out){

        while(i < input.length()){
            c.add(input.charAt(i)); (*****)
            i++;
        }

        int digit;

        i = 0;
        while(i < result.size()-1){
        digit = Character.getNumericValue(c.get(i));
        result.add(digit);
        i++;
        }

        d = toBaseTen(base_in);
        String str = "" + d;
        return str;

    }
    public static void main(String args[]){

    }

    public static double toBaseTen(int base_in){

        i--;
        while(i > 0){

            sum = result.get(i)*(Math.pow(base_in, i));
            i--;
        }
        return sum;
    }

    public int fromBaseTen(int base_out){

    }

}

役に立ちましたか?

解決

Your convert method is static. That means that it is a class-wide method. Your ArrayList "c" is a property of the NumberBase class. Since convert is static, it does not have access to Object-specific properties declared in the class (static means it's a method of the class, not a method that acts on an object).

Basically - if you want to access the properties you defined, you have to make a member of the class they are defined for. Static methods don't need an actual Object to function.

If you remove the static keyword from convert:

public String convert(String input, int base_in, int base_out){
    int i = 0;
    while(i < input.length()){
        c.add(Character.valueOf(input.charAt(i))); //(*****)
        i++;
    }

You can call it with:

public static void main(String args[]){
    NumberBase b = new NumberBase();
    String s = b.convert("test", 0, 3);
}

Because your method was static, you never actually instantiated a member of the NumberBase class. This means that your constructor was never called:

public NumberBase(){
    i = 0;
    c = new ArrayList <Character>();
}

Since you were instantiating (creating) your ArrayList Object in your constructor (which was never called), when the convert method tried to access the ArrayList "c" there was nothing there and you got an exception.

EDIT:

If your method must be static, to use an ArrayList inside of it you either need to pass one in as a parameter or instantiate an ArrayList inside of your method.

ArrayList<Character> c = new ArrayList<>();

^ If you put that inside of the body of your method (somewhere before you use it!) you will not get null pointer exceptions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top