如果Java中存在文件,请创建一个变量?

时间:2014-08-30 08:40:53

标签: java

所以我有这个代码,

static File overlay1 = new File(Minecraft.getMinecraft().mcDataDir, "\\TVMod\\Pictures\\" + pictureList[0].getName()); 

但是当我运行该代码时,我得到一个NullPointerException,因为pictureList [0]不存在。但我希望它只在pictureList [0]存在时才能生成变量。我希望我能很好地解释这一点

1 个答案:

答案 0 :(得分:0)

 static{
        if (pictureList != null && pictureList.length>0)
            overlay1 = new File(Minecraft.getMinecraft().mcDataDir, "\\TVMod\\Pictures\\" + pictureList[0].getName());
    }
 static File overlay1;
相关问题