2010-07-15 37 views
0

私人字符串getEmailTemplateWithActualValueForAccount(字符串模板,客户帐户)抛出:IllegalArgumentException - ,IllegalAccessException,{的InvocationTargetException例外,在java中

Map<String,String> map = new HashMap<String, String>(); 
    List<String> listTags = new ArrayList<String>(); 
    Map<Method, String> methodList = new HashMap<Method, String>(); 

    int startIndex=0; 
    int endIndex=0; 

    for(int i=0; i<template.length(); i++) 
    { 
     char ch = template.charAt(i); 
     if(ch=='$') 
      startIndex = i+1; 
     if(ch=='#') 
     { 
      endIndex = i+1; 
      listTags.add(template.substring(startIndex,endIndex)); 
     } 

    } 

    Method[] methods = Account.class.getMethods(); 

    for (Method method : methods) { 
     String methodName = method.getName(); 
     if(method.getName().startsWith("get")) 
     { 
      methodList.put(method, methodName.substring(3,methodName.length()).toUpperCase()+"#"); 
     } 
    } 

    Set<Method> methodKeySet = methodList.keySet(); 
    for (Method method : methodKeySet) { 
     for (String string : listTags) { 

      if(methodList.get(method).equals(string)) 
      { 
       try{ 
        Object obj = method.invoke(account, null); 
        if(obj!=null) 
         map.put(string, obj.toString()); 
       }catch(NullPointerException e){ 
       } 
      } 
     } 
    } 

    final StringBuilder list = new StringBuilder("\\$("); 
    for(final String key: map.keySet()) 
    { 
     list.append(key); 
     list.append("|"); 
    } 
    list.append("[^\\s\\S])"); 
    Pattern pattern = Pattern.compile(list.toString()); 
    Matcher matcher = pattern.matcher(template); 


    final StringBuffer stringBuffer = new StringBuffer(); 
    while(matcher.find()){ 
     final String string = matcher.group(1); 
     matcher.appendReplacement(stringBuffer, map.get(string)); 
    } 
    matcher.appendTail(stringBuffer); 

    return stringBuffer.toString(); 
} 

我在代码“对象OBJ = method.invoke线异常(账号,空) ;” 代码完全正常工作,但由于此代码在调度程序中,它将在jboss服务器上每20秒创建一个日志。

+1

如果您将问题添加到问题中,您会得到更好的回复。你有代码抛出它吗?如果是这样发布。你担心抓住它吗?如果是这样,有什么方法抛出它,你担心? – corsiKa 2010-07-15 13:39:08

+0

你能提供你的代码抛出这个异常吗? – Longball27 2010-07-15 13:44:21

+0

...所以代码不完美,对不对?只是因为代码编译,并不能完全按照你想要的方式工作。很明显,在这种情况下它并不是,所以事实上并不是“完美的工作”。 – polygenelubricants 2010-07-15 13:46:40

回答

0

根据Javadoc,Method的invoke方法会抛出InvocationTargetException“如果基础方法抛出异常”。所以你最好看看你正在调用的方法来找出它抛出异常的原因。检查异常堆栈跟踪以查找根本原因。

0

而不是捕获NullPointerException,你应该赶上InvocationTargetException,并检查包装的异常。