-3

I have defined two constructors in my code

public SAPRoleImpl()
{
    dateParser=new SimpleDateFormat(MIDDAY_DATE_FORMAT);
    dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar c=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    c.set(Calendar.HOUR_OF_DAY,12);
    c.set(Calendar.MINUTE,0);
    c.set(Calendar.SECOND,0);
    c.set(Calendar.MILLISECOND,0);
    setStartDate(c.getTime());
}

public SAPRoleImpl(String formattedRole)
{
    this();
    ...
}

When I execute the below code:

public static void main(String[] args) {
    SAPRoleImpl sapRole = new SAPRoleImpl("abc|abcdesc||");
    System.out.println(sapRole);
}

It gives this output

:abc|20170127||

This is as expected. But when I want an output of only

abc|||

i.e no start date is to be initialized, I tried this code:

public SAPRoleImpl()
{
}

public SAPRoleImpl(String formattedRole)
{
    this();
    ...
}

This led to a NullPointerException. Probably, It seems that the startdate is null but I am not able to understand the reason behind the same.

Can any one please help me to understand?

BeginnersSake
  • 650
  • 2
  • 18
  • 30
user3363047
  • 41
  • 2
  • 11
  • Well presumably the problem is in your `toString()` override, which you haven't shown us. Please provide a [mcve] - and make sure your post is formatted as clearly as possible... I've reformatted the initial post, but when you edit it to provide the complete example, use the preview to make sure it's still as readable as it can be. – Jon Skeet Jan 27 '17 at 08:21
  • You set the start date via setter in your first constructor version: `setStartDate(c.getTime());` Then you removed that line. - Why do you think the startDate could not be null? – Timothy Truckle Jan 27 '17 at 08:24

1 Answers1

0

Obviously, you have a field startDate that only gets assigned a value when you call the method setStartDate().

When you omit calling that method within your constructor, then that field stays with null.

And most likely, you are then calling some method like toString() on that null field.

Btw: you get your constructor chaining wrong.

The normal way is that you you call a ctor that takes more arguments, like:

public public SAPRoleImpl() {
  this(SOME_DEFAULT_FORMAT_STRING);
}

public SAPRoleImpl(String format) {
  dateParser=new SimpleDateFormat(format);
  ...
  setStartDate(c.getTime());
}

In other words: you absolutely want to put your "real" init code into exactly one constructor; or maybe one init method if there is no other way but doing slightly different things within multiple constructors.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • okay, due to toString() I face an issue here's my to String () part toString(){buff.append(ROLES_DELIM); Date endDate = getEndDate(); if(endDate!=null)buff.append(dateFormatter.format(endDate)); buff.append(ROLES_DELIM); return buff.toString();} but what if, I write the code of default constructor in my parameterized constructor after this(), it still throws a null pointer exception – user3363047 Jan 27 '17 at 08:41