This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Populating panels with buttons using loops.  RSS feed

 
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.
 
Bartender
Posts: 9559
12
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
 
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
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!