How to let a derived class in a parameterized constructor chain access fields of the base class that are initialized using the derived constructorInitialize class fields in constructor or at declaration?Accessing constructor of an anonymous classOrder of constructors for a C# class: parameterized, default, and static?C# constructor chaining? (How to do it?)While constructing the default constructor can not handle exception : type Exception thrown by implicit super constructorchaining constructors in Java without throwing exceptions from the default constructorWhy Java StringReader throws IOException?Chain Constructor from parents classAll reflection methods accessing constructor of class generated through ASM throw NoClassDefFoundError if class references primitive type
Is there a command-line tool for converting html files to pdf?
Plotting Autoregressive Functions / Linear Difference Equations
How do I handle a DM that plays favorites with certain players?
On the consistency of different well-polished astronomy software
Why is Heisenberg shown dead in Negro y Azul?
Write The Shortest Program to Calculate Height of a Binary Tree
Why is the Vasa Museum in Stockholm so Popular?
Is there any difference between "result in" and "end up with"?
How to win against ants
When using the Proficiency Dice optional rule, how should they be used in determining a character's Spell Save DC?
How to check a file was encrypted (really & correctly)
Is it uncompelling to continue the story with lower stakes?
What is the difference between "un plan" and "une carte" (in the context of map)?
Is there a general term for the items in a directory?
How does Geralt transport his swords?
How do I know when and if a character requires a backstory?
The warlock of firetop mountain, what's the deal with reference 192?
how to change dot to underline in multiple file-names?
Why does capacitance not depend on the material of the plates?
Can a Sikh enter a buddhist temple with a turban?
The Game of the Century - why didn't Byrne take the rook after he forked Fischer?
What printing process is this?
How easy is it to get a gun illegally in the United States?
How to design an effective polearm-bow hybrid?
How to let a derived class in a parameterized constructor chain access fields of the base class that are initialized using the derived constructor
Initialize class fields in constructor or at declaration?Accessing constructor of an anonymous classOrder of constructors for a C# class: parameterized, default, and static?C# constructor chaining? (How to do it?)While constructing the default constructor can not handle exception : type Exception thrown by implicit super constructorchaining constructors in Java without throwing exceptions from the default constructorWhy Java StringReader throws IOException?Chain Constructor from parents classAll reflection methods accessing constructor of class generated through ASM throw NoClassDefFoundError if class references primitive type
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a class Feedforward with a parameterized constructor of a configuration file:
public Feedforward(String cfg) throws Exception {
super(cfg);
String tempstr = "";
int currNeuronNum = 0;
int currEdgeNum = 0;
int currLayerID = 0;
int count = 0;
if (!(type).equals("feedforward"))
throw new Exception("cfgError: specify proper type")
//more code
where the super(cfg) calls the constructor of the Network class, where I handle file parsing and storage of universal fields:
protected Network(String cfgPath) throws IOException, Exception
String type;
String activationFunction;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
and when I run a test, it throws a NullPointerException. The type variable in Feedforward is not assigned with the value stored in the file at cfgPath/cfg, hence the exception. Why doesn't constructor chaining do this, and how can I do things differently?
java object constructor constructor-chaining
add a comment |
I have a class Feedforward with a parameterized constructor of a configuration file:
public Feedforward(String cfg) throws Exception {
super(cfg);
String tempstr = "";
int currNeuronNum = 0;
int currEdgeNum = 0;
int currLayerID = 0;
int count = 0;
if (!(type).equals("feedforward"))
throw new Exception("cfgError: specify proper type")
//more code
where the super(cfg) calls the constructor of the Network class, where I handle file parsing and storage of universal fields:
protected Network(String cfgPath) throws IOException, Exception
String type;
String activationFunction;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
and when I run a test, it throws a NullPointerException. The type variable in Feedforward is not assigned with the value stored in the file at cfgPath/cfg, hence the exception. Why doesn't constructor chaining do this, and how can I do things differently?
java object constructor constructor-chaining
add a comment |
I have a class Feedforward with a parameterized constructor of a configuration file:
public Feedforward(String cfg) throws Exception {
super(cfg);
String tempstr = "";
int currNeuronNum = 0;
int currEdgeNum = 0;
int currLayerID = 0;
int count = 0;
if (!(type).equals("feedforward"))
throw new Exception("cfgError: specify proper type")
//more code
where the super(cfg) calls the constructor of the Network class, where I handle file parsing and storage of universal fields:
protected Network(String cfgPath) throws IOException, Exception
String type;
String activationFunction;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
and when I run a test, it throws a NullPointerException. The type variable in Feedforward is not assigned with the value stored in the file at cfgPath/cfg, hence the exception. Why doesn't constructor chaining do this, and how can I do things differently?
java object constructor constructor-chaining
I have a class Feedforward with a parameterized constructor of a configuration file:
public Feedforward(String cfg) throws Exception {
super(cfg);
String tempstr = "";
int currNeuronNum = 0;
int currEdgeNum = 0;
int currLayerID = 0;
int count = 0;
if (!(type).equals("feedforward"))
throw new Exception("cfgError: specify proper type")
//more code
where the super(cfg) calls the constructor of the Network class, where I handle file parsing and storage of universal fields:
protected Network(String cfgPath) throws IOException, Exception
String type;
String activationFunction;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
and when I run a test, it throws a NullPointerException. The type variable in Feedforward is not assigned with the value stored in the file at cfgPath/cfg, hence the exception. Why doesn't constructor chaining do this, and how can I do things differently?
java object constructor constructor-chaining
java object constructor constructor-chaining
asked Mar 27 at 2:52
Duncan WDuncan W
113 bronze badges
113 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Because type is local variable of a method (in this case constructor), though Network is a super class but we cannot access a local variable of any method out side it.
you can make String type=""; as a variable out side constructor , then just assign the value in side Network constructor.
and you can use it in Feedforward class.
public class Network
String type="";
protected Network(String cfgPath) throws IOException, Exception
type=cfgPath;
String activationFunction=cfgPath;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369079%2fhow-to-let-a-derived-class-in-a-parameterized-constructor-chain-access-fields-of%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because type is local variable of a method (in this case constructor), though Network is a super class but we cannot access a local variable of any method out side it.
you can make String type=""; as a variable out side constructor , then just assign the value in side Network constructor.
and you can use it in Feedforward class.
public class Network
String type="";
protected Network(String cfgPath) throws IOException, Exception
type=cfgPath;
String activationFunction=cfgPath;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
add a comment |
Because type is local variable of a method (in this case constructor), though Network is a super class but we cannot access a local variable of any method out side it.
you can make String type=""; as a variable out side constructor , then just assign the value in side Network constructor.
and you can use it in Feedforward class.
public class Network
String type="";
protected Network(String cfgPath) throws IOException, Exception
type=cfgPath;
String activationFunction=cfgPath;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
add a comment |
Because type is local variable of a method (in this case constructor), though Network is a super class but we cannot access a local variable of any method out side it.
you can make String type=""; as a variable out side constructor , then just assign the value in side Network constructor.
and you can use it in Feedforward class.
public class Network
String type="";
protected Network(String cfgPath) throws IOException, Exception
type=cfgPath;
String activationFunction=cfgPath;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
Because type is local variable of a method (in this case constructor), though Network is a super class but we cannot access a local variable of any method out side it.
you can make String type=""; as a variable out side constructor , then just assign the value in side Network constructor.
and you can use it in Feedforward class.
public class Network
String type="";
protected Network(String cfgPath) throws IOException, Exception
type=cfgPath;
String activationFunction=cfgPath;
double bias;
/*file reading stuff; checked with print statements and during
the creation of a Feedforward class, successfully prints
"feedforward" after reading type from file
*/
answered Mar 27 at 3:33
purvaBpurvaB
494 bronze badges
494 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369079%2fhow-to-let-a-derived-class-in-a-parameterized-constructor-chain-access-fields-of%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown