Results 1 to 9 of 9
- 09-17-2011, 10:57 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
Problem in assigning randomed buttons
Hello There!!
I'm just a beginner so i would need your help with theI 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: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);
Thanks in advance.. Hope you can help me.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)
- 09-17-2011, 11:02 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Re: Problem in assigning randomed buttons
I did not see where you initialize JButtons.
- 09-17-2011, 02:03 PM #3
Re: Problem in assigning randomed buttons
At line 35 you call the add method with a null value. Check that line in your code to see what the problem is.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)
- 09-18-2011, 12:38 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
- 09-18-2011, 12:39 AM #5
Re: Problem in assigning randomed buttons
That creates an array with 12 empty slots. It does not put any objects in the slots.Java Code:JButton x []= new JButton [12];
You need to assign them all with valid objects.
- 09-18-2011, 12:39 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
- 09-18-2011, 12:41 AM #7
Re: Problem in assigning randomed buttons
What is the value of x[i]? Is it null?
See post#5
- 09-18-2011, 03:24 AM #8
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
- 09-18-2011, 03:30 AM #9
Similar Threads
-
Buttons Problem
By Coold0wn in forum New To JavaReplies: 3Last Post: 08-25-2011, 06:02 PM -
Renaming Buttons Problem
By Huskies in forum New To JavaReplies: 11Last Post: 07-29-2011, 12:53 AM -
Array Index Out Of Bounds and Problem in Assigning Values
By chronoz1300 in forum New To JavaReplies: 2Last Post: 12-28-2009, 07:14 PM -
Rediculous problem?! Assigning a fraction to a double variable
By Tzoshirzup in forum New To JavaReplies: 3Last Post: 11-24-2008, 07:01 PM -
[B]Tab Sequence problem with radio buttons[/B]
By shobha2k8 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-25-2008, 07:25 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks