Fix possible NPE in creating layers