1
public class AClasse {
 String a;
 public AClasse(String a){
    this.a = a;
 }

 public static void main(String[] args){
    AClasse[] clases = new AClasse[10];
    clases[0].a = "?"; // the exception is in this line 
 }
}

whats the reason and whats the right equivalent statement.

Lr Ayman
  • 17
  • 4

1 Answers1

1

The problem is that clases[0] is null and you are trying to perform some operation on it. Check the following code:

public class AClasse {
    String a;

    public AClasse(String a) {
        this.a = a;
    }

    public static void main(String[] args) {
        AClasse[] clases = new AClasse[10];
        // clases[0].a = "?"; // the exception is in this line

        System.out.println(clases[0]);

        // Do it as follows
        clases[0] = new AClasse("?");
    }
}

Output:

null
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110