简体   繁体   中英

Why am I receiving a null pointer exception?

When I call the calculateStringValue method on any string within the main method, it works perfectly fine. When I try to call the method within the printStringsWithValues method, I get a null pointer exception. Does this have something to do with the string being in an ArrayList ?

public static void main(String[] args){

    List<String> testList = new ArrayList<>();
    testList.add("apple");
    testList.add("hello");
    printStringsWithValues(testList);}


public static void printStringsWithValues(List<String> strings)
{
    for (String string : strings) {

        System.out.println(string + ",    " + calculateStringValue(string));
    }
}

public static int calculateStringValue(String word)
{
    int value = 0;

    for(int j=0; j < word.length(); j++)
    {
        if(letterValueMap.containsKey(word.charAt(j)))
        {
            value += letterValueMap.get(word.charAt(j));
        }    
        else{
            return -1;
        }
    }
    return value; 
}

Using a contextual inference of the type of letterValueMap I assume that it is a Map<Character, Integer> , so I tried to create such kind of map and assign some arbitrary values in order to test your code.

Here is what I did

First declare a letterValueMap variable and then feed it with some values using a static initialization block

public class NullPointerHandler {

    private static Map<Character, Integer> letterValueMap;
    static {
        letterValueMap = new HashMap<Character, Integer>();
        for (int i = 'a'; i<= 'a'+25; i++) {
            letterValueMap.put((char)i, i);
        }

        for (int i = 'A'; i<= 'A'+25; i++) {
            letterValueMap.put((char)i, i);
        }

        letterValueMap.put(' ', 32);
    }

Then I simply use the other part of your code unchanged (except that I have added a few more words for testing purpose)!

        public static void main(String[] args){

            List<String> testList = new ArrayList<String>();
            testList.add("apple");
            testList.add("hello");
            testList.add("ROME");
            testList.add("rome");
            testList.add("Julius Cesar");
            printStringsWithValues(testList);
        }

        public static void printStringsWithValues(List<String> strings)
        {
            for (String string : strings) {

            System.out.println(string + ",    " + calculateStringValue(string));
        }
    }

    public static int calculateStringValue(String word)
    {
        int value = 0;

        for(int j=0; j < word.length(); j++)
        {
            if(letterValueMap.containsKey(word.charAt(j)))
            {
                value += letterValueMap.get(word.charAt(j));
            }    
            else{
                return -1;
            }
        }
        return value; 
    }

}

Testing it gives me the expected results, without generating any null pointer exception

没有空指针

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