简体   繁体   English

错误NullPointerException试图调用传递的方法

[英]Error NullPointerException trying to invoke a method passed

Main Activity there is: 主要活动有:

public class Demo {

    public void main() {
        Class[] paramTypes = new Class[1];
        parameterTypes[0] = Integer.Type;// tried also int.class;
        Method method1 = Demo.class.getMethod("method1", parameterTypes);

        OtherClass.askValue(this, new Demo(), method1, 100);
    }

    public void method1(int value) {
        System.out.println(value);
    }

}

In other Class in other java file: 在其他java文件的其他类中:

public class OtherClass {

    private Context ctx;
    private Object obj;
    private Method mtd;
    private int ris;

    public void askValue (Context context, Object object, Method method, int result) {
        ctx = context;
        obj = object;
        mtd = method;
        res = result;

        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setTitle("title");
        builder.setMessage("This is the message");
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.yes, DIonClickPositive);
        builder.setNegativeButton(R.string.no, DIonClickNegative);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

     private DialogInterface.OnClickListener() DIonClickPositive = DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
                // if this button is clicked, close

                Object[] parameters = new Object[1];
                parameters[0] = res;
                try {
                     mtd.invoke(obj, parameters);
                } catch (InvocationTargetException ite) {
                     Toast.makeText (ctx, "EXC ITE ["+ite.getCause()+"]", Toast.LENGTH_SHORT).show();
                }

                dialog.dismiss();
          }
     }

     private DialogInterface.OnClickListener() DIonClickNegative = DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
                // if this button is clicked, close
                dialog.dismiss();
          }
     }

}

When i confirm to the dialog (press positive button), it should invoke the method "method1", but instead it enter in the exception. 当我确认对话框(按下正面按钮)时,它应该调用方法“method1”,而是输入异常。 The getCause() of that exception is "NullPointerException". 该异常的getCause()是“NullPointerException”。 In the android manual there is written it is caused when the first Object of the invoke is "null". 在android手册中写道,它是在调用的第一个Object为“null”时引起的。 I checked it, but it isn't null. 我查了一下,但它不是空的。

if (obj == null) Toast.makeText(ctx, "OBJ NULL !!!", Toast.LENGTH_SHORT).show();

No toast shown, so it isn't null. 没有显示吐司,所以它不是空的。

How can i resolve ? 我怎么解决?

Thanksss Thanksss

PS: i used this example " click here ", post of Blaise Doughan. PS:我用这个例子“ 点击这里 ”,Blaise Doughan的帖子。

Eclipse Console Log (it is Stack Trace ?): Eclipse控制台日志(它是堆栈跟踪?):

[2015-02-25 20:49:47 - Traffic] Dx 
trouble writing output: already prepared
[2015-02-25 20:49:50 - Traffic] ------------------------------
[2015-02-25 20:49:50 - Traffic] Android Launch!
[2015-02-25 20:49:50 - Traffic] adb is running normally.
[2015-02-25 20:49:50 - Traffic] Performing com.example.traffic.Repetitions activity launch
[2015-02-25 20:50:40 - Traffic] Uploading Traffic.apk onto device 'emulator-5554'
[2015-02-25 20:50:41 - Traffic] Installing Traffic.apk...
[2015-02-25 20:50:54 - Traffic] Success!
[2015-02-25 20:50:54 - Traffic] Starting activity com.example.traffic.Repetitions on device emulator-5554
[2015-02-25 20:50:55 - Traffic] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.traffic/.Repetitions }

I guess that after invoking 我想在调用之后

Method method1 = Demo.class.getMethod("method1", parameterTypes);

method1 will be null . method1将为null This is because there is no method 这是因为没有方法

method1(Integer arg0)

which you call after setting 您在设置后调用

parameterTypes[0] = Integer.Type

If you later call 如果你以后打电话

mtd.invoke(obj, parameters);

you will get a NullPointerException . 你会得到一个NullPointerException

In your Demo class you can define the method 在您的Demo类中,您可以定义方法

public void method1(Integer value) {
    System.out.println(value.intValue());
}

That should work. 这应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用Reflection在私有字段上调用方法时出现NullPointerException - NullPointerException while trying to invoke method on private field using Reflection 为什么在尝试调用构造函数或方法时将“ this”作为参数隐式传递? - Why is “this” being implicitly passed as a parameter whenever trying to invoke a constructor or a method? Java:Method.invoke(this,args)NullPointerException - Java: Method.invoke (this, args) NullPointerException 尝试调用片段接口方法时发生NullPointerException - NullPointerException while attempting to invoke fragment interface method Retrofit android make error java.lang.NullPointerException: Attempt to invoke interface method - Retrofit android make error java.lang.NullPointerException: Attempt to invoke interface method 尝试调用方法时发生IllegalArgumentException - IllegalArgumentException while trying to invoke method 尝试验证传递给方法的值 - Trying to validate a passed value to a method 尝试从动态类调用方法时发生NullPointerException - NullPointerException while trying to invoke methods from dynamic classes 虚方法错误和NullPointerException错误 - Virtual Method Error and NullPointerException Error 如何解决这个问题android错误? (java.lang.NullPointerException: 尝试调用虚方法 'android.content.res.XmlResourceParser) - how to solve this problem android error ?? (java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM