Every Java programmer has faced the dreaded NullPointerException at some point in their life. Sometimes it’s your fault, sometimes it’s a pesky race condition. Regardless, it’s a head ache and generally leads to a ton of if (myVariable != null) { } conditions all over your code. However, the latest craze Kotlin can help with that too. Kotlin introduced null safety into its type system, with the potential of removing all NPEs.

This post is both going to review the official docs linked above, as well as provide some common tips and tricks to work with the nullability - something that is new in this language for many Java programmers.

Continue Reading ...

This year at Google I/O, the Android team announced Android Architecture Components a combination of new, helpful libraries for Android development. One that particularly interested me was the Room Persistance Library, which is an abstraction layer of SQLite designed to make database access and creation a lot easier. Right off the bat it reminded me of Realm, which I learned about at Droidcon NYC and really admired, so I decided to dive in and build a todo list using Room & RxJava.

Continue Reading ...

If you’ve been following along with parts 1-3, you now have an (almost) working todo list application. The only thing we are missing is persisting the data. Running your Android app now and rotating your screen will show you that the items you add won’t persist, and disappear anytime an activity is killed and recreated. This post will show you how to write the list to a text file and read from it.

Continue Reading ...

Following parts 1 and 2 you should have a working Android app in Kotlin that displays a list of Tasks to be completed and lets you mark them as complete. This segment is going to show you how to implement support for adding new items.

Continue Reading ...

Following up on part 2 which demonstrates how to create your Android app and configure Kotlin, we’ll begin building the heart and soul of a Todo List application - the list!

Data Model

Let’s begin by defining our model. We’re going to create a simple class that has two fields, one for description and one for whether or not it’s completed. Here is how this class will look in Kotlin:

	data class Task(var description: String, var completed: Boolean = false) : Serializable

You may not believe it, but that’s all we need. Let’s talk about what’s happening here:

  • A data class is a special class in Kotlin that provides you with default behaviors for all your Object methods like toString() hashCode() equals() and copy(). Read more here.
  • Kotlin allows for default constructors to be defined right with the class name.
  • Kotlin allows for default parameters. So in this case, we have a constructor that can be used as Task("Description") and it will default to incomplete, or we can call it with Task("Description", true) to set the initial value of the completed boolean.
  • We’ve had our class implement Serializable. In this simple app, we’re just going to save the data to a text file instead of over complicating it with SQLite.
Continue Reading ...