Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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 728 times)
0 Members and 1 Guest are viewing this topic.
Offline JESTERRRRRR

Senior Devvie


Medals: 7
Exp: 1 year



« 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: 355
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
Offline JESTERRRRRR

Senior Devvie


Medals: 7
Exp: 1 year



« 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (23 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (69 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!