Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6214867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:57:06+00:00 2026-05-24T06:57:06+00:00

I am using org.apache.commons.beanutils.MethodUtils.invokeMethod . It is giving me Null pointer exception . Following

  • 0

I am using org.apache.commons.beanutils.MethodUtils.invokeMethod. It is giving me Null pointer exception.

Following is my first class

package com;

import org.apache.commons.beanutils.MethodUtils;

public class test {

    public static void main(String[] args) {
        test t = new test();
        t.getName();
    }

    void getName() {
        try {
            Class c = Class.forName("com.test1");
            Object o = c.newInstance();
            System.out.println(o);
            String value = (String) MethodUtils.invokeMethod(o, "getValue",null);
            System.out.println("Results from getValue: " + value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Following is my second class

package com;

public class test1 {

    public String getValue() {
        return "value";
    }

}

When i try to run test it gives me the following error

    com.test1@1add2dd
    java.lang.NullPointerException
    at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:167)
    at com.test.getName(test.java:18)
    at com.test.main(test.java:9)

Please help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:57:08+00:00Added an answer on May 24, 2026 at 6:57 am

    I expect the short answer is that you can’t provide null as the third argument to MethodUtils.invokeMethod. I’ll explain why I suspect this is so (and the Javadocs could really do with mentioning this, if true).

    MethodUtils is almost certainly just a layer around the standard Java reflection library. This takes a method signature, in the form of a name and the classes or the arguments, in order to look up a method. This is required since one can override a method:

    public void foo(String s) {
        System.out.println("Got a string: " + s);
    }
    
    public void foo(int i) {
        System.out.println("Got an int: " + i);
    }
    

    So the invokeMethod call is bundling a few steps together, but it still needs to be able to look up the right java.lang.reflect.Method object. And in order to do this, it’s going to need to know the type of the argument(s) (it looks like the version you’re calling is only for unary methods). So the only way I can see it’s able to do this, is to call arg.getClass() on the object you pass in – and hey presto, an NPE if you pass in null.


    Edit: I was working with the assumption that you intended to call the invokeMethod(Object object, String methodName, Object arg) method – which would indeed exhibit the problems above. After Adam’s comment below, it seems quite possible that you wanted to call the invokeMethod(Object object, String methodName, Object[] args) method which I believe would have worked in this case.

    In that case your problem is caused by the compiler’s type inference and method signature resolution. The null literal can represent any non-primitive type, so it could work for either Object or Object[], while at the same time not representing either of them. Because of this (and some part of the spec that I’m not 100% sure of) the compiler interprets your null as an Object and calls that method, with the consequences outlined above.

    You could have avoided this if your code contained sufficient information to identify the null as a null array – either by casting:

    String value = (String) MethodUtils.invokeMethod(o, "getValue", (Object[])null);
    

    or by assigning to a strongly-typed variable:

    Object[] args = null;
    String value = (String) MethodUtils.invokeMethod(o, "getValue", args);
    

    Asides from the type inference fixes above, you can work around this problem with MethodUtils by supplying the arguments’ class information explicitly:

    MethodUtils.invokeMethod(o, "getValue", new Object[0], new Class[0]);
    

    but this would be relatively straightforward, and perhaps even clearer, using standard reflection:

    void getName() {
        try {
            Class c = Class.forName("com.test1");
            Object o = c.newInstance();
            System.out.println(o);
    
            // The next two lines replace the MethodUtils call
            Method getValueMethod = c.getMethod("getValue");
            String value = getValueMethod.invoke(o);
    
            System.out.println("Results from getValue: " + value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class: import org.apache.commons.beanutils.BeanUtils; import com.thoughtworks.xstream.XStream; ... public class MyBean {
I am using the following approach when logging from my class: import org.apache.commons.logging.Log; import
In Java, we're using the following package to programmatically create excel documents: org.apache.poi.hssf If
I've been using the org.apache.commons.mail.HtmlEmail class, from apache commons-mail, for some time. Eventually, some
I am getting following exception while ftp file over to some other machine. org.apache.commons.net.io.CopyStreamException:
I am using the getResponseBody() method of the org.apache.commons.httpclient.methods.PostMethod class. However, I am always
I tried to use the file upload using Apache Commons but the following exception
I am using org.apache.commons.validator.routines.DateValidator to validate a date with a simple date pattern dd/mm/yyyy
We're using the PostMethod in org.apache.commons.httpclient.methods and noticed that the recycle method from the
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTPFile; import java.io.*; public class FTPUpload{ public static boolean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.