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 7497837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:14:29+00:00 2026-05-29T19:14:29+00:00

If I call a method using reflection, the only way I can get it

  • 0

If I call a method using reflection, the only way I can get it to work properly without throwing a null pointer exception is by returning an int value in the method I’m calling.

For example, the method I want to call:

public int setScore(int n)
{
this.score = n;
return 1;
}

The way I call it:

Method setScore = myClass.getMethod("setScore", new Class<?>[]{int.class});
Object returnValue = setScore.invoke(builder, new Object[]{theScore});

Changing the return type to void and calling it seems to always throw a null pointer exception. Do I need to change how I am approaching things for void methods?

Thanks

  • 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-29T19:14:30+00:00Added an answer on May 29, 2026 at 7:14 pm

    Can you show us where the NullPointerException is thrown? This codes works correctly:

    public void setScore(int n)
    {
        this.score = n;
    }
    

    Note that I simplified your code a bit using varargs:

    Method setScore = builder.getClass().getMethod("setScore", int.class);
    Object returnValue = setScore.invoke(builder, theScore);
    

    Obviously in this case returnValue is null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to Invoke an overloaded method using reflection in .NET (2.0).
I have a question. Is it possible to call generic method using reflection in
I call function in .NET using Reflection (Method.Invoke). If an error occurs inside this
How do I get a method using reflection based on a parameter value when
I'm trying to iterate over an enum, and call a method using each of
I'm trying to call a Page Method using a jQuery 'attached' event function, in
I'm getting a java outOfMemoryError when I call this method - i'm using it
I'm using ASP.NET and attempting to call a method with a signature of [WebMethod]
is there any example on using java method to call rhino-javascript function and return
I create a second thread to call a method that downloads several images using:

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.