• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about "null" intepretations

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi people,

sorry i have a question which might be stupid but i want to clarify something. For my DB interface i have the following statement:

// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int[] find(String[] criteria);

does a null value in can be null or "" or it only refers to null?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null is null in my opinion. Empty is something completely different.
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Benjamin,
Refer to Roberto Buddy's test. He uses String[] criteria = {"Palace", "Smallvile", null, null, null, null, null} to test his find algorithm. I believe our assignment should have null pointer exception handling in this case.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Benjamin

If you read the comments carefully you should have realized that "null" indicates a wildcard.
 
Benjamin Ng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply everyone. Now i doing junit testing on the find method for the Data.java and i need some clarification. If my criteria array is all empty value which means is {"","","","","",""}, what should the expected result for the find method? 0 records or all valid record in the database?
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0 records.
Because all records have their names and cities. Their names and cities are not "".
 
Benjamin Ng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:0 records.
Because all records have their names and cities. Their names and cities are not "".



how about i pass in a String array of {null,null,"","","",""}, is the expected result still 0?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my find-method passing an array containing all null-values will result in all valid records being returned. If an array is passed with non-null values the null values are simply ignored.
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Ng wrote:

Himai Minh wrote:0 records.
Because all records have their names and cities. Their names and cities are not "".



how about i pass in a String array of {null,null,"","","",""}, is the expected result still 0?



The expected result is all records. K Tsang buddy said null indicates a wildcard. The first "null" indicates all records with any name. The second null indicates all records with any city.

If you have a string array of {null, "New York", null, null, null, null}. The expected result is any name in New York city.
 
Benjamin Ng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:

Benjamin Ng wrote:

Himai Minh wrote:0 records.
Because all records have their names and cities. Their names and cities are not "".



how about i pass in a String array of {null,null,"","","",""}, is the expected result still 0?



The expected result is all records. K Tsang buddy said null indicates a wildcard. The first "null" indicates all records with any name. The second null indicates all records with any city.

If you have a string array of {null, "New York", null, null, null, null}. The expected result is any name in New York city.



Sorry i am getting confused. Let me clarify something, based on my understanding, if i send a String array of {null,null,"","","",""}, shouldn't the expected result is 0 because even though both name and location are using null, but the rest of the search is not using wildcard so the check should be something like <is the rate begins with "">, <is the size begins with "">, <is the owner begins with ""> which for these cases they are not begin with "" so the final results is return 0 records?
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Benjamin buddy,
Agree. This is confusing.
You may want to assume "" is a value. For example, name is "".
If the name criteria is "", search for names "".
If the name criteria is null, return all records with any name.

Agree with this ?
 
Benjamin Ng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:Hi Benjamin buddy,
Agree. This is confusing.
You may want to assume "" is a value. For example, name is "".
If the name criteria is "", search for names "".
If the name criteria is null, return all records with any name.

Agree with this ?



Yes i agreed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic