Fix an NPE

PiperOrigin-RevId: 392546577
diff --git a/java/com/google/turbine/parse/ConstExpressionParser.java b/java/com/google/turbine/parse/ConstExpressionParser.java
index fa5556a..db144cf 100644
--- a/java/com/google/turbine/parse/ConstExpressionParser.java
+++ b/java/com/google/turbine/parse/ConstExpressionParser.java
@@ -17,7 +17,6 @@
 package com.google.turbine.parse;
 
 import static com.google.common.collect.Iterables.getOnlyElement;
-import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableList;
 import com.google.errorprone.annotations.CheckReturnValue;
@@ -232,9 +231,11 @@
         case NOT:
         case TILDE:
         case IDENT:
-          // primary returns non-null for these token kinds
-          return new Tree.TypeCast(
-              position, asClassTy(cvar.position(), cvar.name()), requireNonNull(primary(false)));
+          Expression expression = primary(false);
+          if (expression == null) {
+            throw error(ErrorKind.EXPRESSION_ERROR);
+          }
+          return new Tree.TypeCast(position, asClassTy(cvar.position(), cvar.name()), expression);
         default:
           return expr;
       }
diff --git a/javatests/com/google/turbine/parse/ParseErrorTest.java b/javatests/com/google/turbine/parse/ParseErrorTest.java
index 5fcfc88..2c48b81 100644
--- a/javatests/com/google/turbine/parse/ParseErrorTest.java
+++ b/javatests/com/google/turbine/parse/ParseErrorTest.java
@@ -294,6 +294,19 @@
                 "   ^"));
   }
 
+  @Test
+  public void notCast() {
+    String input = "@j(@truetugt^(oflur)!%t";
+    TurbineError e = assertThrows(TurbineError.class, () -> Parser.parse(input));
+    assertThat(e)
+        .hasMessageThat()
+        .isEqualTo(
+            lines(
+                "<>:1: error: could not evaluate constant expression",
+                "@j(@truetugt^(oflur)!%t",
+                "                     ^"));
+  }
+
   private static String lines(String... lines) {
     return Joiner.on(System.lineSeparator()).join(lines);
   }