Forums Register Login

how to create an object array? not Object

+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
thanks for the replies..... Sure, from the next time i ll use the code BUTTON.

+Pie Number of slices to send: Send
 

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???
+Pie Number of slices to send: Send
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.
no wonder he is so sad, he hasn't seen this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1126 times.
Similar Threads
RandomAccessFile Modify Help
Explain Working of compareTo() method with respect to this example
parsing data and storing in the xml
Question reg. Pass By Value
Generic type casting
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:26:14.