Oops Null Pointer

Java programming related

Minecraft Modding: A Template Mod

My son loves playing Minecraft mods and wanted to make his own. He had a plethora of grand ideas. I advised to start small to create something achievable. And thus the Sandwich Mod (GitHub link) was created. It adds a piece of bread and a slice of cheese, which can be crafted into a cheese sandwich.

I couldn’t find any quick start mod samples for Minecraft 1.11, but I did find a reasonably straight-forward and detailed video tutorial series by CJMinecraft. From the ideas there I created a simple mod that adds 3 new edible items and a recipe.

To get started you need

  • The Java 1.8 JDK installed
  • A Java IDE (Eclipse or Intellj)
  • git to check out the code
  • A copy of Minecraft

The mod is based on Mincraft Forge, which provides a bunch of hooks to get started. I use Gradle as a build tool as it downloads all the dependencies you need and compiles and packages the mod.

Forge also provides a launcher so you can start Minecraft from your IDE with you mod installed to play-test or debug.

Check out the readme file on GitHub for all the instructions and let me know if you get stuck.


Gradle sourceCompatibility with Eclipse

Recently I had an issue setting the sourceCompatibility for the eclipse plugin. Turns out you can only set this property after the Java plugin has been applied. If you don’t you get a ClassCastException (BigDecimal to JavaVersion).

You can typically set the sourceCompatibility property directly the file after the apply for the Java plugin (but before the eclipse plugin) but in my build I’m using a common build file to apply plugins so I don’t have the flexibility to do that.

Instead I need to use a plugin callback as follows:

