diff --git a/Mage.Tests/src/test/java/org/mage/test/cards/abilities/keywords/MeldTest.java b/Mage.Tests/src/test/java/org/mage/test/cards/abilities/keywords/MeldTest.java
index d6a617ec4d0..8a1c5be48a2 100644
--- a/Mage.Tests/src/test/java/org/mage/test/cards/abilities/keywords/MeldTest.java
+++ b/Mage.Tests/src/test/java/org/mage/test/cards/abilities/keywords/MeldTest.java
@@ -163,4 +163,47 @@ public class MeldTest extends CardTestPlayerBase {
assertHandCount(playerB, 0);
}
+
+ /**
+ * With Hanweir Garrison and Hanweir Battlements in your control put Hanweir
+ * Battlements' ability in the stack to transform(i.e. meld). In answer to
+ * that, return to hand Hanweir Garrison. Resolve Hanweir Battlements
+ * ability.
+ *
+ * Expected result: The ability fizzles.
+ *
+ * Actual results: A NPE error is lauched.
+ */
+ @Test
+ public void testReturnToHand() {
+ addCard(Zone.BATTLEFIELD, playerA, "Mountain", 5);
+
+ // Whenever Hanweir Garrison attacks, put two 1/1 red Human creature tokens onto the battlefield tapped and attacking.
+ // (Melds with Hanweir Battlements.)
+ addCard(Zone.BATTLEFIELD, playerA, "Hanweir Garrison"); // Creature 2/3 {2}{R}
+
+ // {T}: Add {C} to your mana pool.
+ // {R},{T}: Target creature gains haste until end of turn.
+ // {3}{R}{R},{T}: If you both own and control Hanweir Battlements and a creature named Hanweir Garrison, exile them, then meld them into Hanweir, the Writhing Township.
+ addCard(Zone.BATTLEFIELD, playerA, "Hanweir Battlements"); // Land
+
+ // Brisela, Voice of Nightmares 9/10
+ // Flying, First strike, Vigilance, Lifelink
+ // Your opponents can't cast spells with converted mana cost 3 or less.
+ addCard(Zone.BATTLEFIELD, playerB, "Island", 1);
+ // Return target creature to its owner's hand.
+ addCard(Zone.HAND, playerB, "Unsummon", 1); // Instant {U}
+
+ activateAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "{3}{R}{R}");
+ castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerB, "Unsummon", "Hanweir Garrison", "{3}{R}{R}");
+
+ setStopAt(1, PhaseStep.BEGIN_COMBAT);
+ execute();
+
+ assertGraveyardCount(playerB, "Unsummon", 1);
+
+ assertPermanentCount(playerA, "Hanweir Battlements", 1);
+ assertHandCount(playerA, "Hanweir Garrison", 1);
+
+ }
}
diff --git a/Mage/src/main/java/mage/abilities/effects/common/MeldEffect.java b/Mage/src/main/java/mage/abilities/effects/common/MeldEffect.java
index 6f57fcaaa77..7f2f4c6af7e 100644
--- a/Mage/src/main/java/mage/abilities/effects/common/MeldEffect.java
+++ b/Mage/src/main/java/mage/abilities/effects/common/MeldEffect.java
@@ -79,32 +79,36 @@ public class MeldEffect extends OneShotEffect {
filter.add(new NamePredicate(meldWithName));
TargetPermanent target = new TargetControlledCreaturePermanent(filter);
Set meldWithList = target.possibleTargets(sourceId, source.getControllerId(), game);
+ if (meldWithList.isEmpty()) {
+ return false; // possible permanent has left the battlefield meanwhile
+ }
UUID meldWithId;
if (meldWithList.size() == 1) {
meldWithId = meldWithList.iterator().next();
- }
- else {
+ } else {
controller.choose(Outcome.BoostCreature, target, sourceId, game);
meldWithId = target.getFirstTarget();
}
// Exile the two permanents to meld.
Permanent sourcePermanent = game.getPermanent(sourceId);
Permanent meldWithPermanent = game.getPermanent(meldWithId);
- sourcePermanent.moveToExile(null, "", sourceId, game);
- meldWithPermanent.moveToExile(null, "", sourceId, game);
- // Create the meld card and move it to the battlefield.
- Card sourceCard = game.getExile().getCard(sourceId, game);
- Card meldWithCard = game.getExile().getCard(meldWithId, game);
- if (!sourceCard.isCopy() && !meldWithCard.isCopy()) {
- meldCard.setOwnerId(controller.getId());
- meldCard.setTopHalfCard(meldWithCard, game);
- meldCard.setbottomHalfCard(sourceCard, game);
- meldCard.setMelded(true);
- game.addMeldCard(meldCard.getId(), meldCard);
- game.getState().addCard(meldCard);
- meldCard.moveToZone(Zone.BATTLEFIELD, sourceId, game, false);
+ if (sourcePermanent != null && meldWithPermanent != null) {
+ sourcePermanent.moveToExile(null, "", sourceId, game);
+ meldWithPermanent.moveToExile(null, "", sourceId, game);
+ // Create the meld card and move it to the battlefield.
+ Card sourceCard = game.getExile().getCard(sourceId, game);
+ Card meldWithCard = game.getExile().getCard(meldWithId, game);
+ if (!sourceCard.isCopy() && !meldWithCard.isCopy()) {
+ meldCard.setOwnerId(controller.getId());
+ meldCard.setTopHalfCard(meldWithCard, game);
+ meldCard.setbottomHalfCard(sourceCard, game);
+ meldCard.setMelded(true);
+ game.addMeldCard(meldCard.getId(), meldCard);
+ game.getState().addCard(meldCard);
+ meldCard.moveToZone(Zone.BATTLEFIELD, sourceId, game, false);
+ }
+ return true;
}
- return true;
}
return false;
}