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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:24:13+00:00 2026-05-10T15:24:13+00:00

I have a constructor like as follows: public Agent(){ this.name = John; this.id =

  • 0

I have a constructor like as follows:

public Agent(){      this.name = 'John';     this.id = 9;     this.setTopWorldAgent(this, 'Top_World_Agent', true);  } 

I’m getting a null pointer exception here in the method call. It appears to be because I’m using ‘this’ as an argument in the setTopWorldAgent method. By removing this method call everything appears fine. Why does this happen? Has anyone else experienced this?

  • 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. 2026-05-10T15:24:14+00:00Added an answer on May 10, 2026 at 3:24 pm

    You can pass this to methods, but setTopWorldAgent() cannot be abstract. You can’t make a virtual call in the constructor.

    In the constructor of an object, you can call methods defined in that object or base classes, but you cannot expect to call something that will be provided by a derived class, because parts of the derived class are not constructed yet. I would have expected some kind of compiler error if setTopWorldAgent() was abstract.

    In Java, you can get surprising behavior with the contructor and derived classes — here is an example

    http://en.wikipedia.org/wiki/Virtual_functions#Java_3

    If you are used to C# or C++, you might think it’s safe to call virtual functions and not be calling overridden ones. In Java, the virtual call is made even though the derived class is not fully constructed.

    If this isn’t what’s happening, then presumably, all of the parts of this that setTopWorldAgent() needs are initialized — if not, it’s probably one of the members of this that needs to be initialized.

    Edit: thought this was C#

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

Sidebar

Related Questions

Ought I to unit test constructors? Say I have a constructor like this: IMapinfoWrapper
In C#, if you have multiple constructors, you can do something like this: public
I have an enum construct like this: public enum EnumDisplayStatus { None = 1,
Is there a way to have some kind of default constructor (like a C++
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two constructor : function clsUsagerEmailUserName($nickName, $email) { $this->nickName = $nickName; $this->email =
Let's say I have this class: public abstract class CustomerCollectionBase : Collection<Customer>{} One of
I have a polygon soup of triangles that I would like to construct a
Can an abstract class have a constructor? If so, how can it be used
For my exception class i have a constructor that has multi arguments (...) which

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.