• Post Reply Bookmark Topic Watch Topic
  • New Topic

Are only assignment statements allowed in a class' constructor?  RSS feed

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings all,

Given some arrays of strings



I'd like my constructor to accept an array of strings and implicitly convert any non-blank character to logical true


but the program crashs at runtime due to the line specified ( verified by commenting out the offending line)

Any ideas on how to achieve this?


Still-learning Steve

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a chance that inn_str[2] is null?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Is there a chance that inn_str[2] is null?

Or undefined? What the heck is it?

In answer to the question in your thread title, of course not. There are no syntactically correct Java statements prohibited in constructors that I can think of, but I'm a beginner.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, no chance. inn_arry[2] will contain either a "Y", "X", or "" (blank) values

Thanks for your quick reply!


Still-learning Steve
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:
Bear Bibeault wrote:Is there a chance that inn_str[2] is null?

Or undefined? What the heck is it?

I assumed it was just sloppy posting and that it's the method's parameter. He could never run the class otherwise as it would not compile...
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ach, my bad, yes sloppy posting, should read
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
I assumed it was just sloppy posting and that it's the method's parameter. He could never run the class otherwise as it would not compile...


Unless, of course, the OP is using an IDE that allows you to run code that doesn't compile -- in which case you get an error at runtime saying that it doesn't compile.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, what do you mean by "crashes program at runtime"? It would help if you tell us what happens.

Henry
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why yes I am executing it from within Eclipse. Eclipse doesn't flag any statements in the constructor.



Where the constructor is called from within


which in turn is called by


But the line I marked within the constructor HAS to be the problem since after commenting it out the program executes just fine.

Thanks for your help!

Still-learning Steve
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Rogers wrote:

I'm betting that if you scroll down a bit in the stack trace, there's at least one more section introduced by the phrase "Caused by:". It would be most helpful if you would include any and all of these parts of the stack trace, as well.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking through the stack trace more closely I saw where a NullPointerException was occuring on this line within utils.java


where this function now gets called by the constructor


So a previous poster was correct in that I do have to check for nulls. When I comment out the 2nd and 3rd if-statementsin the utility function the program executes to completion. Hm.


Still-learning Steve
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm curious; does this actually work:

Class stringArrayClass = Class.forName("[Ljava.lang.String;")


I thought Class.forName needed a fully-qualified class name, with no extraneous characters.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should - that is the fully qualified name. The fully qualified name of an array class is [L followed by the fully qualified component type followed by a ;. Any additional array dimensions adds a single [ at the start. See for yourself:
For arrays the fully qualified name happens to match the JNI name. (But that's probably by design.)

But I agree with Mike - post the rest of the stack trace, especially the cause part. That's the real exception; reflection merely wraps that into an InvocationTargetException.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this question is too difficult for "beginning". Moving thread.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are calling an instance method from the constructor it should either have private access or the final modifier.

I think you would do better to use shortcut operators for the null test. You can then get the whole thing into one line. Note there are some useful methods in the String class which would help you.You need the () because the bang operator ! has a higher precedence than == and ||.

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you apply DeMorgan's law and get
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!