-3

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!

  • Where you are calling/using these methods ? – Suresh Atta Dec 26 '14 at 12:37
  • 1
    Step through your code with a debugger and you'll find out very quickly. [Debugging in Eclipse](http://www.vogella.com/tutorials/EclipseDebugging/article.html) will give a good basis that will help you solve most of these things on your own in any IDE. – Jeroen Vannevel Dec 26 '14 at 12:39
  • 2
    `but as soon as I compile this code you will get an NullPointerException in the code` NPE at compile time..? – Abhishek Dec 26 '14 at 12:39
  • @Abhishek he must be using a compiler with alien technology :) ,Sorry OP. – Rishi Dec 26 '14 at 12:40
  • Code looks ok, it should work if used in a proper way. My guess is that either you are calling these methods in wrong order, or on different instances of the containing class. – Predrag Maric Dec 26 '14 at 12:40
  • What specific line of code is throwing the exception? – Kenster Dec 26 '14 at 13:21

1 Answers1

0

first are you sure you are calling CreatingArray() before getWsize()?

then try using only the global variable vectorTime, without using the local variables tm and timestamp. just to make sure it works.

//declaring global variable
ArrayList<Long> vectorTime;                         //before
ArrayList<Long> vectorTime = new ArrayList<Long>(); //after

//inside CreatingArray()
ArrayList<Long> tm = new ArrayList<Long>(); //before
vectorTime.clear();                         //after

//inside getWsize
long SumSamp1 = timestamp.get(0); //before
long SumSamp1 = vectorTime.get(0);//after

after you have found the error, you can put back those local variable.

hanbin615
  • 575
  • 1
  • 5
  • 13