0

c.getCollectibles()[i][j].isCollected() ? collectiblePanels[i][j].setSplash(c.getCollectibles()[i][j].getIcon()) : repaint();

I am writing a program in Java and have an error in my program. I am using a conditional operator. c.getCollectibles() returns a two-dimensional array of a Collectible class, and isCollected() is a non-static public method in the Collectible class that returns a boolean value.

collectiblePanels is a two-dimensional array of a class that contains a setSplash() method. The setSplash() method takes in the same type that getIcon() returns (the specifics are irrelevant to the error). Overall, setSplash() is void.

repaint() is a random void method. Again, specifics are irrelevant to the error.

The specific error is below:

The left-hand side of an assignment must be a variable
Syntax error on token "?", invalid AssignmentOperator
Syntax error on token ":", ; expected
Justin Albano
  • 3,809
  • 2
  • 24
  • 51
  • 1
    I have tried the same thing using the standard if-else format and no error occurs. This seems like a very peculiar bug on Oracle's side or a very stupid error on mine. – Brandon Koerner Apr 08 '18 at 03:05
  • 2
    [JLS-15.25. The Conditional Operator `? :`](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.25) says **explicitly** *It is a compile-time error for either the second or the third operand expression to be an invocation of a `void` method.* Here ***both*** are `void`. – Elliott Frisch Apr 08 '18 at 03:07
  • 1
    Go with the latter, definitely the latter -- it's a bug in your understanding of how the ternary operator works. But this is all easily findable if you would search Google on your error message. [For example](https://www.google.com/search?&q=site%3Ahttps%3A%2F%2Fstackoverflow.com%2F+java+ternary+operator+left-hand+side+of+an+assignment+must+be+a+variable) – Hovercraft Full Of Eels Apr 08 '18 at 03:08
  • You've discovered, the hard way, that the conditional operator isn't a drop-in replacement for an `if` statement. Live and learn, my friend :-) – Kevin Anderson Apr 08 '18 at 03:10
  • Thanks again. After familiarizing myself better with how java handles the conditional operator, I feel a bit silly with this question. – Brandon Koerner Sep 16 '18 at 04:31

0 Answers0