【问题标题】:Reflection - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0反射 - 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:0
【发布时间】:2014-07-01 08:14:04
【问题描述】:

以下是我尝试从 Oracle 中使用的代码,用于理解反射。 我收到以下错误

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);
        }
    }
}

【问题讨论】:

  • 您认为异常意味着什么?你看过右边的任何相关链接吗?
  • 你如何运行这个应用程序?

标签: java reflection collections indexoutofboundsexception


【解决方案1】:

根据您的异常,您没有将任何参数传递给您的类的 main 方法 -

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");

【讨论】:

  • 谢谢 Elliott Frisch,它现在可以工作了!! :) 如果您有更多链接或程序可以帮助我理解 Java 中反射的概念,请提供帮助。再次感谢您!
  • @SagarVyas 碰巧,yes。另外,如果它现在可以工作,请accept the answer.
  • Elliott Frisch 感谢您的指导。我对这个网站很陌生。
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 2023-03-29
  • 2012-04-06
相关资源
最近更新 更多