0

I'm fairly new to java only starting this year, sophomore year in high school, I am currently working on a side project and can't get this block of code to work, I know where it's not working I just need help knowing why.

public Timer[] t = new Timer[3];

public TimerTask tt[] = new TimerTask[3];

public int ttc = 0; 
public int[] hasBought = new int[3];
public int[] j = 1;

public void run()
{
    for(;ttc<3; ttc++)
    {
        System.out.println(ttc+"a");
        tt[ttc] = new TimerTask()
                {

                    @Override
                    public void run()
                    {
                        System.out.println(ttc+"b");
                        count += (j*hasBought[ttc]);                            
                    }

                };

        System.out.println(ttc+"c");
        t[ttc].scheduleAtFixedRate(tt[ttc] , 1000, 1000);
    }

From what I currently know this should all run fine and print 0a 0b 0c 1a 1b 1c 2a 2b 2c and it should also start 3 timers but it is printing 0a 0c and coming up with a NullPointerException error. The 2nd part makes sense because the TimerTasks obviously aren't being initialized because it isn't printing 0b.

EDIT

I'm not sure what is causing the problem but I'm guessing it is something to do with these lines:

tt[ttc] = new TimerTask()
                {
                    @Override
                    public void run()

EDIT

I'm not having problems with a NullPointerException, I know what one is and how to fix it, I don't understand why the TimerTask isn't initiating.

0 Answers0