Supernaturalgirl 1967
Supernaturalgirl 1967

Reputation: 289

Adding Arraylist values method java

I want to create a method to add the Arraylist firstRow. But I keep getting the error nullPointerexception. Its because of the for loop for(int i = 0; i<firstRow.length; i++) in the getArraylistsum() method

Here is the whole code:

int[] row1;

public int getArraylistsum(){
    int sum = 0;
    for(int i = 0; i<row1.length; i++){
        sum += row1.length;
    }
    return sum;
}

public static void main(String[] args){
    ArrayList<Integer> row1 = new Arraylist<>(10); 
    row1.add(1);
    row1.add(8);
    row1.add(6);

    ClassName row = new ClassName(); 
    System.out.println(row.getArraylistsum());

}

Thanks for your help.

Upvotes: 0

Views: 113

Answers (7)

Visme
Visme

Reputation: 983

I think int[] row1 is not necessary and values is not assigned to row1 array too. rather than used row1 array, passed the row1 arraylist to your method. In the main function, ArrayList is written as Arraylist. I changed this into Arraylist.

 public int getArraylistsum(ArrayList<Integer> row1){

    int sum = 0;
    for(int i = 0; i<row1.size(); i++){
        sum += row1.get(i);
    }
    return sum;
}

public static void main(String[] args){
    ArrayList<Integer> row1 = new ArrayList<Integer>(10); 
    row1.add(1);
    row1.add(8);
    row1.add(6);

    ClassName row = new ClassName(); 
    System.out.println(row.getArraylistsum(row1));

}

Upvotes: 1

DDshah
DDshah

Reputation: 41

You should use for each function like

public int getArraylistsum(ArrayList<Integer> row1){

    int sum = 0;
    for(int i:row1){
        sum += i;
    }
    return sum;
}

Upvotes: 0

subodh
subodh

Reputation: 6158

null.length definitely gives you NullPointerException int[] row1 = null; and you are calling method on null instance inside getArraylistsum() method.

initialize row1 instance inside your getArraylistsum() method using below code.

row1=new int[size];

Upvotes: 0

Aviator
Aviator

Reputation: 522

You are using two row1 variables. Make the ArrayList variable a static class variable by declaring the List by static ArrayList<Integer> row1; instead of the int[]. In the Main Method you then should use ClassName.row1 = new ArrayList<Integer>();.

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

You have not initialize

  int[] row1;// when you create a instance of a class this row1 will null

Then row1.length give you NullPointerException

You need to initialize row1 before use it.

 int[] row1=new int[size];

Upvotes: 1

Ashish Aggarwal
Ashish Aggarwal

Reputation: 3026

First initialize row1

int[] row1;

In your case as row1 is not intialized and you are trying to acces the length of that row1 thats why you received NullPointerException

Upvotes: 0

Eran
Eran

Reputation: 393811

ArrayList<Integer> row1 is declared and initialized in your main method. Your getArraylistsum() tries to access a different int[] row1 variable which in not initialized. Therefore it causes a NullPointerException.

Upvotes: 0

Related Questions