2014-05-13 2 views
-6

다음은 Reflection을 이해하기 위해 Oracle에서 작업 한 코드입니다. 나는리플렉션 - 스레드 "main"의 예외 java.lang.ArrayIndexOutOfBoundsException : 0

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 
at classdeclarationspy.ClassDeclarationSpy.main(ClassDeclarationSpy.java:29) 

이 오류를 해결 도와주세요 다음과 같은 오류를 얻고있다. 당신의 예외를 바탕으로

/** 
* 
* @author 113282 
*/ 
public class ClassDeclarationSpy { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 

     try { 

      Class<?> c = Class.forName(args[0]); 
      out.format("Class:%n %s%n%n", c.getCanonicalName()); 
      out.format("Modifiers:%n %s%n%n", Modifier.toString(c.getModifiers())); 
      out.format("Type Parameters:%n"); 





      TypeVariable[] tv = c.getTypeParameters(); 
      if (tv.length != 0) { 
       out.format(" "); 
       for (TypeVariable t : tv) 
        out.format("%s ", t.getName()); 
       out.format("%n%n"); 
      } else { 
       out.format(" -- No Type Parameters --%n%n"); 
      } 

      out.format("Implemented Interfaces:%n"); 
      Type[] intfs = c.getGenericInterfaces(); 
      if (intfs.length != 0) { 
       for (Type intf : intfs) 
        out.format(" %s%n", intf.toString()); 
       out.format("%n"); 
      } else { 
       out.format(" -- No Implemented Interfaces --%n%n"); 
      } 

      out.format("Inheritance Path:%n"); 
      List<Class> l = new ArrayList<Class>(); 
      printAncestor(c, l); 
      if (l.size() != 0) { 
       for (Class<?> cl : l) 
        out.format(" %s%n", cl.getCanonicalName()); 
       out.format("%n"); 
      } else { 
       out.format(" -- No Super Classes --%n%n"); 
      } 

      out.format("Annotations:%n"); 
      Annotation[] ann = c.getAnnotations(); 
      if (ann.length != 0) { 
       for (Annotation a : ann) 
        out.format(" %s%n", a.toString()); 
       out.format("%n"); 
      } else { 
       out.format(" -- No Annotations --%n%n"); 
      } 

      // production code should handle this exception more gracefully 
     } catch (ClassNotFoundException x) { 

      if (args.length == 0) { 
       throw new IllegalArgumentException("year is required"); 
      } 
     } 

    } 


    private static void printAncestor(Class<?> c, List<Class> l) { 
     Class<?> ancestor = c.getSuperclass(); 
     if (ancestor != null) { 
      l.add(ancestor); 
      printAncestor(ancestor, l); 
     } 
    } 
} 
+4

당신은 예외가 의미에 대해 어떻게 생각하십니까? 오른쪽에있는 관련 링크를 살펴 보셨습니까? –

+0

이 애플리케이션을 어떻게 실행합니까? – Pshemo

답변

1

, 당신이 주요 방법 '클래스에 인수를 전달하지 않습니다 -

System.out.println(Arrays.toString(args)); // <-- to display your arguments. 
// Class<?> c = Class.forName(args[0]); // <-- you should have a default 
Class<?> c = Class.forName(args.length > 0 ? args[0] : "java.lang.Object"); 
+0

Elliott Frisch에 감사드립니다. :) 만약 당신이 나를 좀 더 도움이 자바에서 반사의 개념을 이해하는 데 도움이 될 것입니다 더 많은 링크 또는 프로그램이 있습니다. 다시 한번 감사드립니다! –

+0

@SagarVyas 그렇게되면 [예] (http://www.frischcode.com/2013/11/reflections.html). 또한 작동중인 경우 [답변 수락] (http://meta.stackexchange.com/a/5235/243725)을 클릭하십시오. –

+0

Elliott Frisch 안내해 주셔서 감사합니다. 나는이 웹 사이트에 대해 아주 새로운 사람이다. –

관련 문제