0

Im getting a NullPointerException error on my code. I really dont know why it is happenning. This's only a sample of the code, i can post the full version if needed.

private static final Scanner in=new Scanner(System.in);
public static void main(String[] args) {
   int n1=lerTamanho();
   String [] vec1=new String [n1];
   readVector(vec1); -> Line 11 
}

private static int lerTamanho() {
    System.out.print("Number: ");
    int num=in.nextInt();
    while (num<=0) {
        System.out.print("Error! Number: ");
        num=in.nextInt();
    }
    return num;
}

private static void readVector(String vec[]) {
    int cont=0;
    String s;
    do  {  
        System.out.print("Name: ");
        s=in.nextLine();
        if (validate(s,vec)) { -> Line 30
            vec[cont]=s;
            cont++;
        } else {
            System.out.println("That name already exists!");
        }
    } while (cont<vec.length);
    }
private static boolean validate(String s, String vec[]) {
    boolean success=true;
    for (int i=0; i<vec.length; i++) {
        if (vec[i].equalsIgnoreCase(s)) { >-> Line 44
            return !success;
        }
    }
    return success;
}

Error I'm getting:

Exception in thread "main" java.lang.NullPointerException
at javaapplication63.JavaApplication63.validate(JavaApplication63.java:41)
at javaapplication63.JavaApplication63.lerVetor(JavaApplication63.java:30)
at javaapplication63.JavaApplication63.main(JavaApplication63.java:11)
4
  • something must have called validate passing it null for either s or vec Commented Nov 20, 2013 at 17:11
  • 1
    It would be very useful if you pointed out which line is line 41. Commented Nov 20, 2013 at 17:11
  • Looks like either vec or s (in the validate method) hasn't been initialized. You might want to post the code for the lerVector() method. Commented Nov 20, 2013 at 17:11
  • s can be null and this code would do fine, vec is null. Commented Nov 20, 2013 at 17:12

5 Answers 5

3

By default, all the elements in the String[] are initialized with null. The NullPointerException is thrown on this line:

if (vec[i].equalsIgnoreCase(s))

In order to fix it, assign the elements with values, before validating

Sign up to request clarification or add additional context in comments.

Comments

1

When you make a new array, ie String [] vec1=new String [n1];, the elements inside the array are initialized to null. Thus, when you try to access vec[i].equalsIgnoreCase(s) in

if (vec[i].equalsIgnoreCase(s)) {

you get a NPE.

3 Comments

I'd change the wording here to say the elements of the array are initialized to null rather than uninitialized. Saying it's uninitialized sounds like it might be filled with garbage (like what would happen in C/C++ with freshly-allocated memory). Java always sets new memory to the "zero" value, which is null for Objects, 0 for numbers and false for Booleans.
@SimplyPanda Thank you. I missed that detail ^^
@Exsound We always appreciate when our answers are selected; kocko's is more complete and was faster, so I do suggest it! (Also his was accurate upon conception)
1
   String [] vec1=new String [n1];

This statement initializes the array - it does not initialize the individual buckets of the array. For that you can walk the array in a look and set them explicitly.

 for (String s : vec) {
     s = "Some String"; //or new String()
 }

Comments

1

Switch vec[i].equalsIgnoreCase(s) to s.equalsIgnoreCase(vec[i])

2 Comments

Can you tell me the difference between them?
if vec[i] is null, you are trying to call a method of an object which doesn't exist. By swapping them, the method is called on s, which should always be non-null. When it gets to the portion of the array that hasn't been assigned to yet, it will essentially be s.equalsIgnoreCase(null), which will just return false. That way you don't have to worry about breaking from the loop once you reach the null values. If you wanted to keep the original order, you would need to add if(vec[i] == null){break;} right before
0

You have created an array with below code.

String [] vec1=new String [n1];

It only creates an array with n1 elements, but all values in those elements are null.

You can try below and see.

int n1=lerTamanho();
String [] vec1=new String [n1];
for (int i=0; i < n1; i++) {
    vec1[i]= "SOME-STRING-TO-THIS-ELEMENT";
}
readVector(vec1);

NPE comes from vec[i].equalsIgnoreCase(s) where vec[i] is null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.