27 December 2010

[Newbie] NullPointerException when calling findViewById

Well this one really took me quiet some time to solve it. When ever I called findViewById whitin my Activity I got a bloody NullPointerException :(

Here is the code that causes the NullPointerException.

public class DisplayActivity extends Activity {

    private final String TAG = "DisplayActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView target = (TextView) findViewById(R.id.targetDisplay);
        target.setText(text); // <-- That causes the NullPointerException

        setContentView(R.layout.display);        
    }
} 
 
The solution of course is very easy and was mainly caused by the lack of my Android knowledge. Android of course only can access elements within a layout after it the layout is explicitly set. Therefore I had to set the layout using setContentView(..) and after that the Activity can access the TextView targetDisplay. So the fix was unbelievably easy as you can see in the listing below.

public class DisplayActivity extends Activity {

    private final String TAG = "DisplayActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display); // do this first

        TextView target = (TextView) findViewById(R.id.targetDisplay);
        target.setText(text); // <--No NullPointerException anymore                
    }
}

13 October 2010

Counting lines of code using Groovy

The following Groovy scripts easily counts the lines of files. In this example comparing the lines of code between an multi module Maven 2 project and the Gradle oposite.

def root = new File('/home/username/dirOne')
def pomCount = 0
def gradleCount = 0

private int getLOC(File f) {
    int i = 0  
    f.eachLine { i++ }
    return i
}

root.eachFileRecurse {

    if (it.isFile()) {
        if (it.name == 'pom.xml') {
             println "Reading ${it.absolutePath}"
             pomCount += getLOC(it)
        }
        else if (it.name.matches('.*\\.gradle')) {
             println "Reading ${it.absolutePath}"
             gradleCount += getLOC(it)
        }
        else {
          //println "Ignoring ${it.name} (${it.absolutePath})"
        }
    }
}
println "LOC of all pom.xml ${pomCount}"
println "LOC of all gradle.build ${gradleCount}"

29 September 2010

Apply changes in .bashrc immediately

I have just learned that if you change something in your .bashrc file (e.g. add a new environment variable) and you want to apply it immediately to your shell environment just type.

source ~/.bashrc

Et voilà. Your current bash terminal has implemented your changes done in .bashrc

3 March 2010

Java Calendar API - A misleading piece of software


Today I found a bug in a software module which was caused by the misleading Calendar API of Java regarding the method called before. Of course if the author of the code would have considered the API documentation where the behavior is well described this bug could have been prevented. But the method seems absolutely simple that I would not have read the API documentation neither.


Here comes the code in which the bug was located

public boolean isPasswordExpired(Date lastPwdChange,
  boolean isPwdChangeNeeded) {
 boolean pwdExpired = false;
 if (getMaxValidityTime() > 0 && lastPwdChange != null) {
  Calendar calendar = Calendar.getInstance();
  calendar.setTime(lastPwdChange);
  calendar.add(Calendar.DAY_OF_YEAR, getMaxValidityTime());
  log.debug("Password must not be older than " + getMaxValidityTime() + " days");
  pwdExpired = calendar.before(new Date()); // <-- here comes the BUG!
 }
 return pwdExpired || isPwdChangeNeeded;
}

isPasswordExpired checks if a password has expired. The bug is located at calendar.before(new Date()) where the before method of a java.util.Calendar gets called. Since the parameter of before is a java.lang.Object actually everything can be used to call before. In this example the parameter is a java.util.Date. This looks quiet straight forward, does it? But its oh so wrong!


The API documentation describes it as follows:
if and only if when is a Calendar instance. Otherwise, the method returns false.
Since this feature 'A password can expire' has not been used until now and the code has not been covered by unit tests :( this malfunction has not been detected yet.



22 February 2010

My Android must have apps selection

Recently I have bought the new Google Nexus One (US Version). Here is a small must have collection of Android apps which I find quiet useful so far.
  • ASTRO File Manager - A very nice tool to manage your file on your SD card.
  • NetSentry - NetSentry keeps track of how much data is transferred over each network interface available to your Android system.
  • Daily Dilbert - Well there is nothing more to say :)
  • Google Maps - Nice new edition for Nexus One introducing a navigation system
  • TweetCast - A nice Twitter client
All apps can be found at the Android Market.