本帖最后由 杨兴庭 于 2013-8-2 18:56 编辑
- package cn.itcast.day1;
- import java.lang.reflect.*;
- public class ReflectTest {
- public static void main(String[] args) throws ClassNotFoundException,
- NoSuchMethodException, SecurityException, IllegalAccessException,
- IllegalArgumentException, InvocationTargetException {
- Class startClass = Class.forName(args[0]);
- Method mMain = startClass.getMethod("main", String[].class);
- mMain.invoke(null, (Object)new String[]{"1111","2222","3333"});
- }
- }
- class TestArguments {
- public void main(String[] args) {
- for(String arg : args) {
- System.out.println(arg);
- }
- }
- }
复制代码 运行结果:
Exception in thread "main" java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at cn.itcast.day1.ReflectTest.main(ReflectTest.java:11)
运行配置中把args[0]配置成了cn.itcast.day1.TestArguments
代码也基本是按老师教的写的
可为什么会出错呢???
|