I have tried to use an ArrayList within my class, but I get Nullpointerexception and I really dont know why, here is my class:
......
RawVector vector2;
**ArrayList<Long> vectorTime;**
public RawVector CreatingArray(){
try {
ArrayList<Float> x = new ArrayList<Float>();
ArrayList<Float> y = new ArrayList<Float>();
ArrayList<Float> z = new ArrayList<Float>();
**ArrayList<Long> tm = new ArrayList<Long>();**
File n = new File("file.csv");
FileReader fileReader = new FileReader(n);
BufferedReader reader = new BufferedReader(fileReader);
for(String zeile = reader.readLine(); zeile != null; zeile = reader.readLine()){
StringTokenizer data = new StringTokenizer(zeile, " ,");
**tm.add(Long.parseLong(data.nextToken()));**
x.add(Float.parseFloat(data.nextToken()));
y.add(Float.parseFloat(data.nextToken()));
z.add(Float.parseFloat(data.nextToken()));
size++;
}
vector2 = new RawVector();
vector2.setX(x);
vector2.setY(y);
vector2.setZ(z);
vector2.setSize(size);
vector2.setTime(tm);
reader.close();
**this.vectorTime = tm;**
}catch (IOException e) {
e.printStackTrace();
}
return vector2;
}
public ArrayList<Long> zeitstempel(){
return this.vectorTime;
}
public long getWsize(){
long wsize;
ArrayList<Long> timestamp;
**timestamp = vectorTime;**
**long SumSamp1 = timestamp.get(0);**
long SumSamp2 = timestamp.get(vectorTime.size());
long timespan = SumSamp2 -= SumSamp1;
long timespanInSec = TimeUnit.MILLISECONDS.toSeconds(timespan);
wsize = Zeitfenster*vectorTime.size()/timespanInSec;
return wsize;
}
.....
I have created an ArrayList<Long> tm
in the method CreatingArray()
and assigned it to the instance variable vectorTime
by this.vectorTime = tm
. Next, I have used this instance variable in the method getWize()
and assigned to ArrayList<Long> timestamp
but as soon as I compile this code you will get an NullPointerException in the code line long SumSamp1 = timestamp.get(0)
.
What's the problem??? I was pretty sure that the instance variable vectorTime
is initialized by this.vectorTime = tm
. But according to the exception it doesn't and I don't know why?!
Probably I confuse assginment and initialization...
Im new to Java therefore I think this kind of mistakes are typically for beginners like me. Nevertheless I hope that any of you can help and can give me some useful tips to fix that problem.
Thanks in advance for any help!!!
Best Regards!