NullPointerException when setting array element

I'm trying to create a Method to output an array showing only specific numbers beginning with 030 , didnt find any Solution in the Internet yet. when running the Program it shows the following Exception : Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Contact.setNumber(String)" because "array[i]" is null. I already created two Classes , one for Contact , where i put getters and Setters for Number and Numerisation , Second Class ContactManagement for filtering the array containing the numbers (public static Contact[] ShowBerlinsContactList(Contact[] list) )

public class ContactManagement {

static Contact N1 = new Contact("1", "0301340711036");
static Contact N2 = new Contact("2", "0301144663311");
static Contact N3 = new Contact("3", "0201629360578");
static Contact N4 = new Contact("4", "0451629360123");
static Contact N5 = new Contact("5", "0303544442103");
static Contact N6 = new Contact("6", "0305344442103");
static Contact N7 = new Contact("7", "0701571456781");
static Contact N8 = new Contact("8", "0701571446781");

public static Contact[] ContactList = new Contact[]{N1, N2, N3, N4, N5, N6, N7, N8};


public static Contact[]  ShowBerlinsContactList(Contact[] list) {
    Contact[] array = new Contact[list.length] ;

    for (int i = 0; i <= list.length; i++) {
        String a = list[i].getNumber();
        char c0 = a.charAt(0);
        char c1 = a.charAt(1);
        char c3 = a.charAt(2);
        for(int j=0;j<= array.length; j++) {
            if ((c0 == '0') && (c1 == '3') && (c3 == '0')) {
                array[i].setNumber(a);
            }
        }
    }
    return array ;
}

public static void main(String[] args) {
    try{
    for(int i = 0 ; i <= ContactList.length ; i++) {
        System.out.println("N"+(i+1) +":" + ContactList[i].toString());
    }
    } catch (Exception e) {}


    Contact[] BerlinContactList = new Contact[10] ;
     for(int j=0 ; j < 10 ; j++){
         BerlinContactList[j] = ShowBerlinsContactList(ContactList)[j] ;
         System.out.println(BerlinContactList[j]);
     }
    }
    }
public class Contact {

public String Number;
public String Numerisation;

 Contact(String Numerisation, String Number) {
  this.Number = Number;
  this.Numerisation = Numerisation;
 }

 public String getNumber() {
  return Number;
 }

 public String getNumerisation() {
  return Numerisation;
 }

 public void setNumber(String number) {
  Number = number;
 }

 public void setNumerisation(String numerisation) {
  Numerisation = numerisation;
 }

 public String toString() {
  return  Number;
 }
}

i tried changing public static Contact[] ShowBerlinsContactList(Contact[] list) into public static void method , and printed the output directly inside the method and it works , so i dont get why it cant return the result in an array.



from Recent Questions - Stack Overflow https://ift.tt/3aCSSoq
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Network Error and Timeout on Authorize.net JS

Spring Elasticsearch Operations