DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Competing Consumers With Spring Boot and Hazelcast
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin

Trending

  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Competing Consumers With Spring Boot and Hazelcast
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  1. DZone
  2. Data Engineering
  3. Databases
  4. NetBeans in the Classroom: Time to Say Goodbye to NULL as a Return Value

NetBeans in the Classroom: Time to Say Goodbye to NULL as a Return Value

Ken Fogel user avatar by
Ken Fogel
·
Jun. 04, 15 · Interview
Like (0)
Save
Tweet
Share
2.29K Views

Join the DZone community and get the full member experience.

Join For Free

ken fogel is the program coordinator and chairperson of the computer science technology program at dawson college in montreal, canada. he is also a program consultant to and part-time instructor in the computer institute of concordia university 's school of extended learning. he blogs at omniprogrammer.com and tweets @omniprof .

i loved null . when i taught c and then c++, i explained to my students how pointers had three states. the first state was that of a valid address to an object in memory. the second was an invalid address or garbage address that occurs when a local pointer is created or a pointer goes out of scope. unfortunately there was no way to tell a valid from an invalid address. then there was the null state. this is a pointer in waiting. not yet pointing at a valid object and easily recognized as such. i required my students to initialize local pointer references to null or to assign them a null after deleting them. every time a function returned a pointer the very next line of code after calling the function had to read “ if (ptr == null) ”.

when i moved on to teaching java, the pointer was gone. any student who sees their first null pointer error message may disagree but i leave that to another debate. in place of the pointer is the reference. like the c/c++ pointer, it too has three states. the major java enhancement is that the compiler will recognize that you are trying to use the invalid address state on the left hand side of an expression and will declare an error.

so that leaves us with a valid address or null. therefore, just like with c/c++, a programmer must test if a reference is null or not. this must never be ignored. it just takes one java method that attempts to access a value in, as an example, an arraylist whose reference is null to bring down a program.

to see the problem, let's look at this method:

public arraylist findall() throws sqlexception {
        arraylist rows = null;
        string selectquery = "select id, commonname, latin, ph, kh, temp, fishsize, speciesorigin, tanksize, stocking, diet from fish";
        try (connection connection = drivermanager.getconnection(url, user,
                password);
                preparedstatement pstatement = connection
                .preparestatement(selectquery);
                resultset resultset = pstatement.executequery()) {
            if (resultset.next()) {
                // only create the arraylist if there is something to put in it
                rows = new arraylist<>();
                do {
                    fishdata fishdata = new fishdata();
                    fishdata.setcommonname(resultset.getstring("commonname"));
                    fishdata.setdiet(resultset.getstring("diet"));
                    fishdata.setkh(resultset.getstring("kh"));
                    fishdata.setlatin(resultset.getstring("latin"));
                    fishdata.setph(resultset.getstring("ph"));
                    fishdata.setfishsize(resultset.getstring("fishsize"));
                    fishdata.setspeciesorigin(resultset.getstring("speciesorigin"));
                    fishdata.setstocking(resultset.getstring("stocking"));
                    fishdata.settanksize(resultset.getstring("tanksize"));
                    fishdata.settemp(resultset.getstring("temp"));
                    fishdata.setid(resultset.getlong("id"));
                    rows.add(fishdata);
                } while (resultset.next());
            }
        }
        return rows;
    }

the way in which this method is coded, the arraylist is only created if there were any records returned from the database query otherwise a null is returned. here is a method that converts the arraylist of records into a string by calling upon findall() :

    public string retrievefish() {
        stringbuilder sb = new stringbuilder();
        fishdao fishdao = new fishdaoimpl();
        arraylist data;
        try {
            data = fishdao.findall();
            // must check for null
            if (data != null) {
                data.stream().foreach((fd) -> {
                    sb.append(fd.tostring()).append("\n");
                });
            }
        } catch (sqlexception e) {
            log.error("error retrieving records: ", e.getcause());
        }
        return sb.tostring();
    }

there is nothing wrong with this code. what can go wrong is the programmer, especially a student programmer. the error is forgetting the if (data != null). here is a classic student error:

        try {
            // oops, forgot to check for null
            data = fishdao.findall();
            data.stream().foreach((fd) -> {
                sb.append(fd.tostring()).append("\n");
            });
        } catch (sqlexception e) {
            log.error("error retrieving records: ", e.getcause());
        }

if only there was some way to make a programmer aware of the possibility of a null reference? there is and it appeared in java 8. it is the optional type. its primary purpose is to eliminate returning null from methods. by changing the return type to optional we can change the findall() method’s first and last lines:

public optional> findall() throws sqlexception {
	. . . 
	// the original code does not change
	. . .
        return optional.ofnullable(rows);
}

if the reference being returned is placed in an optional.ofnullable() then it may or may not be null. the caller of this method must deal with the possibility of a null .

here is how it is dealt with:

    public optional retrievefish() {
        stringbuilder sb = new stringbuilder();
        fishdao fishdao = new fishdaoimpl();
        //arraylist data;
        try {
            // ifpresent is required so a check for null can never be forgotten 
            fishdao.findall().ifpresent(data -> data.stream().foreach((fd) -> {
                sb.append(fd.tostring()).append("\n");
            })
            );
        } catch (sqlexception e) {
            log.error("error retrieving records: ", e.getcause());
        }
        return optional.of(sb.tostring());
    }

notice that to access the arraylist in the optional the method ifpresent() is used. if this is true then the lambda is invoked to copy the arraylist into the stringbuilder object.

another feature of optional is in play in this method. the return type is optional<string> and the last statement reads return optional.of(sb.tostring()) . the of() member method of optional can only accept a non-null value so if the stringbuilder object is somehow null there will be an exception.

the next time you are writing a method that can return a null reference please don’t. return an optional instead.

Pointer (computer programming) Database Classroom (Apple) NetBeans

Opinions expressed by DZone contributors are their own.

Trending

  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Competing Consumers With Spring Boot and Hazelcast
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: