aspose file tools*
The moose likes Java in General and the fly likes NPE when trying to get size() in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "NPE when trying to get size() in ArrayList" Watch "NPE when trying to get size() in ArrayList" New topic
Author

NPE when trying to get size() in ArrayList

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Hello!

I'm stuck with an assignment. It consists of bouncing footballs. Everytime I click on the screen I create a new bouncing football. (soccer). I have four classes: FotBollsPlan which is the footballfield that has an ArrayList consisting of footballs and a timer. FotBoll that draws the football . It actually picks it from reflibrary where I've put them. It has also a timer. FootballMain that extends JFrame. And last UppDatera that updates the footballfield in intervalls, for example 1000 ms, with a timer. And it is here(Uppdatera ) where I have my problem. Here is the code:


So, everything works fine til the last class - uppDatera. I e I'm able to click on the panel and all these footballs appears and they are bouncing around. But as a last issue to do I have to update the footballfield at intervalls and all footballs have to been drawn on the panel at that moment and I have to have a piece of text in the bottom of the frame (panel?, label?) that says how many footballs that are on the field right now. Maybe I don't need this class uppDatera at all. But at line 219 I get an NullPointerException. And I can't understand how to get the size from the ArrayList?

Can anyone please help me here?

Anders
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Question:

Where do you initialize the private java.util.List<FotBoll>fb; found at row 203 that are referred at 218.
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Thank you for answering!

I've been working with the problem today and I believe that I don't need the class uppDatera. So, now the class FotBollsPlan looks like this:


With this I'm quite close to the final code. But now I've got another problem were I'm stuck. It is in the method actionPerformed.
The balls are bouncing around OK and after a while they stop which I want them to. This happends since I stop the timer and setDelay to 3000 ms.(line 47 - 52) But as you can see I restart() the timer again and I thought that the timer should jump back again to 50 ms - so that the balls starts to bounce fast again, but they don't. I've tried different scenarios here with setInitialDelay and so on but nothing works correct. How do I get back to a delay in the timer of 50 ms?

Anders
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Anders Kviback wrote:
...

This happends since I stop the timer and setDelay to 3000 ms.(line 47 - 52) But as you can see I restart() the timer again and I thought that the timer should jump back again to 50 ms - so that the balls starts to bounce fast again, but they don't. I've tried different scenarios here with setInitialDelay and so on but nothing works correct. How do I get back to a delay in the timer of 50 ms?

Anders


You set it.




In your code, you set the delay to 50 ms in the constuctor. When you have done 100 loops, you stop the timer t on line 8 in the code snippet above. Then you set the instance variable delay = 3000 on line 9 and use that in line 10 to set the delay on the timer. You then set the loopslot to 0 again and call restart on the timer. This means that the timer now cleans out any pending events and starts with the new value of 3000.

You could have some fun with this class.

Do like this.

Add the interface java.awt.event.MouseWheelListener to your FotbollsPlan class.

Add addMouseWheelListener(this); on line 28, under the mouse listener.

Add this method to the class:



You could comment out the if-loopslot section in actionPerformed if you want to.





Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Sorry, but I can't do that(use mouseWheelMoved(MouseWheelEvent e). The program

itself is supposed to stop in even intervals. So, when I do setDelay(3000) the

program should change to this. And it does, but if I set it back to 50 ms with

setDelay(50) it doesn't take the first setDelay(3000), it stays at 50.


__
--
--
--

rita : 2
loopslot: 31
DELAY: 50
loopslot: 39
DELAY: 50
loopslot: 49
DELAY: 50
rita : 2
loopslot: 32
DELAY: 50
loopslot: 40
DELAY: 50
loopslot: 50
XXXX - if sats: 0
DELAY: 3000
rita : 2
loopslot: 33
DELAY: 50
loopslot: 41
DELAY: 50
loopslot: 1
DELAY: 3000
rita : 2
loopslot: 34
DELAY: 50
loopslot: 42
DELAY: 50
loopslot: 35
DELAY: 50
loopslot: 43
- - -
---
- -
and so on ....

loopslot: 48
DELAY: 50
loopslot: 49
DELAY: 50
loopslot: 50
XXXX - if sats: 0
DELAY: 3000
loopslot: 1
DELAY: 3000
loopslot: 2
DELAY: 3000
rita : 2
loopslot: 2
DELAY: 3000
loopslot: 2
DELAY: 3000
loopslot: 3
DELAY: 3000

I stopped here..


What I see is that if I for example add 2 balls to the

footballfield I see that I have 2 + 1 = 3 events going on. And when loopslot = 50
only one event changes to 3000? Sholudn't all events change to 3000? I mean it's

one timer-object that I've changed to 3000. But only one loop gets affected.

There is something here that I don't understand.

Do you?

Anders
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Yes, I do... It is doing exactly what you tell it to do.

I have commented in the code.



If we write down, in plain swenglish, what we want to do in the code would look like this:



I always write a "story" in comments in my methods before I start writing the program. That way it is easier to get the right logic. If you can't write it in plain english (eller på svenska för den delen) then it is hard to write it in code. At least initially.
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Thank you for putting some effort into my problem. But I'm sorry, this doesn't work out. I don't think that I've explained the issue properly.
At evenly intervalls(jämna mellanrum) the footballfield shall be updated and all the balls shall be painted out with a piece of text that shows how many balls that we have on the field for the moment.
( We don't have to bother about the text now.)
So, for a small period of time , say 3000ms, the balls will stand still and thereafter they will start bouncing again, i e the delay will go back to 50 ms. But they don't do that now. If I set the delay back to 50 ms the program doesn't take the 3000 ms.
And if I read your code it looks fine, but it doesn't work the way I want it to do.


So, the code stays at 3000 ms when it should jump back to 50, why?
For every ball that I create, there will be an event. Maybe I have to change more into the code?

Anders
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

I think the problem is in the way you interpret the logic. When you are in the actionPerformed-method, that is were you call the repainting of the balls. When you set the timer delay to something else, you are only setting the intervals at where you trigger the actionPerformed to be called.

The question now is if you have multiple timers or multiple actionPerformed?

Could you post the complete code as it is now again??

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Hello, again!

Here is the code:
class FotBoll

class FotBollsPlan


main class



Hopefully you can find something, I just feel totaly stuck here,
Anders
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
So, I managed to solve it. I googled it - it is kind of a simple answer. But I do not understand why the setDelay method doesn't work. ??
Here is the code:


Thanks a lot for putting your time into this.

Anders
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Don't ever sleep on the EDT; read Concurrency in Swing for more information.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
OK, but I do not understand how to do this, since I just couldn't get this to work with setDelay and the other swing timer functions. Thank you anyway. Or do you have a suggestion how to do it?

Anders
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NPE when trying to get size() in ArrayList