开发者

Unable to add strings into arrayList

may i know why? i have passed in three strings to the addTab method and it is there in the variable when i debug but it says it is null why is that so? i have also instantiated the 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);
                    arraylis开发者_运维技巧t = (ArrayList<String>)obj_in.readObject();
                    return arraylist;
                }catch(Exception e){
                    return null;
                }finally{
                    f_in.close();
                    obj_in.close();
                    return null;
                }

    }

main method.

STFile file = new STFile();
    file.addTab("Title", "image", "sound");

it keeps throwing

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


Your readFile method will always return null, because you've got return null; in your finally block.

So if the file exists, qnsTitle (etc) will be null, causing the NullPointerException later.

I would strongly advise you not to catch Exception in the way you're doing in readFile, either. Only catch specific exceptions if you must do so at all - but in this case I wouldn't in the first place, or possibly only to wrap it in a different exception. Simply returning null from the catch block is hiding the fact that something's gone wrong, and introducing another problem further down the line. I suggest you just expand the throws clause of your method (e.g. to include IOException) and remove the catch block.


having return null; in your finally block in readFile could be the problem, try removing that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