• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create an object array? not Object

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class open extends Thread
{
String ag;
public void run()
{
System.out.println(" In Run method ");
String nam = getName();
System.out.println(nam);
ag = "$Z12J.#ZJMX";
open(ag);
}

public native void open(String procname);

static {
System.loadLibrary("plibopen");
}

public static void main(String args[])
{
open[] obj = new open[10];
for(int i=1;i<=10;i++)
obj[i].start();
}
}


When i run this, i m getting NullpointerException. How to create an object array???
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to manually create the objects to put into the array; i.e,



Note that array indices go from 0 up to length-1; the first array element is not number 1, and the last is not at the array length!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the CODE button; your posting is difficult to read.I think the problem is that although you have correctly initialised the array called obj, all its 10 references point to null until you assign them
 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies..... Sure, from the next time i ll use the code BUTTON.

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:You have to manually create the objects to put into the array; i.e,



Note that array indices go from 0 up to length-1; the first array element is not number 1, and the last is not at the array length!


Thanks.
i am able to run the program after making necessary changes..... one more doubt, now can i call this program as a multi threaded one, which spawns 10 threads???
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer you second question,

You can make the program multi threaded just the way you originally did it, by calling the start() method of the thread object.



By the way, if your only intention is to use the class open as a multi-threaded one and not actually give it thread behavior you should implement the Runnable interface rather than extending the Thread class.

The only reason to extend the Thread class is when you are customizing the Thread class to add some functionality, which you shouldn't be generally doing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic