9 Replies - 517 Views - Last Post: 11 June 2016 - 05:02 AM Rate Topic: -----

#1 char1040  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-September 15

Error catching in bidirectional association - best practice?

Posted 10 June 2016 - 05:41 PM

Hi - I had a query regarding what it would be best to do in a situation where an error could be thrown up in this situation. There are two classes involved in this situation: a Block, and a Grid, which contains many Blocks. A Block can also have an Owner (i.e. a Grid object passed to it after creation). However, it's not mandatory for a Block to have an Owner, and if it doesn't a NullPointerException gets thrown. I've tried to catch it using the following:

public Grid getOwner() {
		try {
			@SuppressWarnings("unused")
			int test = this.owner.getGridID();
			return this.owner;
		} catch (NullPointerException e) {
			System.err.println("Sorry, this can't be called as the block has no owner.");
			return null;
		}
	}


However, is this the right thing to do? It stops the usual NullPointerException details being output to the console, and instead adds a more "friendly" message and returns a null object. In the event that someone else was using the Block class for something (not sure they would, but that's by the by), would it be better to just have the NullPointerException thrown, as would be the default behaviour, rather than this message? I'm aware that it's not that important, considering it's just a personal project, but I wanted to know in case I was making something a bit more significant.

Also, is there a way to call the getGridID() method (which returns an integer) without having to use the @SuppressWarnings statement? It feels like a bit of a fudge, but it seems to work.

Thanks in advance for your help :)

Is This A Good Question/Topic? 0
  • +

Replies To: Error catching in bidirectional association - best practice?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,600
  • Joined: 20-September 08

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 01:20 AM

Quote

However, is this the right thing to do?
In a word - no. NPE should not be caught (normally) as it's usually a sign of programming errors. What you should do instead is test for null and proceed accordingly
@SuppressWarnings is probably OK - or just ignore them
Was This Post Helpful? 1
  • +
  • -

#3 char1040  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-September 15

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 02:26 AM

View Postg00se, on 11 June 2016 - 09:20 AM, said:

Quote

However, is this the right thing to do?
In a word - no. NPE should not be caught (normally) as it's usually a sign of programming errors. What you should do instead is test for null and proceed accordingly
@SuppressWarnings is probably OK - or just ignore them

Thanks for your response. When you say "test for null", do you mean in terms of unit testing, or would I just get rid of the try-catch thing altogether and leave...

public Grid getOwner() {
	@SuppressWarnings("unused")
	int test = this.owner.getGridID();
	return this.owner;
}


...as this would give the NullPointerException at the required time, rather than when the user tries to get a non-existent grid ID from the non-existent owner?

Or did you mean neither of those things? Sorry if I'm being a bit dim.
Was This Post Helpful? 0
  • +
  • -

#4 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 04:30 AM

What is the getOwner method supposed to return? What if the value of owner is null? What should it do then?
Was This Post Helpful? 0
  • +
  • -

#5 char1040  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-September 15

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 04:35 AM

View PostNormR, on 11 June 2016 - 12:30 PM, said:

What is the getOwner method supposed to return? What if the value of owner is null? What should it do then?

It's supposed to return the Grid that the Block is part of. Assuming there's no owner, it (at present) returns null, but I suppose it could return a blank grid. If it did that, though, how would the person trying to get the owner know that it wasn't actually part of a Grid? (I suppose the grid being blank might be a clue, but they might not find out that it's blank until later.)
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 04:42 AM

Quote

It's supposed to return the Grid that the Block is part of.

If the Grid is null, then should it return null???
Then the method doesn't need to do any tests and can simply return owner.
Was This Post Helpful? 0
  • +
  • -

#7 char1040  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-September 15

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 04:55 AM

View PostNormR, on 11 June 2016 - 12:42 PM, said:

Quote

It's supposed to return the Grid that the Block is part of.

If the Grid is null, then should it return null???
Then the method doesn't need to do any tests and can simply return owner.

Thanks - I guess that makes sense. I think, originally, I wanted some sort of way to gently inform the user (or coder, rather) that there was no Grid associated with the Block, but if that's not the done thing, I suppose there's no harm in returning a null grid and letting a NullPointerException be thrown when the person tries to find out what the associated Grid's ID is.
Was This Post Helpful? 0
  • +
  • -

#8 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 05:00 AM

Quote

inform the user (or coder, rather) that there was no Grid associated with the Block,

If the getOwner method is defined as returning null when there is no Grid, then the caller will be responsible for testing for a null value before using it.
Was This Post Helpful? 0
  • +
  • -

#9 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 05:00 AM

Quote

inform the user (or coder, rather) that there was no Grid associated with the Block,

If the getOwner method is defined as returning null when there is no Grid, then the caller will be responsible for testing for a null value before using it.
Was This Post Helpful? 1
  • +
  • -

#10 char1040  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-September 15

Re: Error catching in bidirectional association - best practice?

Posted 11 June 2016 - 05:02 AM

View PostNormR, on 11 June 2016 - 01:00 PM, said:

Quote

inform the user (or coder, rather) that there was no Grid associated with the Block,

If the getOwner method is defined as returning null when there is no Grid, then the caller will be responsible for testing for a null value before using it.

That clears that up! Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1