in Education by
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? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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#

Related questions

0 votes
    I have a constructor like as follows: public Agent(){ this.name = "John"; this.id = 9; this. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    Which of this keyword can be used in a subclass to call the constructor of superclass? (a) super ( ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    I was just discussing with some colleagues about Java constructors, design-patterns and good way to initialize ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    What should be the execution order, if a class has a method, static block, instance block, and ... stackoverflow.com 🔗Source: Java Interview Questions and Answers...
asked Dec 19, 2020 in Technology by Editorial Staff
0 votes
    Which of this method is given parameter via command line arguments? (a) main() (b) recursive() ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    I'm attempting to call my sql stored procedure which takes RaceDate as an input and returns Location as an ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm attempting to call my sql stored procedure which takes RaceDate as an input and returns Location as an ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    All compilers I could get my hands on agree that this is fine: template auto foo(Check, T...) - ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I'm still trying to understand this piece of code that I found in a project I'm working on where ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I'm still trying to understand this piece of code that I found in a project I'm working on where ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    What is the difference between a constructor and a method?...
asked Feb 9, 2023 in Technology by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
...