• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame...  RSS feed

 
AaronMweu Munzaa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a GUI with vaarious components like JButtons, JLabels among others and I have correctly put these in JPanels and my code compiles OK. But when I run the application it shows a NullPointerException in a line where I am adding a button to the JPanel...I have cross checked and my button is ok declared and initialised as required but I just cant seem to get the root of the problem...Oh I am also setting a titled border on the panels...Any info or advice would go a long way to help a distressed me..

The code is down here just in case of need for ref: I dont expect any1 to go thru the ugly 10s of lines of codes...but if u have the time I would really appreciate::

Distressed ME


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class WaitingCustomerDriverGUI extends JFrame
{
//declare the components

JLabel nameLbl,telLbl,vipWaitingLbl,totalWaitingLbl;

JButton nextBtn,addBtn;
JTextField nameTxt,telTxt,vipCustomerTxt;
JTextField emptyTxt,vipWaitingTxt,totalWaitingTxt;
JPanel allPnl,controlPnl,displayPnl;

WaitingCustomerDriverGUI()
{
//initialize the components
nameLbl = new JLabel("Name:");
telLbl = new JLabel("Telephone:");
vipWaitingLbl = new JLabel("VIPs Waiting:");
totalWaitingLbl = new JLabel("Total Waiting:");

nameTxt = new JTextField(30);
telTxt = new JTextField(30);
vipWaitingTxt = new JTextField(30);
totalWaitingTxt = new JTextField(30);
vipCustomerTxt = new JTextField(30);
emptyTxt = new JTextField(30);

allPnl= new JPanel();
controlPnl = new JPanel();
displayPnl = new JPanel();

//set borders,layouts and add components
// to the panels
displayPnl.setLayout(new GridLayout(2,3));
displayPnl.setBorder(
BorderFactory.createTitledBorder("Current Customer"));
displayPnl.add(nameLbl);
displayPnl.add(nameTxt);
displayPnl.add(emptyTxt);
displayPnl.add(telLbl);
displayPnl.add(telTxt);
displayPnl.add(vipCustomerTxt);

controlPnl.setLayout(new GridLayout(2,3));
controlPnl.setBorder(
BorderFactory.createTitledBorder("Queue Functions"));
controlPnl.add(vipWaitingLbl);
controlPnl.add(vipWaitingTxt);
controlPnl.add(nextBtn);
controlPnl.add(totalWaitingLbl);
controlPnl.add(totalWaitingTxt);
controlPnl.add(addBtn);

allPnl.setLayout(new GridLayout(2,0));
allPnl.add(displayPnl);
allPnl.add(controlPnl);

}
public static void main(String[]args)
{
WaitingCustomerDriverGUI w = new WaitingCustomerDriverGUI();
w.pack();
w.show();
}

}
 
Gary Down
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you need to instantiate (initialize in your words) the JButtons after you declare them and befoe you try to add them to you JPanel.

Gary.
[ May 17, 2004: Message edited by: Gary Down ]
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have not initialized "nextBtn" class variable.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!