Java 无法将字符串添加到arrayList中

Java 无法将字符串添加到arrayList中,java,Java,我可以知道为什么吗?我已经向addTab方法传递了三个字符串,当我调试时它在变量中,但它说它为null,为什么会这样?我还实例化了arrayList public class STFile implements Serializable{ private ArrayList<String> qnsTitle; private ArrayList<String> qnsImagePath; private ArrayList<String&g

我可以知道为什么吗?我已经向addTab方法传递了三个字符串,当我调试时它在变量中,但它说它为null,为什么会这样?我还实例化了arrayList

public class STFile implements Serializable{

    private ArrayList<String> qnsTitle;
    private ArrayList<String> qnsImagePath;
    private ArrayList<String> qnsSoundPath;
    private Boolean fileExist;
    //Constructor for STFile,gets existing data files if exists and load values from it to data files arraylists, if dont exist
    //arraylists for data file will be instantiated.

    public STFile()
    {
         setFileExists(checkIfAllFileExist());
         if(getFileExist())
         {
             try {
                setQnsTitle(STFile.readFile(STMain.TITLES_PATH));
                setQnsImagePath(STFile.readFile(STMain.IMAGES_PATH));
                setQnsSoundPath(STFile.readFile(STMain.SOUNDS_PATH));

            }catch(IOException e)
            {
                System.out.println("in class STFile, IOEXception");
            }catch(ClassNotFoundException e)
            {
                System.out.println("in class STFile, ClassNotFoundException");
            }
         }else
         {

             File titleFile = new File(STMain.TITLES_PATH);
             File imageFile = new File(STMain.IMAGES_PATH);
             File soundFile = new File(STMain.SOUNDS_PATH);
            qnsTitle = new ArrayList<String>();
            qnsImagePath = new ArrayList<String>();
            qnsSoundPath= new ArrayList<String>();
         }

    }

    public void addTab(String title,String imagePath,String soundPath)
    {
        getQnsTitle().add(title);
        getQnsImagePath().add(imagePath);
        getQnsSoundPath().add(soundPath);
        try {
            writeFiles();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("in STFile addtab Exception");
            e.printStackTrace();
        }
    }

public static ArrayList<String> readFile(String filePath) throws ClassNotFoundException, IOException
    {

                ArrayList<String> arraylist = new ArrayList<String>();
                ObjectInputStream obj_in = null;
                FileInputStream f_in = null;

                try {

                    f_in = new FileInputStream(filePath);
                    obj_in = new ObjectInputStream (f_in);
                    arraylist = (ArrayList<String>)obj_in.readObject();
                    return arraylist;
                }catch(Exception e){
                    return null;
                }finally{
                    f_in.close();
                    obj_in.close();
                    return null;
                }

    }
它一直在扔

Exception in thread "main" java.lang.NullPointerException
    at STFile.addTab(STFile.java:53)
    at STMain.main(STMain.java:18)

具有
返回nullreadFile
中的
块可能是问题所在,请尝试删除该块。

使
返回nullreadFile
中的
块中的code>可能是问题所在,请尝试删除该块。

您的
readFile
方法将始终返回null,因为您得到了
返回null在最后一个块中

因此,如果该文件存在,
qnsTitle
(etc)将为null,导致以后出现
NullPointerException


我强烈建议您也不要像在
readFile
中那样捕捉异常。只捕获特定的异常,如果您必须这样做的话——但在这种情况下,我不会首先捕获,或者可能只是将其包装在不同的异常中。简单地从
catch
块返回
null
,就隐藏了出问题的事实,并进一步引入了另一个问题。我建议您只需扩展方法的
throws
子句(例如,包括
IOException
)并删除catch块。

您的
readFile
方法将始终返回null,因为您得到了
返回null在最后一个块中

因此,如果该文件存在,
qnsTitle
(etc)将为null,导致以后出现
NullPointerException


我强烈建议您也不要像在
readFile
中那样捕捉异常。只捕获特定的异常,如果您必须这样做的话——但在这种情况下,我不会首先捕获,或者可能只是将其包装在不同的异常中。简单地从
catch
块返回
null
,就隐藏了出问题的事实,并进一步引入了另一个问题。我建议您只需扩展方法的
throws
子句(例如,包括
IOException
)并删除catch块。

+1,尽管我有点失望您没有写得很好!)谢谢,注意到并最终解决了问题我删除了null+1,尽管我有点失望你没有写得很好!:)谢谢,注意到并最终解决了问题,我删除了空值
Exception in thread "main" java.lang.NullPointerException
    at STFile.addTab(STFile.java:53)
    at STMain.main(STMain.java:18)