0

In the first class I have:

package example.identification;

import example.common.InvalidDataException;

public class IdentifiableImpl implements Identifiable {

    private String identifier;

    public IdentifiableImpl(String id) throws InvalidDataException {
        setIdentifier(id);
    }

    @Override
    public String getIdentifier() {
        return identifier;
    }

    public final void setIdentifier(String id) throws InvalidDataException {
        if (id == null || id.length() == 0) {
            throw new InvalidDataException("Null or empty ID passed to setIdentifier");
        }
        identifier = id;
    }
}

In the second class I have:

package example.identification;
public class IdentifiableTest {

@Test
    public void testGetIdentifier() {
        IdentifiableImpl instance = new IdentifiableImpl();
        instance.setIdentifier("Test");
    }

The problem is in the second class with the line instance.setIdentifier("Test"); There is an error reported by the IDE on that line that says "Cannot find symbol."

My question is, why can I not call the setIdentifier("Test") method on instance ?

Omar N
  • 1,720
  • 2
  • 21
  • 33

1 Answers1

1

You need to add a constructor in your IdentifiableImpl.java class and you're missing a curly brace in your IdentifiableTest.java class

You need the constructor or you won't be able to create the instance. You can't call setIdentifier("Test") on instance because it was never created.

Added Constructor in IdentifiableImpl

public IdentifiableImpl() {

}

Fixed brace in testGetIdentifier

public void testGetIdentifier() {
    IdentifiableImpl instance = new IdentifiableImpl();
    instance.setIdentifier("Test");
    System.out.println(instance.getIdentifier());
}

It works fine for me after these changes

Chris Sharp
  • 2,005
  • 1
  • 18
  • 32