应该初始化的ArrayList返回Null / Thing NullPointerException

时间:2018-12-20 00:57:36

标签: java nullpointerexception null-pointer

注意:这是先前问题的重发,该问题被标记为“什么是NullPointerException”线程的重复项;但是,我已经读完了,如果他们回答了我的问题,那么我显然是愚蠢的并且很想念它。

我正在使用一种为RPG随机生成字符的工具,并且正在使用ArrayList存储生成的字符。

但是,每当我尝试使用get方法返回列表时

public ArrayList<Character> getCharacters() {
    return characters;
}

它只是返回null,然后抛出NPE。

我有一个方法可以从文件中加载以前保存的字符,现在这也是初始化列表的地方:

public class Character {

ArrayList<Character> characters;
ArrayList<Character> savedCharacters;

....

public void loadCharacters() {
    characters = new ArrayList<Character>();
    savedCharacters = new ArrayList<Character>();
    try {
        FileInputStream fis = new FileInputStream("characters\\characters.scf");
        ObjectInputStream ois = new ObjectInputStream(fis);
        try {
            savedCharacters.addAll((ArrayList<Character>) ois.readObject());
            System.out.println(savedCharacters.toString());
            characters = savedCharacters;
        } catch (EOFException e) {
            System.out.println("Caught");
        }
        ois.close();
    } catch (FileNotFoundException e) {
        System.out.println("Could not load characters. Creating blank characters set, please reopen program.");
        ArrayList<Character> characters = new ArrayList<Character>();
        try {
            FileOutputStream fos = new FileOutputStream("characters\\characters.scf");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            Character ca = new Character();
            ca.generateCharacter();
            oos.writeObject(characters);
            oos.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

此方法在此类中被调用:

public class MainFrame extends JFrame {

private GeneratorGUI generator;
//private StoryCreator storyCreator;

public MainFrame() {
    super("Random Character Generator v0.1");
    Backstories b = new Backstories();
    Character c = new Character();
    b.loadDefaultBackstories();
    c.loadCharacters();
    System.out.println("Loaded");
    // Heat at 350 for however long it takes to get this working
    // Update 1: Quite a while it would seem
    generator = new GeneratorGUI();
    //StoryCreator storyCreator;
    //storyCreator = new StoryCreator();

    setLayout(new BorderLayout());
    //add(storyCreator, BorderLayout.CENTER);
    add(generator);
    setSize(1500, 800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}
}

最后,在构造函数中调用getCharacters方法以设置表的数据:

public class GeneratorGUI extends JPanel {
// Gather ingredients
private JTextArea textArea;
private JTextField nameField;
private JPanel buttonPanel;
private JPanel namePanel;
private JButton generateButton;
private JButton enterName;
private JButton saveButton;
private JButton show;
private CharacterList cList;
private Character c;
private int row;
private boolean isVisible = false;

public GeneratorGUI() {
    System.out.println("Generating");
// Add to pan and sautee
    textArea = new JTextArea();
    buttonPanel = new JPanel();
    namePanel = new JPanel();
    nameField = new JTextField();
    generateButton = new JButton("Generate a character");
    enterName = new JButton("Enter a name for the currently selected character");
    saveButton = new JButton("Save the currently selected character");
    show = new JButton("Show all characters");
    cList = new CharacterList();

    ....

    cList.setData(c.getCharacters());

它应该在调用setData时进行初始化,我什至使用了一些输出来确保它在加载数据之前没有尝试创建GeneratorGUI(如图所示)。 ArrayList当前为空,但仍不应该返回null。我什至还有一个其他对象在做其他事情的列表,当列表为空时,它们可以正常工作。 最后,这是堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GeneratorGUI.<init>(GeneratorGUI.java:103)
at MainFrame.<init>(MainFrame.java:29)

编辑:我将相关代码(loadCharacters和getCharacters)放入单独的类/项目中,列表上的toString输出空列表(但不为null)。此处的代码:

public class Character {
ArrayList<Character> characters;
ArrayList<Character> savedCharacters;

public ArrayList<Character> getCharacters() {
    return characters;
}

public void loadCharacters() {
    characters = new ArrayList<Character>();
    savedCharacters = new ArrayList<Character>();
    /*try {
        FileInputStream fis = new FileInputStream("characters\\characters.scf");
        ObjectInputStream ois = new ObjectInputStream(fis);
        try {
            savedCharacters.addAll((ArrayList<Character>) ois.readObject());
            System.out.println(savedCharacters.toString());
            characters = savedCharacters;
        } catch (EOFException e) {
            System.out.println("Caught");
        }
        ois.close();
    } catch (FileNotFoundException e) {
        System.out.println("Could not load characters. Creating blank characters set, please reopen program.");
        ArrayList<Character> characters = new ArrayList<Character>();
        try {
            FileOutputStream fos = new FileOutputStream("characters\\characters.scf");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            Character ca = new Character();
            // ca.generateCharacter();
            oos.writeObject(characters);
            oos.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
*/
}

public static void main(String[] args) {
    Character c = new Character();
    c.loadCharacters();
    System.out.println(c.getCharacters());

}

}

但是,我仍然不知道为什么这行得通,而普通的代码行不通。

0 个答案:

没有答案