I'm a newbie in Java, but i'm taking a course where we write a web-app which is able to save, store, edit and show resumes. I'm stuck with the 'save' method, where we send a resume as parameter and have to check by resume's UUID:
if the UUID already exists in the array - an error message will be shown,
if the UUID is not in the array - the resume will be saved.
I've made my method with a temp boolean, but according a task it's not right, so i'm trying to solve it with a for loop and an if statement with break, but i can't understand how to save the resume because NPE throw in if statement. Here is my code:
public class ArrayStorage {
private Resume[] storage = new Resume[10000];
private int size;
public void save(Resume resume) {
for (int i = 0; i <= size; i++) {
if (storage[i].getUuid().equals(r.getUuid())) {
System.out.println("ERROR: Resume with " + storage[i].getUuid() + " is already exist!");
break;
}
storage[size] = resume;
size++;
}
}
Size variable is there to avoid checking the whole array, just elements that were stored.
Edited. I forgot an equal sign in a for loop, and this is an error
Exception in thread "main" java.lang.NullPointerException
at com.webapp.storage.ArrayStorage.save(ArrayStorage.java:23)
at com.webapp.MainTestArrayStorage.main(MainTestArrayStorage.java:22)
In MainTestArrayStorage i have those fields
Resume r1 = new Resume();
r1.setUuid("uuid1");
Resume r2 = new Resume();
r2.setUuid("uuid2");
Resume r3 = new Resume();
r3.setUuid("uuid3");
Resume r4 = new Resume();
r4.setUuid("uuid1");
ARRAY_STORAGE.save(r1);
ARRAY_STORAGE.save(r2);
ARRAY_STORAGE.save(r3);
ARRAY_STORAGE.save(r4);
The last line is for check method, that shouldn't save it, because it has the same UUID If you need any additional information i'll be glad to answer. Thanks.
P.S. Sorry for my English, i'm not good at it.