0

I'm trying to set a Date (highestNumber) with a long (highest) but it throws a NullPointer exception, I can't figure out why.

public static Date highestNumber = null;

long highest = 0;
    for (int x = 0;x <numberofData - 2;x++){
        long first = Array.Stop.get(x).getTime();
        long next = Array.Stop.get(x+1).getTime();
        if(x==0){highest = first;}
        if (highest < next){
            highest = next;
        }
    }
    highestNumber.setTime(highest);

Exception is being thrown on the setTime() line. highest gets a valid value of 1523328768914

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.Date.setTime(long)' on a null object reference

Edit: I don't feel like this is a duplicate of "What is a nullpointerexception since i didn't realise a Date had to be initiaized. I thought it was just a variable like a string.

dec0yable
  • 87
  • 13

2 Answers2

0

Instead of

 highestNumber.setTime(highest);

Use

highestNumber= new Date(highest);

You haven't initialized your highestNumber object. Before using it at highestNumber.setTime(highest); That's why NullPointerException.

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
0

You have not initialized highestNumber. You need to initialize highestNumber like this,

public static Date highestNumber = new Date();
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33