0

Simple Apex (Java actually) question, after I initialized the variable 'a' in a constructor, I tried to access it with a different method but it remains null.

Class FirstClass{
    String a;
    public FirstClass (String a){
        this.a =a;
    }

    public void methodWhichIssueOccur() {
        System.debug('a: ' +a); // ---->  a is null
    }
}

@IsTest
Class TestClass{
    @IsTest
    public void testFirstClass(){
        String a = 1;
        FirstClass fc = new FirstClass(a);
        fc.methodWhichIssueOccur();
    }
}
  • 2
    String a = 1 will not compile as it's a number and not string. (Hope that's a typo) Try debugging fc.a after an instance of FirstClass is created. – Vijay Ganji Sep 10 '19 at 17:20
1

Your constructor is using the wrong name, try this instead:

Class FirstClass{
    String a;
    public FirstClass(String a){
        this.a = a;
    }

    public void methodWhichIssueOccur() {
        if(a == '1') {
            System.debug('a: '+ a); // ---->  a is null
        }
    }
}

@IsTest
Class TestClass{
    @IsTest
    public void testFirstClass(){
        String a = 1;
        FirstClass fc = new FirstClass(a);
        fc.methodWhichIssueOccur();
    }
}
|improve this answer|||||
  • I apologize @vm53 for I made a mistake when posting the question, the constructor name is FirstClass, otherwise it would not let me save it. I edited my post. – Json Sep 10 '19 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.