Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Norm

Thread: Problem in assigning randomed buttons

  1. #1
    thesonyuh is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Problem in assigning randomed buttons

    Hello There!!
    I'm just a beginner so i would need your help with the
    Java Code:
    JButton x []= new JButton [12];
    	int [] arr=new int[15];
    	
        public Bahay_Kubling() {
        	setLayout(new GridLayout(3,4));
        	int randomNumbers = 12;
        	Container con=getContentPane();
    		ArrayList<Integer> numbers = new ArrayList<Integer>();
    		int number = (int)(Math.random() * randomNumbers);
    		numbers.add(number);
    		for(int i = 0; i < randomNumbers-1; i++){
    			do{
    				number = (int)(Math.random() * 12);
    				
    			}while(numbers.indexOf(number)!=-1);
    			numbers.add(number);
    		}
    		for(Integer i: numbers){
    			System.out.println(i);
    			con.add(x[i]);
    			x[i].addActionListener(this);
    		}
    			
    		
        	setTitle("Bahay Kubling Matching Game");
        	setSize(300,400);
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	setLocationRelativeTo(null);
        	setVisible(true);
    I think I'm done with randoming unique numbers but the problem is assigning it to the buttons. I'm having error during runtime supposedly.. These is the error:
    Java Code:
    11
    Exception in thread "main" java.lang.NullPointerException
        at java.awt.Container.addImpl(Container.java:1086)
        at java.awt.Container.add(Container.java:410)
        at Bahay_Kubling.<init>(Bahay_Kubling.java:35)
        at Bahay_Kubling.main(Bahay_Kubling.java:54)
    Thanks in advance.. Hope you can help me.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Problem in assigning randomed buttons

    I did not see where you initialize JButtons.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default Re: Problem in assigning randomed buttons

    11
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1086)
    at java.awt.Container.add(Container.java:410)
    at Bahay_Kubling.<init>(Bahay_Kubling.java:35)
    At line 35 you call the add method with a null value. Check that line in your code to see what the problem is.
    DarrylBurke likes this.

  4. #4
    thesonyuh is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Problem in assigning randomed buttons

    Quote Originally Posted by mine0926 View Post
    I did not see where you initialize JButtons.
    Sorry for late reply..
    It's here:
    Java Code:
    public class Bahay_Kubling extends JFrame implements ActionListener{
    	JButton x []= new JButton [12];
    	int [] arr=new int[15];

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default Re: Problem in assigning randomed buttons

    Java Code:
    JButton x []= new JButton [12];
    That creates an array with 12 empty slots. It does not put any objects in the slots.
    You need to assign them all with valid objects.

  6. #6
    thesonyuh is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Default Re: Problem in assigning randomed buttons

    Quote Originally Posted by Norm View Post
    At line 35 you call the add method with a null value. Check that line in your code to see what the problem is.
    Thanks for the reply..
    Line 35 is :
    con.add(x[i]);
    I don't know whats wrong.. please help.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default Re: Problem in assigning randomed buttons

    What is the value of x[i]? Is it null?
    See post#5

  8. #8
    thesonyuh is offline Member
    Join Date
    Sep 2011
    Posts
    7
    Rep Power
    0

    Thumbs up Re: Problem in assigning randomed buttons

    Quote Originally Posted by Norm View Post
    What is the value of x[i]? Is it null?
    See post#5
    Ok. I got it.! Thanks Mod..
    I just added
    Java Code:
    x[i] = new JButton("1", new ImageIcon ("stan.jpg"));
    before adding it to container..
    Thanks a lot.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default Re: Problem in assigning randomed buttons

    That should work better.

Similar Threads

  1. Buttons Problem
    By Coold0wn in forum New To Java
    Replies: 3
    Last Post: 08-25-2011, 07:02 PM
  2. Renaming Buttons Problem
    By Huskies in forum New To Java
    Replies: 11
    Last Post: 07-29-2011, 01:53 AM
  3. Array Index Out Of Bounds and Problem in Assigning Values
    By chronoz1300 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 08:14 PM
  4. Replies: 3
    Last Post: 11-24-2008, 08:01 PM
  5. [B]Tab Sequence problem with radio buttons[/B]
    By shobha2k8 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-25-2008, 08:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •