Search code examples
scalaintellij-ideaidesymbolscannot-find-symbol

Scala: Method within Class issues within IntelliJ [Cannot Resolve Symbol]


My code is working fine and executing, my issue is with IntelliJ giving me a 'cannot resolve symbol' error on the methods of the class.

code is as follows:

// Q1. Create a Sailboat class with methods to raise and lower the sails,
// printing Sails raised, and Sails lowered, respectively.
// Create a Motorboat class with methods to start and stop the motor,
// returning Motor on, and Motor off, respectively. Create an object (instance)
// of the Sailboat class. Use assert for verification:

class Sailboat {
  def raise  = "Sails raised"
  def lower = "Sails lowered"
}

val sailboat = new Sailboat
val r1 = sailboat.raise
assert(r1.equals("Sails raised"), "Expected Sails raised, Got " + r1)
val r2 = sailboat.lower
assert(r2.equals("Sails lowered"), "Expected Sails lowered, Got " + r2)

See attached screenshot of inteliJ's error, this occurs on both methods and the equals method. InteliJ Error

I have a very similar script that is basically the same thing but it does not give any errors:

class Motorboat {
  def on  = "Motor on"
  def off = "Motor off"
}

val motorboat = new Motorboat
val s1 = motorboat.on
assert(s1 == "Motor on", "Expected Motor on, Got " + s1)
val s2 = motorboat.off
assert(s2 == "Motor off", "Expected Motor off, Got " + s2)

see attached screenshot of similar code with no error: Similar code with no error in IntelliJ

What I have tried;

  1. re-writing the class & methods
  2. restarting the IDE
  3. restarting my laptop
  4. updating IntelliJ
  5. google search on why this is happening

Any ideas on how to solve this would be greatly appreciated

Thanks!


Solution

  • Very strange behaviour, but I have managed to resolve this by adding a 3rd class within the same file;

    class Flare {
      def light = "Flare used!"
    }
    
    val flare = new Flare
    val f1 = flare.light
    assert(f1 == "Flare used!", "Expected Flare used!, Got " + f1)
    

    This has removed the errors from the sailboat class and code is clear on IntelliJ

    Very strange....