Forums Register Login

Populating panels with buttons using loops.

+Pie Number of slices to send: Send
Here are my three classes so so far:
BarTill.java

ChoicesPanel.java

SubChoicesPanel.java

The class that is giving me bother is SubChoicesPanel. I want to construct 7 panels adding buttons to each of these panels using the loop and apply the labels. If you look at the code for ChoicesPanel you will a slightly more simplified version of what I am trying to do here which works fine. Because this time I am using two-dimensional arrays (arrays of arrays strictly speaking) this time I am not quite getting the desired results. The code currently throws a NullPointerException.
Can any of you please help?
If I have not mentioned something that I should have please let me know!
Thanks, Dave.
+Pie Number of slices to send: Send
 

Originally posted by Dave Johnson:

If I have not mentioned something that I should have please let me know!


You neglected to show us the stack trace from that exception. If you look at the stack trace you will see that it has a class name and a line number on it. Look at that line of code and chances are there's a variable you forgot to initalize.
+Pie Number of slices to send: Send
Starting application C:\Documents and Settings\Dave Johnson\Desktop\BarTill\BarTill.class
java.lang.NullPointerException
at BarTill.init(BarTill.java:19)
at BarTill.main(BarTill.java:26)
Exception in thread "main"
Interactive Session Ended
+Pie Number of slices to send: Send
You have not initialized the SubChoicesPanel array. You decalred it, but did not initialize it. Hence you are getting a NullPointerException when trying to reference scp[i] = ....
Kyle
+Pie Number of slices to send: Send
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class BarTill extends JApplet
{
ChoicesPanel panel1;
SubChoicesPanel scp[];
public void init()
{
FlowLayout fl=new FlowLayout(FlowLayout.LEFT);
Container contentArea=getContentPane();
contentArea.setLayout(fl);
setBackground(new Color(150,0,0));
panel1=new ChoicesPanel();
getContentPane().add(panel1);
scp=new SubChoicesPanel[7];
for(int i=0;i<7;i++)
{
scp[i]=new SubChoicesPanel(i);
}
getContentPane().add(scp[1]);
}
public static void main(String arg[])
{
BarTill bt=new BarTill();
bt.init();
JFrame f=new JFrame();
f.setBackground(new Color(200,0,0));
f.setSize(1000,430);
f.setContentPane(bt.getContentPane());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.show();
}
}
Just been playing with the code. I had used almost the same earlier. Not to worry I got there in the end.
Thanks for the help
It's feeding time! Give me the food you were going to give to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1048 times.
Similar Threads
JLabel for more than 1 line of text!
I'm stuck!
Some buttons do... Some buttons don't...
Tally will not add up
GUI nightmare
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:53:57.