• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create an object to be null if the class's constructor parameter is int?  RSS feed

 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I am really bad at explaining and I am actually attending school and just started learning java.
I ran into a problem that I don't know how to solve.
I have a class of Date with a constructor with 3 parameters in it. Those 3 parameters are int data type just to enter month, year, day.
I have another class called Author which has a constructor of Date diedDate; as a parameter passing to the Author constructor.
I was asked to call the Date parameter is null, call the default constructor but I thought for the Date parameter I could only enter something like 0,0,0 instead of typing in null, null, null because null is for String data type isn't it?

Thanks in advance people
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null applies any reference variable - not just those of type String. It indicates that the variable is not pointing at any object.

so you could create a new Author(null) which would pass a null date of death.
i.e. you don't create it a date. You just pass null meaning 'no value'




 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Date constructor parameters have to be ints, but the Date object reference itself can be null. So if you have an Author constructor like:



you can always call it like this:



In that case call the default constructor of Author.
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about if my Author constructor is

Because I tried entering which works of course but if I error comes up.
 
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
Edin Tin wrote:How about if my Author constructor is

Then I would say that the constructor is poorly designed.
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just trying the Author b = new Author(null);
and I get this error.


Is it actually something wrong with my code that I should find out?
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Edin Tin wrote:How about if my Author constructor is

Then I would say that the constructor is poorly designed.


Can you give me some hints how this should be done?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well from your description above:
I have another class called Author which has a constructor of Date diedDate; as a parameter passing to the Author constructor

I'm not sure where the Author constructor with three ints came into it. Is that something you added?

NullPointerException is one of the most common errors to encounter.

If a variable is null, it has no value and obviously you can't call any methods on it.
NullPointerException happens when you try and use an object with no value.

The standard approach is to check before you use an object:
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry for the confusion.
I have to make a few constructors under Author class
one is something like

another one is


And now I get what you mean with


but the second constructor which I still don't get how I can fix it so null can be entered.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if( year == 0 && month == 0 && day == 0 )
died = null;

Edit: I would be more inclined to do away with the three argument constructor entirely.
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:if( year == 0 && month == 0 && day == 0 )
died = null;

Edit: I would be more inclined to do away with the three argument constructor entirely.


Ah, thanks, haven't tried it yet but gosh it's so simple and I am still confused by myself.
Sorry didn't, really get what you mean by your edit.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edin Tin wrote:

That constructor is great but the next one is brittle. Best to keep date manipulation out of the class if possible.
Edin Tin wrote:
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: . . . the constructor is poorly designed.
Maybe “poorly designed” is a bit strong, Bear. What that constructor means is the class is saying
I absolutely insist you give me a date of death before I create any objects.
That does not permit authors whose date of death you don't know.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and it also doesn't accommodate authors who are still alive
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Edin Tin, please indent all code when you post it. We have some suggestions about indentation here. Code with { and } all on one line is very difficult to read.

oops I am sorry I will start doing that..by the way, can I still ask something else in here? Even though still about the Author constructor but difference case or I should open another thread? :|
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:… and it also doesn't accommodate authors who are still alive


Quite.
Which sort of implies a constructor that doesn't take a date of death.

Besides, I would have thought an Author would requires slightly more in their constructor than whether they are dead or not...
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should I do to the constructor if I am told that if first last and middle name are null. Do not create the name object at all.
I do not get the part where it says do not create the name object at all.
How can I not create the object?
I have something like this

I also tried

but not sure if this is what do not create the object means.

read the indentation Hopefully this is how it should be
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edin Tin wrote:What should I do to the constructor if I am told that if first last and middle name are null. Do not create the name object at all.
I do not get the part where it says do not create the name object at all.

A constructor must create the object UNLESS it throws an exception.

Edit: Of course that means the calling code must handle the exception in some fashion.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You can skip the code in the if block entirely:

The reason is that pseudonym will already be null (it's the default value for object attributes).
Of course, you could be explicit and do the "pseudonym = null" bit you posted, but it isn't necessary.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edin Tin wrote: . . .
read the indentation . . . Hopefully this is how it should be
Afraid not. I had to break the long lines, and you can see how to do it for future reference. Also don't use long // comments. Use /* comments */ instead which can contain multiple lines.

There is something wrong with your checking the three parameters for nullity. What if two of them are null? Can the Name constructor cope with that?
You are not calling a default constructor but a no‑arguments constructor. You only get a default constructor when you don't write your own constructors. You might call a no‑arguments constructor. But why have you got so many constructors in your Name class? The more constructors you write, the more opportunity you have for things to go wrong.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have the nullity checks in the wrong place. You are using the constructor in the Author object to check about creating a Name object. That is the responsibility of the Name object. I think you should change the constructor to take a Name parameter. Then you can consider whether to pass null or not.Look up single responsibility principle.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think you have the nullity checks in the wrong place. You are using the constructor in the Author object to check about creating a Name object. That is the responsibility of the Name object.


That's not possible, surely?
Whether a Name attribute is created or not cannot be the responsibility of the Name class itself.

Though the idea that the Name construction should be moved out of Author is something well worth considering.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have put it a lot better than I did, Dave Tolls. You explained what I meant to say rather than what I wrote.
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:

You can skip the code in the if block entirely:

The reason is that pseudonym will already be null (it's the default value for object attributes).
Of course, you could be explicit and do the "pseudonym = null" bit you posted, but it isn't necessary.


So what you mean is...inside the if statement, if it's true, it can be nothing in the statement?
I should try this
Still disgusting everything everyone is saying hehe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!