I don't know how to initialize synapseNeuronList arrayList. got null error

private ArrayList <Integer> synapseNeuronList;
for (int j=1;j<strLine.split(" ").length-1;j++){                                              
synapseNeuronList.add(Integer.valueOf(strLine.split(" ")[j]));  
}
share|improve this question
1  
private ArrayList <Integer> synapseNeuronList = new ArrayList<Integer>(); ... – zeller Jul 9 '12 at 20:27
By the way, you should store the result of your split() method call in a variable. Otherwise you're calling it twice per loop, which is a big performance hit. Also, are you sure you want those conditions in your for loop and not for(int j=0;j<strs.length;j++) – Matt Jul 10 '12 at 3:40

2 Answers

up vote 4 down vote accepted

Your synapseNeuronList needs to be reference to some ArrayList (or its subtype) object. To create object use new operator like this

private ArrayList <Integer> synapseNeuronList = new ArrayList <Integer>();
share|improve this answer
Excellent thanks – cfircoo Jul 9 '12 at 20:32

You would either do this directly when you declare the variable:

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

or inside a constructor:

public YourClassConstructor() {
    synapseNeuronList = new ArrayList<Integer>();
}

Hope this helps!

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.