• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array of objects

 
feroz muhamed
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Montalvo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, this:
for(int i=0;1<5;i++){
will go into an infinite loop. should be i<5.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic