aspose file tools*
The moose likes Java in General and the fly likes SOME1 HELP ME URGENT- NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SOME1 HELP ME URGENT- NullPointerException" Watch "SOME1 HELP ME URGENT- NullPointerException" New topic
Author

SOME1 HELP ME URGENT- NullPointerException

AaronMweu Munzaa
Greenhorn

Joined: Mar 04, 2004
Posts: 8
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();
}
});
}
}
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Quick! it's URGENT!!!


I've heard it takes forever to grow a woman from the ground
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Mar 04, 2004
Posts: 8
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();
}
});
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SOME1 HELP ME URGENT- NullPointerException
 
Similar Threads
JTabbed Pane: How to hide the frame containing the tabbed panes
JScrollPane inside JTabbedPane
Layout Problems
JTabbedPane with Keyevents
JSplitterView treats panes differently