Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

SOME1 HELP ME URGENT- NullPointerException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to work with JTabbedPane but I have struck a dead. My code compiles ok but when it runs it gives me a nullpointer exception and I can't seem to solve this one...Please help for I am desperate
I would really appreciate any help that I can get.
Aaron
THE CODE::
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Tab1 extends JPanel
{
JPanel viewPnl,dataPnl,xtraPnl;
JPanel addPnl,infoPnl,controlPnl;
JList ticketLst;
JLabel seatLbl,seatData,nameLbl,nameData, priceLbl,priceData;
JCheckBox priorityChk,bsnClassChk;
JButton deleteBtn,addBtn,cancelBtn;
JScrollPane ticketLstScrl;
JTextField seatTxtFld,nameTxtFld,priceTxtFld;
public Tab1()
{
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
//make the view panel and its various components
//using JComponent since addTab() method of JTabbedPane takes
//takes JComponent as its arguments
JComponent viewpanel = makePanel();
ticketLst = new JList ();
seatLbl = new JLabel("Seat No:");
seatData = new JLabel();
nameLbl = new JLabel("Name:");
nameData = new JLabel();
priceLbl = new JLabel("Price:");
priceData = new JLabel();
dataPnl= new JPanel();
dataPnl.setLayout(new GridLayout(3,2));
dataPnl.add(seatLbl);
dataPnl.add(seatData);
dataPnl.add(nameLbl);
dataPnl.add(nameData);
dataPnl.add(priceLbl);
dataPnl.add(priceData);
dataPnl.add(xtraPnl);
priorityChk = new JCheckBox("Priority");
deleteBtn = new JButton("Delete");
xtraPnl = new JPanel();
xtraPnl.setLayout(new GridLayout(1,2));
xtraPnl.add(priorityChk);
xtraPnl.add(deleteBtn);
viewPnl.setLayout(new BorderLayout());
viewPnl.add(ticketLst,BorderLayout.EAST);
viewPnl.add(dataPnl,BorderLayout.CENTER);
viewPnl.add(xtraPnl,BorderLayout.SOUTH);
tabbedPane.addTab("View Panel", viewpanel);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_V);
JComponent addpanel = makePanel();
seatTxtFld = new JTextField(30);
nameTxtFld = new JTextField(30);
priceTxtFld = new JTextField(30);
infoPnl = new JPanel();
infoPnl.setLayout(new GridLayout(3,2));
infoPnl.add(seatLbl);
infoPnl.add(seatTxtFld);
infoPnl.add(nameLbl);
infoPnl.add(nameTxtFld);
infoPnl.add(priceLbl);
infoPnl.add(priceTxtFld);
bsnClassChk = new JCheckBox("Business Class");
addBtn = new JButton("Add");
cancelBtn = new JButton("Cancel");
controlPnl = new JPanel();
controlPnl.setLayout(new GridLayout(2,2));
controlPnl.add(bsnClassChk);
controlPnl.add(addBtn);
controlPnl.add(priorityChk);
controlPnl.add(cancelBtn);
addPnl = new JPanel();
addPnl.setLayout(new GridLayout(2,1));
addPnl.add(infoPnl);
addPnl.add(controlPnl);
tabbedPane.addTab("Add",addpanel);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_A);
//Add the tabbed pane to this panel.
add(tabbedPane);
//Uncomment the following line to use scrolling tabs.
//tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private JComponent makePanel()
{
JPanel panel = new JPanel();
return panel;
}
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("Tab1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new Tab1();
newContentPane.setOpaque(true); //content panes must be opaque
frame.getContentPane().add(new Tab1(),
BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick! it's URGENT!!!
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to do would be to look at the error message. It will most likely include a file name and line number. Look at that line in your file; one of the variables used on that line is null, simple as that.
As far as getting people here to help you -- nobody's going to read all that ugly code. If you're still stuck after looking at the stack trace, then you could paste it into a new message in this thread and (this is important!) tell us which line in the original message is that one named in the stack trace.
Finally, please realize that, although your problem is important to you, everyone thinks their problem is urgent. Saying "URGENT" in your subject line actually just turns people off.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the exception:

So count down 38 lines in your code and you find this line:

You could comment it out to check... NullPointer means "we can't find it..."
Java is saying there is no xtraPnl. Did you forget to instantiate the variable?
 
AaronMweu Munzaa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hill and Wood for your advice and HELP [really appreciated]
My code now runs ok: Just in case you are interested - the problem was that I was adding the components into panels in the wrong order - I havent added anything I simply rearranged the order:
BELOW is the working code - HUUUUURRRRAAAAAYYY and thanks for the help once again!!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Tab1 extends JPanel
{
JPanel viewPnl,dataPnl,xtraPnl;
JPanel addPnl,infoPnl,controlPnl;
JList ticketLst;
JLabel seatLbl,seatData,nameLbl,nameData, priceLbl,priceData;
JCheckBox priorityChk,bsnClassChk;
JButton deleteBtn,addBtn,cancelBtn;
JScrollPane ticketLstScrl;
JTextField seatTxtFld,nameTxtFld,priceTxtFld;
public Tab1()
{
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
//make the view panel and its various components
//using JComponent since addTab() method of JTabbedPane takes
//takes JComponent as its arguments
JComponent viewpanel = makePanel();
ticketLst = new JList ();
seatLbl = new JLabel("Seat No:");
seatData = new JLabel();
nameLbl = new JLabel("Name:");
nameData = new JLabel();
priceLbl = new JLabel("Price:");
priceData = new JLabel();
priorityChk = new JCheckBox("Priority");
deleteBtn = new JButton("Delete");
xtraPnl = new JPanel();
xtraPnl.setLayout(new GridLayout(1,2));
xtraPnl.add(priorityChk);
xtraPnl.add(deleteBtn);
dataPnl= new JPanel();
dataPnl.setLayout(new GridLayout(3,2));
dataPnl.add(seatLbl);
dataPnl.add(seatData);
dataPnl.add(nameLbl);
dataPnl.add(nameData);
dataPnl.add(priceLbl);
dataPnl.add(priceData);
dataPnl.add(xtraPnl);
viewPnl = new JPanel();
viewPnl.setLayout(new BorderLayout());
viewPnl.add(ticketLst,BorderLayout.EAST);
viewPnl.add(dataPnl,BorderLayout.CENTER);
viewPnl.add(xtraPnl,BorderLayout.SOUTH);
tabbedPane.addTab("View Panel", viewpanel);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_V);
JComponent addpanel = makePanel();
seatTxtFld = new JTextField(30);
nameTxtFld = new JTextField(30);
priceTxtFld = new JTextField(30);
infoPnl = new JPanel();
infoPnl.setLayout(new GridLayout(3,2));
infoPnl.add(seatLbl);
infoPnl.add(seatTxtFld);
infoPnl.add(nameLbl);
infoPnl.add(nameTxtFld);
infoPnl.add(priceLbl);
infoPnl.add(priceTxtFld);
bsnClassChk = new JCheckBox("Business Class");
addBtn = new JButton("Add");
cancelBtn = new JButton("Cancel");
controlPnl = new JPanel();
controlPnl.setLayout(new GridLayout(2,2));
controlPnl.add(bsnClassChk);
controlPnl.add(addBtn);
controlPnl.add(priorityChk);
controlPnl.add(cancelBtn);
addPnl = new JPanel();
addPnl.setLayout(new GridLayout(2,1));
addPnl.add(infoPnl);
addPnl.add(controlPnl);
tabbedPane.addTab("Add",addpanel);
tabbedPane.setMnemonicAt(1, KeyEvent.VK_A);
//Add the tabbed pane to this panel.
add(tabbedPane);
//Uncomment the following line to use scrolling tabs.
//tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private JComponent makePanel()
{
JPanel panel = new JPanel();
return panel;
}
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("Tab1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new Tab1();
newContentPane.setOpaque(true); //content panes must be opaque
frame.getContentPane().add(new Tab1(),
BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!