首页 新闻 会员 周边

java.lang.ExceptionInInitializerError —— 自己编译的Java程序出现的

0
悬赏园豆:20 [待解决问题]
//这个工具类定义了 使用DOM4J技术 获取xml文档的Document对象(getDocument()方法)

//和write2xml(Document document)向xml文档中写入数据的方法

 

//private static String dbFilePath = "E:/JavaEE_WorkSpace/day12_01_regist/src/users.xml";//这么写没有问题

  

 //private static String dbFilePath;//用静态代码块初始化这个代表xml文档的字符串就出现

 //问题了!!!!! 求解答!!!
   

static {
        URL url = Dom4JUtil.class.getClassLoader().getResource("/src/users.xml");
        dbFilePath = url.getPath();
    }

 

    
    /**
     * 该方法用于获取指定路径的xml文档
     * @return 成功获取到xml文档返回Document对象,失败返回null
     * @throws Exception 如果路径指定的文件不存在,抛出此异常 —— 工具类的异常可以抛也可以不抛
     */
    
    public static Document getDocument() throws Exception{
        SAXReader reader = new SAXReader();
        return reader.read(dbFilePath);
    }
    
    /**
     * 该方法用于向指定路径的xml文档中书写信息
     * @param filePath
     * @throws Exception 如果不存在指定路径的xml文档,抛出此异常
     */
    
    public static void write2xml(Document document) throws Exception{
        OutputStream out = new FileOutputStream(dbFilePath);
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, format);
  
        writer.write(document);
      
        writer.close();
    }
UzumakiNaruto的主页 UzumakiNaruto | 初学一级 | 园豆:182
提问于:2016-04-28 09:17
< >
分享
所有回答(1)
1

private static String filePath;
static{
filePath=XmlUtils.class.getClassLoader().getResource("users.xml").getPath();
}

戒。 | 园豆:202 (菜鸟二级) | 2016-04-29 09:58

为什么这么改?

支持(0) 反对(0) UzumakiNaruto | 园豆:182 (初学一级) | 2016-04-29 20:32

@UzumakiNaruto: 静态代码块的变量要是静态变量,普通变量不行

支持(0) 反对(0) 戒。 | 园豆:202 (菜鸟二级) | 2016-04-29 20:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册