• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for null  RSS feed

 
mika
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a question considering checking for null in java applications. I was wondering if there is a suggested technique or a 'rule of thumb' for when to check for null.

For example, I have a method that returns the selected row of a table (which could be -1 if nothing is selected) then this method is called to get the data for the selected row (which could be -1, resulting to 'null' data) and this method is called to get the data for the selected row and compare it to the items of a vector. So, when i do have a bunch of inter-dependencies, is it best to check for null in every single method, or is this an overkill?
What are the general suggestions in cases like these?
Thanks a lot,
M.
 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always check for null when accessing an object for the first time.
 
basha khan
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first u declare a list(usually vector) for holding selected components.store the pointers of the selected rows to this vector.
then check
selectedstoringvector.isEmpty()
now u can find if there is any selected row present.
then
use iterator(either for loop or Iterator object)
get the value of the pointer by using an API method
like
selectedstoringvector.get(index).getValue();(use original method..this is example).
OK??
basha
 
Murtaza Hussaini
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply break this into two step, one to get the selected record in the table and then get the value of the selected record. Before performing the second step, check the value is not equals to -1, if not, perform the second operation, otherwise, use the JOptionPane.showMessageDialog() to ask the user to select the record.
I've did this, and this works...
Originally posted by M Neil:
Hi all,
I have a question considering checking for null in java applications. I was wondering if there is a suggested technique or a 'rule of thumb' for when to check for null.

For example, I have a method that returns the selected row of a table (which could be -1 if nothing is selected) then this method is called to get the data for the selected row (which could be -1, resulting to 'null' data) and this method is called to get the data for the selected row and compare it to the items of a vector. So, when i do have a bunch of inter-dependencies, is it best to check for null in every single method, or is this an overkill?
What are the general suggestions in cases like these?
Thanks a lot,
M.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!