-2

I'm trying to populate an ArrayList<Integer> with numbers. When I call on the method in the main method, the compiler complains that

Exception in thread "main"` 

    java.lang.NullPointerException
        at PrimeSum.setGeneralArray(PrimeSum.java:11)
        at PrimeSum.main(PrimeSum.java:36)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Process finished with exit code 1

This a sample of my code.

public class PrimeSum {
private ArrayList<Integer> generalArray;
private int limit;
public void setGeneralArray() {
    for(int i = 2; i < 2000001; i++) {
        generalArray.add(i);
    }
}
// ...
// This is how I call on the method.
public static void main(String[] args) {
    PrimeSum ps = new PrimeSum();
    ps.setGeneralArray();
    // ...

What's the thing I'm doing wrong?

das-g
  • 8,581
  • 3
  • 33
  • 72
Samson Akanbi
  • 33
  • 2
  • 6

6 Answers6

0

You forgot to initialize the ArrayList, my friend.

private ArrayList<Integer> generalArray = new ArrayList<Integer>();

Best way to do it is intialize it in a constructor.

public class PrimeSum {
private ArrayList<Integer> generalArray;
private int limit;
public PrimeSum(){
    generalArray = new ArrayList<Integer>();
}
public void setGeneralArray() {
    for(int i = 2; i < 2000001; i++) {
        generalArray.add(i);
    }
}
...
//This is how I call on the method.
public static void main(String[] args) {
    PrimeSum ps = new PrimeSum();
    ps.setGeneralArray();
    ...
Pankaj Singhal
  • 12,388
  • 7
  • 37
  • 74
0

Initialization, is missing for the array:

private ArrayList<Integer> generalArray = new ArrayList<Integer>();

Without this, the generalArray is null (thus the NullPointerException).

private ArrayList<Integer> generalArray;
public PrimeSum(){
    generalArray = new ArrayList<Integer>();
}
nitishagar
  • 7,764
  • 3
  • 20
  • 35
0

generalArray is a reference type, its initial value is null, that's what the JLS 4.12.5. Initial Values of Variables states:

For all reference types (ยง4.3), the default value is null.

ReferenceType:
    ClassOrInterfaceType
    TypeVariable
    ArrayType

To initialize it, you should use the new keyword and call the constructor that matches your needs.

Maroun
  • 87,488
  • 26
  • 172
  • 226
0

The instance field generalArray is still null when you invoke add.

You need to initialize it before you populate it.

Either upon declaration:

private ArrayList<Integer> generalArray = new ArrayList<Integer>();

Or in the constructor:

public PrimeSum() {
    generalArray = new ArrayList<Integer>();
}

Or in an instance statement:

{
    generalArray = new ArrayList<Integer>();
}
Mena
  • 45,491
  • 11
  • 81
  • 98
0

By default all the Objects in java is initialised to null and you are trying to say null.add(number) and hence you are getting null pointer exception. See this for details. You need to initialize it. You could do it either directly in the class itself

 private ArrayList<Integer> generalArray = new ArrayList<Integer>();

or you could initialize it in constructor which is called whenever you create an object of type PrimeSum as you do in main like PrimeSum ps = new PrimeSum();. You could do it as below:

 public PrimeSum () {
     generalArray = new ArrayList<Integer>();
 }
SMA
  • 33,915
  • 6
  • 43
  • 65
0

create a arraylist of integers

   ArrayList<Integer> generalArray = new ArrayList<Integer>

for add item in list do this:

   generalArray.add(1);
   generalArray.add(2);
   generalArray.add(3);
Mohammadreza Khatami
  • 1,284
  • 2
  • 11
  • 24