Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Populating panels with buttons using loops.

 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kyle amburn
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!