【问题标题】:NullPointerException when I try to call a method outside my main method当我尝试调用主方法之外的方法时出现 NullPointerException
【发布时间】:2013-10-25 08:29:27
【问题描述】:

在下面的代码中,我尝试将用户的输入作为字符串读取,然后将所述输入存储到 ArrayList 中。我创建了一个单独的方法,该方法应该从 main 中获取用户输入并将其操作到 ArrayList 中。我的问题是,当我尝试将输入数据从我的主方法传递到同一类的单独方法时,我的代码会抛出 NullPointerException。

我收到的错误消息:

线程“main”中的异常 java.lang.NullPointerException at SummationPuzzle.convertStr(SummationPuzzle.java:22) 在 SummationPuzzle.main(SummationPuzzle.java:96)

import java.util.*;

public class SummationPuzzle 
{

    public static ArrayList<Character> fsList;
    public static ArrayList<Character> lastW;
    public static ArrayList<Character> finaList;

    /**
     * Reads in 3 words entered by user and converts the first two string into a single <Character> ArrayList
     * takes the third string entered and converts it into it's own <Character>ArrayList
     * @param firstW
     * @param secondW
     * @param thirdW
     */
    public static void convertStr(String firstW, String secondW, String thirdW)
    {
        String combined = new String(firstW + secondW); 
        for(int i = 0; i< combined.length(); i++)
        {
            fsList.add(combined.charAt(i));
            for(int j = 0; j< thirdW.length(); j++)
            {
                lastW.add(thirdW.charAt(j));
            }
        }
        removeDuplicate(fsList, lastW);
        //feeds the resulting lists into the removeDuplicate method
    }

    /**
     * Combines two <Character> ArrayList into a one <Character> ArrayList with single instances of the char
     * @param fsList
     * @param lastW
     */
    public static void removeDuplicate(ArrayList<Character> fsList, ArrayList<Character> lastW)
    {
        ArrayList<Character> tempList = new ArrayList<Character>();
        tempList.addAll(fsList);
        tempList.addAll(lastW);
        for(char dupLetter : tempList)
        {
            if(!finaList.contains(dupLetter))
            {
                finaList.add(dupLetter);
            }
        }
        System.out.println(finaList + "This is the list with duplicates removed");
        assignNum(finaList, lastW);
        //feeds results into the assignNum method
    }

    /**
     * Assigns a random number to the char that resides at each address in the <Character> ArrayList
     * if the First char in the lastW ArrayList is present in the finaList <Character> ArrayList then the program
     * assigns that character the value of "1"
     * @param finaList
     * @param lastW
     */
    public static void assignNum(ArrayList<Character> finaList, ArrayList<Character> lastW)
    {
        char[] assignLetter= new char[finaList.size()];
        Random r = new Random();
        for(int i = 0; i< assignLetter.length; i++)
        {
            assignLetter[i] = finaList.get(i);
            assignLetter[i] = (char)r.nextInt(assignLetter.length);
            System.out.println((long)assignLetter[i]);
            if(lastW.get(0).equals(assignLetter[i]))
            {
                assignLetter[i] = 1;
            }
        }

        System.out.println(assignLetter.toString() + "This is the list with numbers assigned");

    }


    //main method
    public static void main(String[] args)

    {
        //Receive user input
        Scanner userIn = new Scanner(System.in);
        System.out.println("Please enter your first word");
        String firstW = userIn.next().trim();
        System.out.println("Please enter your Second word");
        String secondW = userIn.next().trim();
        System.out.println("Please enter your Third word");
        String thirdW = userIn.next().trim();


        //print the summation puzzle
        System.out.println(firstW+ " + " + secondW + " = "+ thirdW);
        convertStr(firstW, secondW, thirdW);
    }
}

【问题讨论】:

    标签: java methods parameters arraylist nullpointerexception


    【解决方案1】:

    你已经声明了数组,但没有初始化它们:

    public static ArrayList<Character> fsList = new ArrayList<Character>();
    public static ArrayList<Character> lastW = new ArrayList<Character>();
    public static ArrayList<Character> finaList = new ArrayList<Character>();
    

    【讨论】:

    • 谢谢,我一直盯着这个程序这么久,我犯了愚蠢的错误
    【解决方案2】:
     public static ArrayList<Character> fsList = new ArrayList<Character>();
     public static ArrayList<Character> lastW = new ArrayList<Character>();
     public static ArrayList<Character> finaList = new ArrayList<Character>();
    

    不再初始化。所以只有你得到空指针异常。

    【讨论】:

      【解决方案3】:

      fsList必须在使用前初始化:

      fsList = new ArrayList<Character>();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-06
        • 2022-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多