aspose file tools*
The moose likes Beginning Java and the fly likes array of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array of objects" Watch "array of objects" New topic
Author

array of objects

feroz muhamed
Ranch Hand

Joined: Nov 15, 2003
Posts: 87
please give me the explanation why this code shows error']' needed
any other solution for this to create object like this.
code
-----------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class Fu extends Frame
{
public Fu()
{
setLayout(new FlowLayout());
for(int i=0;1<5;i++){
Button b[i]=new Button(""+i);
add(b[i]);
setSize( 400 , 400 );
setVisible( true );
}
}
public static void main( String[] args )
{
new Fu() ;
}
}
---------------------------------------------------------------------
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
You are trying to assign a new Button to an array which does not exist. You need to define your Button array outside the loop.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by feroz muhamed:
please give me the explanation why this code shows error']' needed
any other solution for this to create object like this.
code
-----------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class Fu extends Frame
{
public Fu()
{
setLayout(new FlowLayout());
for(int i=0;1<5;i++){
Button b[i]=new Button(""+i);
add(b[i]);
setSize( 400 , 400 );
setVisible( true );
}
}
public static void main( String[] args )
{
new Fu() ;
}
}
---------------------------------------------------------------------

Hi feroz, the syntax you posted to create an array is wrong (See Paul's) post.

You should create the array outside the loop as Paul suggested.
Hope this helps.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Dave Montalvo
Greenhorn

Joined: Sep 17, 2003
Posts: 5
also, this:
for(int i=0;1<5;i++){
will go into an infinite loop. should be i<5.
 
wood burning stoves
 
subject: array of objects