plugins.withType(JavaPlugin) {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6

Logging aggregation and filtering for humans and machines

Some thoughts around better approaches logging have been coalescing after reading a few articles. With the filtering power of log aggregation and processing tools in mind like ELK stack, this article challenging the assumption that logs are for only for humans was interesting. I still think human readable logs can be important, so maybe a logging tool can configured to dump both a formatted string and a JSON detailed log into different log targets. Maybe a good log aggregator means there is no need to look elsewhere.

The idea of logs as the central source of data in distributed systems in intriguing. I found Jay Kreps’ article on the subject enlightening.

Finding the right granularity for the format is also an interesting problem. I have been working with a monitoring system that used the Common Base Event format mapped into a database table. The format (more specifically the current implementation) was not ideal for the way we use the system and is a source of performance issues, but is entrenched in the applications of the enterprise. A JSON based version stored in a document database and feed into ELK would be a much better fit.

Eclipse Luna with Google plugin requires JDK 7

I installed the google plugin (4.4) for eclipse luna but no google icons appeared in the toolbar and no options for google were in the preferences.

Looking at the log file in workspace/.metatdata/ .log it reports that JDK 7 is required.

I added -vm {path to jdk7} and all is good.

To use an old GWT version before 2.5 you will need a gwt-codeserver.jar file in the GWT directory to fool the plugin (found the hint here)

Scala and Eclipse Luna – installed but no scala perspective or tooling

I just installed a fresh Eclipse Luna (J2EE) on Linux Mint and then installed the Scale IDE (version 4 for Scala 2.11.1) from the nightly build and after the restart there was no sign of Scala. However the error log was complaining about the Scala components, so they were installed… somewhere…

I restarted Eclipse with -clean and after this (and some tweaks to the Eclipse ini file) it all works fine. I didn’t try the milestone build so I can’t comment if it shares the same issues.


Gerkin evaluation engine

An workmate from a previous job posted this article discussing the release of a Gerkin (Given-When-Then) evaluation engine. Interesting idea. I figure that JBehave has a built in interpreter and maybe this could be leveraged for that project to switch from JBehave to another testing framework at will.

Wiring Restlet JaxRs application with Spring

When wiring a JaxRs application from Spring I got most of the way using this post. My issue was different as  I already had a bean ref context for the rest of my application that I wanted to get my bean from.

I also wanted to respect the scope of the objects in the Spring configuration files and the example seemed to be always creating new instances when calling beanFactory.createBean(jaxRsClas).

The key was to convert the class name into a string and call beanFactory.getBean(classAsName) and then casting the bean it’s (generic) type.

import org.apache.commons.lang.StringUtils;

 * Uses application content and bean ref context in web.xml to wire each new object from Spring.
public class SpringRestletServlet extends ServerServlet {

  private static final Logger logger = Logger.getLogger(SpringRestletServlet.class);

   * Override the createApplication method to inject Spring objects
   * @param context - Restlet context
   * @return - application instance
  public Application createApplication(Context context) {
    JaxRsApplication app = (JaxRsApplication)super.createApplication(context);

    // set up spring to be the object creator
    app.setObjectFactory(new SpringObjectFactory(getWebApplicationContext().getAutowireCapableBeanFactory()));
    return app;

  private static class SpringObjectFactory implements ObjectFactory {
    private final AutowireCapableBeanFactory beanFactory;

    public SpringObjectFactory(AutowireCapableBeanFactory beanFactory) {
      this.beanFactory = beanFactory;

    public T getInstance(Class jaxRsClass) throws InstantiateException {
      logger.debug( "Wiring JAX-RS class [" + jaxRsClass + "] using Spring." );

      String classAsName = StringUtils.uncapitalize(jaxRsClass.getSimpleName());
      return castBeanToType(classAsName);

    private T castBeanToType(String classAsName) {
      return (T)beanFactory.getBean(classAsName);

  private WebApplicationContext getWebApplicationContext() {
    return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

And here is the related parts if the web.xml file that use the bean ref content that is on the classpath to get beans used by the rest of the application:





<!-- Restlet adapter -->

<!-- Catch all requests -->

Is REST over or just re-dressed?

I just read Steve Klabnik’s REST is over article which, while not as dramatic as the title suggests, pitches the admirable idea of renaming “real” REST services to Hypermedia APIs. I like the idea as I have felt and seen reoccurring confusion caused by (mis)understanding what representation state transfer encompasses. Steve’s main point is that the term Hypermedia clearly excludes certain types of REST-ish APIs. It certainly took me a little while to let go of idea of invoking operations and instead focus on changing the state of entities resources.

Steve’s article was also a call to stop arguing and start building. I saw the argument as a symptom of the confusion around focusing on the best way to live up to REST-ful principals. There is clearly a love of getting things done http with (typically) human readable message formats. I’m more inclined to peek at systems via their REST / JSON interfaces to quickly gather information rather than going via a client.

We think we still need a bite sized term for Hypermedia API; I like HAPI. Adding a little developer happiness can’t be argued with, right?

Book review: ExtGWT Rich Internet Application Cookbook

ExtGWT cookbook


The ExtGWT Cookbook from Packt Publishing covers the GXT UI framework version 2 (currently at 2.2.6) as well as a quick overview of key features of GWT and some ideas on building robust UI frameworks. While a good knowledge of GWT itself is recommended, this book could be used a basic primer of some of the required concepts.

I think it’s important to bring up that the version of GXT this book covers is now a major revision behind the latest: GXT 3.0.

A lot has changed in GXT 3.0 and thus some of the detail in the Cookbook is not applicable to projects using this version. Some examples are:

  • The GXT data model (BeanModel) is now replaced by POJOs (using GWT AutoBeans)
  • Wigets can now use UIBinder
  • GXT Event listeners are replaced by GWT handlers
  • The flash charts are replaced by a new JavaScript based chart package

So with that in mind, who is this book useful for? The GXT widgets are relatively unchanged and this book covers the set-up and options of nearly all the widgets in detail and is still helpful from this aspect. The book also covers some useful ideas for building robust UI frameworks; it explores the idea of an “action” that different views can share, each which their own set of supported options. For those who have existing investments in applications using GXT 2 or are just starting projects will find this book a great companion – I wish I had this book during the creation of my current application as it would of helped clarify some of the quirks in GXT and replace the rather lack lustre GXT documentation. It also would of added some depth in the architecture, reducing repetition and linking related components.


The ExtGWT Cookbook’s layout is code heavy – in a good way. Each recipe follows the format of a description of a feature, a “how do do it” code section and a detailed “how it works” line by line breakdown of the code. In this way you are rarely left wondering what a line of code does and any GXT quirks are normally highlighted and explained.

The progression of the examples builds well through each chapter and in the same way the chapters tend to naturally flow on from one another, so the book can be read linearly, but each chapter is self contained enough that you can jump into a recipe with little reference to other sections.

The main documentation that GXT provides is the API and a set of examples with code. While better than nothing, they often are not enough to cover real life scenarios or they leave important options out. The ExtGWT Cookbook has more examples with a focus on building practical applications, and importantly the examples are detailed and backed by a comprehensive description.

I felt the detail and relevance was typically compelling enough to encourage reading through whole chapters. Nearly ever chapter covered a new aspect or feature that I hadn’t previously been aware of, making reading this book worthwhile.


The first three chapters cover the basic elements of panels, windows, tabs and “click-ware” (buttons, toolbars and menus). The section on tabs shows real world experience in the example which toggles the tab strip based on the number of tabs. The last recipe of chatper 3 “Bind a single action to several click-wares” introduces a useful pattern to relate an underlying action or behaviour to different representations that is elaborated and expanded in Chapter 11 in the recipe “A novel UI with MVP, actions and a bus”.

The next four chapters cover GXT specific layouts, trees and grids with enough thoroughness to provide a good head start in using these components.

Chapter 8 covers the GXT templates which allow the modification of views within components with easy access to model data properties. This was a topic that I found GXT’s own documentation was particularly unhelpful, while this chapter succinctly covered with several handy examples.

The built in charts are well covered in Chapter 9, although few would find this flash based package very useful (especially for mobile devices). The last recipe “Drawing on a canvas” hints at alternatives, but for detailed charts look to GXT 3 or an external library.

The chapters “Drag-and-drop”  (chapter 10) and “Theming” (chapter 12) adequately explain these topics and the appendices provide thoughtful coverage of topics that GWT developers need to know.

I found the “Advanced Tips” chapter (11) the most useful and insightful as it covered important architectural concepts for larger applications (MVP, History, working with persistence frameworks). Having gone through the pain of building similar patterns before they were more commonly discussed for GWT projects, I like the way this chapter helps lay the foundations of testable and robust GWT based software. The working server push recipe was also stimulates thoughts on building more interactive applications.


In all the ExtGWT Cookbook is a very detailed and comprehensive overview of GXT 2.x. It inspired me to read the whole thing cover to cover and I only really didn’t learn something from the charts chapter as we have moved to Highcharts (and GXT has moved to a new JavaScript based charting framework in GXT 3).

There are some aspects of GXT’s layout system that were (for me) difficult to deal with that are not covered in this book – in particular deeply nested layouts and using components in resizable windows (say a grid in a portlet in a resizeable window). The GXT documentation avoided complicated layouts and the cookbook’s layouts are typically not too complicated either. This book is aimed at the beginner to intermediate as it lacks some depth in relation to constructing large / complicated GXT based applications, but it still has enough detail to learn some useful tips from.

I’d look forward to a GXT 3 version by the same authors, especially if it covered some more advanced patterns.

Quick guide to transforming XML with XSLT

As a reminder to myself for a quick pattern for transforming XML via XSLT:

Some setup:

public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
public static final String XSL_HEADER = "<xsl:stylesheet version=\"1.0\" " +
public static final String ID_XFORM = "<xsl:template match=\"@*|node()\"><xsl:copy>" +
    "<xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template>";

public static final String TRANS_ATTR = "<xsl:template match=\"ATTR_PATH@ATTR_NAME\">" +
    "<xsl:attribute name=\"ATTR_NAME\">ATTR_VALUE</xsl:attribute></xsl:template>";
public static final String XSL_END = "</xsl:stylesheet>";

And a usage sample transforming an attribute’s value:

String xsl = XFORM_ATTR_XSL.replace("ATTR_PATH", "");
xsl = xsl.replace("ATTR_NAME", "myAttribute");
xsl = xsl.replace("ATTR_VALUE", "newValue");

ByteArrayInputStream xslInputStream = new ByteArrayInputStream(xsl.getBytes("UTF-8"));
FileInputStream xml = new FileInputStream(new File("myXmlFile");
InputStream transformedXml = null;

try {
    transformedXml = transformXml(xml, xslInputStream);
finally {
    if (xml != null) xml.close();

and the transform method:

private InputStream transformXml(InputStream xml, InputStream xsl) throws TransformerConfigurationException, TransformerException {

    TransformerFactory factory = new org.apache.xalan.processor.TransformerFactoryImpl();
    Transformer transformer = factory.newTransformer(new StreamSource(xsl));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new StreamSource(xml), new StreamResult(baos));
    return new ByteArrayInputStream(baos.toByteArray());