Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (710)
Games in Android Showcase (212)
games submitted by our members
Games in WIP (784)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  extends Thread null pointer  (Read 1030 times)
0 Members and 1 Guest are viewing this topic.
« Posted 2011-12-31 04:17:42 »

I created an inner class, that extends Thread. Pretty simple class, I override the run method, which does what I want. However I stored objects of this type in an array, BUT I forgot to do the arrayname = new objectname[10]; or whatever number I wanted. I just declared objectname[] arrayname;

So obviously I should get a null pointer exception if I try arrayname[0] = new objectname(args);

The constructor for the inner class had start(); in it so it began as soon as I created it. With this in the constructor, I still get the exception, but it begins running the Thread anyway??? How can it do this? If I take the start() out of the constructor, and try to do arrayname[0].start(); it does not work as expected. What do I not know about? Just curious, it's not caused any problems.
Offline sproingie

JGO Kernel

Medals: 202

« Reply #1 - Posted 2011-12-31 05:11:30 »

I'm not sure what led you to try to start one of the objects in the array rather than the instance of the thread you created.  Regardless, take the start out of the constructor -- constructors should not be starting new threads or doing anything other than initializing a new instance.  Secondly, don't subclass Thread, use the Thread(Runnable) constructor instead.

Offline ra4king

JGO Kernel

Medals: 471
Projects: 3
Exp: 5 years

I'm the King!

« Reply #2 - Posted 2011-12-31 06:15:28 »

That's because your object is created first THEN it attempts to set it at that index. So your constructor is called, control returns to the "arrayname[0] = ...." line, and a NPE is thrown.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
« Reply #3 - Posted 2011-12-31 16:35:40 »

Oh I see, thanks  ra4king.

Why is that a preferred way sproingie? I've seen it in many books so just picked it up from there
Offline BoBear2681

JGO Coder

Medals: 19

« Reply #4 - Posted 2011-12-31 17:39:43 »

The idea is that you're not changing the behavior of a Thread, just giving it something to run.  In practice it often makes no difference; it's a design thing.
Offline sproingie

JGO Kernel

Medals: 202

« Reply #5 - Posted 2011-12-31 20:01:36 »

One big reason you should subclass Runnable instead of Thread, other than mere aesthetics, is that it's much easier to put your Runnables into a java.util.concurrent.ExecutorService later on.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

numerical (80 views)
2017-02-21 07:32:16

numerical (81 views)
2017-02-21 07:31:46

theagentd (185 views)
2017-02-18 13:42:33

theagentd (188 views)
2017-02-18 13:35:16

h.pernpeintner (1351 views)
2017-01-24 22:39:11

h.pernpeintner (1339 views)
2017-01-24 22:38:32

Galdo (1900 views)
2017-01-12 13:44:09

Archive (1986 views)
2017-01-02 05:31:41

0AndrewShepherd0 (2522 views)
2016-12-16 03:58:39

0AndrewShepherd0 (2311 views)
2016-12-15 21:50:57
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!