简体   繁体   English

当我尝试在主方法之外调用方法时发生NullPointerException

[英]NullPointerException when I try to call a method outside my main method

In the following code I am attempting to read input from a user as a string, then store said input into a ArrayList. 在以下代码中,我尝试以字符串形式读取用户的输入,然后将所述输入存储到ArrayList中。 I created a separate method that is supposed to take the user input from the main and manipulate it into the ArrayList. 我创建了一个单独的方法,该方法应该从主体接收用户输入并将其操纵到ArrayList中。 My problem is that my code throws a NullPointerException when trying to pass the input data from my main method into the seperate method of the same class. 我的问题是,当尝试将输入数据从我的main方法传递到同一类的单独方法中时,我的代码抛出NullPointerException。

Error Messages I recieve: 我收到的错误消息:

Exception in thread "main" java.lang.NullPointerException at SummationPuzzle.convertStr(SummationPuzzle.java:22) at SummationPuzzle.main(SummationPuzzle.java:96) SummationPuzzle.convertStr(SummationPuzzle.java:22)的线程“ main”中的java.lang.NullPointerException异常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);
    }
}

you have declared arrays, but not init them: 您已经声明了数组,但是没有初始化它们:

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

not initialize any more. 不再初始化。 So only you got null pointer exception. 所以只有你有空指针异常。

fsList必须在使用前初始化:

fsList = new ArrayList<Character>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试将一个方法调用到我的主函数中时,我不断收到错误消息 - i keep getting an error when I try to call a method into my main function 当我尝试在没有主要方法的情况下在 class A 中调用 class B 的方法时出现错误 - Error appears when i try to call a method of class B in class A without main method 当我尝试在线程内访问静态方法时,它会抛出NullPointerException - static method throws NullPointerException when I try to access it inside thread 如何在Java中的main方法之外调用方法 - how to call on a method outside of the main method in java 尝试捕获块:无论是在类本身中还是在类外调用方法时 - Try and catch blocks: whether in the class itself or when I call the method outside the class 当该方法单独运行时,我的代码会尝试工作,但是当我在测试程序时调用该方法时…出现捕获错误 - try of my code works when the method is run on its own, but when I call the method when testing my program… catch errors appears 如何将我的主要方法放入try catch块? - How do I place my main method into a try catch block? 我可以在我的主要方法 JAVAXB、JAVAFX 之外执行编组吗 - Can i perform Marshalling outside of my main method, JAVAXB, JAVAFX 是否可以在主类之外调用重写的方法? - Is it possible to call an overridden method outside the main class? Java NullPointerException当我在自定义类上调用方法时 - Java NullPointerException When I call a method on a custom class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM