-3

I am trying to pass a String of "A" to a constructor. The problem I am running into is the instance variable that has to be set isn't filled with anything. Now I am trying something different than I ussually do. As soon as I try anything with the array variable it gives a NPE. So how to fix this and avoid it in the future. Thanks!

public Array(String x){
        if(x == "A" ){
            array = { { 1, 1 }, { 1, 1 }, { 1, 1 } };
        }
        if(x == "B"){
            array = { { 2, 2 }, { 2, 2 }, { 2, 2 } }; 
        } 
        if(x == "C"){
            array = { { 3, 3 }, { 3, 3 }, { 3, 3 } };
        }
    }
Marciano
  • 142
  • 1
  • 11
  • 6
    Use `String.Equals` instead of `==` – Mike G Jan 08 '15 at 17:26
  • 1
    Can you please show use where you're calling that constructor? Where to you get the exception? What is its Stacktrace? – Tom Jan 08 '15 at 17:26
  • 4
    Can you post the entire class code? I find it strange you would name the class Array – Laplace Jan 08 '15 at 17:26
  • I get the exeption as soon as I try and get the length of the 'array' it is called array because I am testing it in a seperate class the class I am using it in includes a ui but I didn't want to open the ui everytime I changed something – Marciano Jan 08 '15 at 18:04

2 Answers2

2

array is probably always going to be null. For two reasons...

First, the comparison logic isn't correct. Don't use == to compare strings, use String.equals instead.

Second, the logic has no default condition. What happens if none of the conditions are satisfied? array is never initialized. Add a default:

if(x.equals("A") ){
    array = { { 1, 1 }, { 1, 1 }, { 1, 1 } };
} else if(x.equals("B")){
    array = { { 2, 2 }, { 2, 2 }, { 2, 2 } }; 
} else if(x.equals("C")){
    array = { { 3, 3 }, { 3, 3 }, { 3, 3 } };
} else {
    // set array to a default value?
    // throw an exception?
    // something else?
}

(Note: I can't say with much certainty if throwing an exception from within a constructor is good or bad practice in Java. There could very easily be details of how the JVM behaves on the matter which are unfamiliar to me. If it is bad practice, then perhaps a factory method instead of a constructor would be a good way around that.)

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • Totally forgot about String.equals. Will add a default condition. Only thing that is happening right now is it is saying this: "Array constants can only be used in instantiators." – Marciano Jan 08 '15 at 17:33
  • @Marciano: That one's new to me, but to be fair I don't do a lot with Java. Based on the error, perhaps a way to fix that would be to set these hard-coded array constants as just that, constants (or private final class members of some kind), and reference them within this method, instead of defining them in-line in this method. – David Jan 08 '15 at 17:35
  • I have tried that but that doesn't cut it either. I was just trying to dodge the hassle of inputting the arrays into the constructor itself, but seeing that this way of doing it isn't not working I am looking for alternatives – Marciano Jan 08 '15 at 17:42
0
public ArrayWrapper(String x){ //"Array" is a confusing name for a class
    if(x == null) {
        System.err.println("x is null! Please instantiate x before calling Array().");
        return;
    }
    //...
}
Kukanani
  • 718
  • 1
  • 6
  • 22