使用getMethod()的NullPointerException

时间:2015-04-11 12:05:01

标签: java

这是我问过的上一个问题的延续。从我的Server.class调用NullPointerException方法时,我似乎得到public static void main

这是我的Server.class

public static void main(String[] args) throws IOException {
   try {
    //what happens here is that I open up a socket and start listening
    }
    catch (Exception X) {System.out.println(X);}
}

这是我在另一个类中的runServer方法:

public static void runServer() throws Exception{

    File sourceFile = new File("/Users/Joel/NetbeansProjects/FYP/src/ui_v2/Server.java");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null);
    fileMgr.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("/Users/Joel/NetbeansProjects/FYP/src/ui_v2")));

    compiler.getTask(null, fileMgr, null, null, null, 
            fileMgr.getJavaFileObjectsFromFiles(Arrays.asList(sourceFile)));
    fileMgr.close();

    try {
        Class params[] = {};
        Object paramsObj[] = {};
        Class thisClass = Class.forName("ui_v2.Server");
        Method thisMethod = thisClass.getMethod(null, params);//this is the line where the exception is thrown
        thisMethod.invoke(thisClass, paramsObj);
    }
    catch (Exception e) {
        e.printStackTrace();
}}

这是我的堆栈跟踪:

java.lang.NullPointerException
at java.lang.Class.searchMethods(Class.java:2995)
at java.lang.Class.privateGetMethodRecursive(Class.java:3040)
at java.lang.Class.getMethod0(Class.java:3010)
at java.lang.Class.getMethod(Class.java:1776)
at ui_v2.GenerateChat.runServer(GenerateChat.java:354)
at ui_v2.GenerateChat.Btn_ConfirmActionPerformed(GenerateChat.java:230)
at ui_v2.GenerateChat.access$000(GenerateChat.java:26)
at ui_v2.GenerateChat$1.actionPerformed(GenerateChat.java:129)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我调试了我的Server.class,我不认为NullPointerException是从那里抛出的。谁能告诉我哪里出错了?谢谢!

3 个答案:

答案 0 :(得分:5)

thisClass.getMethod(null, params);

getMethod()允许查找具有给定名称的类的方法。您为该名称传递null。那可能无法奏效。没有方法具有空名称。

仔细阅读getMethod()的javadoc,并注意两个参数的含义。 main()main(String[] args)的方法不同。

答案 1 :(得分:4)

getMethod中的第一个参数应该是方法名称,但那里有一个null

相反,你应该这样做:

Method thisMethod = thisClass.getMethod("main", String[].class);
String[] args = {};
thisMethod.invoke(null, (Object) args);

答案 2 :(得分:3)

thisClass.getMethod(null, params);

请浏览java documentation。它明确指出名称不应为空。

Throws:
NoSuchMethodException - if a matching method is not found or if the name is "<init>"or "<clinit>".
NullPointerException - if name is null