0

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.

5
  • Perhaps you could write something like 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 the ArrayList that you created inside main. Commented Oct 31, 2014 at 5:22
  • @DavidWallace I guess the value of row1 never passed to getArraylistsum function Commented Oct 31, 2014 at 5:23
  • 1
    @KickButtowski I imagine that will be the general gist of Supernatural's next question on this site. Commented Oct 31, 2014 at 5:25
  • @DavidWallace another issue is the op is using arraylist in the main and array in the function. so how the op wants to match the size of arraylist to array ? god knows Commented Oct 31, 2014 at 5:26
  • @DavidWallace cannot stop laughing bc of ur comments :D :'( :D Commented Oct 31, 2014 at 5:31

7 Answers 7

1

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];
Sign up to request clarification or add additional context in comments.

Comments

1

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));

}

Comments

0

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.

Comments

0

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

Comments

0

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>();.

Comments

0

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];

Comments

0

You should use for each function like

public int getArraylistsum(ArrayList<Integer> row1){

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

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.