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.
int[] row1 = new int[10];on your first line - actually make the array instead of just declaring it. Also, it's a really bad idea to use the same name for that array as you've used for theArrayListthat you created insidemain.