wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to create an object array? not Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to create an object array? not Object" Watch "how to create an object array? not Object" New topic
Author

how to create an object array? not Object

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37938
    
  22
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

Joined: Aug 27, 2008
Posts: 104
thanks for the replies..... Sure, from the next time i ll use the code BUTTON.

Vinney Shanmugam
Ranch Hand

Joined: Aug 27, 2008
Posts: 104
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

Joined: Feb 08, 2009
Posts: 597

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.

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
wood burning stoves
 
subject: how to create an object array? not Object
 
Similar Threads
Generic type casting
Explain Working of compareTo() method with respect to this example
RandomAccessFile Modify Help
parsing data and storing in the xml
Question reg. Pass By Value