Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HELP

0 views
Skip to first unread message

Sherrine

unread,
Jan 3, 2002, 12:17:04 AM1/3/02
to
I got the following error when I tried to run my file.

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at MainUI.<init>(MainUI.java:49)
at MainUI.main(MainUI.java:96)


It seems that the problem lies with my main()?
Attached is my code.
Can anyone advise me on what is wrong?
Thanks

============ MainUI.java ==================
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;

public class MainUI extends JFrame{

DriverOnePanel fdp;
DriverTwoPanel sdp;
PaymentPanel pp;
BookingPanel bp;
CarDetailsPanel cdp;

JButton Open, Save, Clear;

JLabel lOrderNo = new JLabel(" Order No.: ");
JTextField tOrderNo = new JTextField (14);

public MainUI() {

Date todayDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String sMyDate = formatter.format(todayDate);

//JButton Definitions
ImageIcon openIcon = new ImageIcon("folder05.gif");
ImageIcon saveIcon = new ImageIcon("diskett3.gif");
ImageIcon clearIcon = new ImageIcon("whiteout.gif");

//Specify the type of layout manager used
FlowLayout Layout = new FlowLayout();
Layout.setAlignment(FlowLayout.LEFT);

JPanel MainPanel = new JPanel(Layout);
JPanel button_Panel = new JPanel(Layout);

JButton open = new JButton ("Open", openIcon);
button_Panel.add(Open);

JButton save = new JButton(" Save ", saveIcon);
button_Panel.add(Save);

JButton clear = new JButton(" Clear ", clearIcon);
button_Panel.add(Clear);

button_Panel.add(lOrderNo);
button_Panel.add(tOrderNo);

JTabbedPane tabbedPane = new JTabbedPane();

fdp = new DriverOnePanel();
fdp.chkDriver1.setVisible(false);
tabbedPane.addTab("Hirer", null, fdp, "Hirer's Particulars");

sdp = new DriverTwoPanel();
tabbedPane.addTab("Second Driver", null, sdp, "Second Driver's
Particulars");

bp = new BookingPanel();
tabbedPane.addTab("Booking Information", null, bp, "Booking Information");

pp = new PaymentPanel();
tabbedPane.addTab("Payment Information", null, pp, "Payment Details");

cdp = new CarDetailsPanel();
tabbedPane.addTab("Car Details", null, cdp, "Car Details");

tabbedPane.setFont(new java.awt.Font("SansSerif", 1, 12));
setTitle("Swift Car Rental Pte Ltd");

getContentPane().setLayout(new BorderLayout());
MainPanel.add(button_Panel, BorderLayout.NORTH);
MainPanel.add(tabbedPane, BorderLayout.CENTER);
getContentPane().add(MainPanel);
setResizable(false);
pack();
setSize(500,500);
show();
}

public static void main(String[] args) {
try{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel
");
} catch(Exception e){}

MainUI UI = new MainUI();

UI.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});

}
}


Manfred Leonhardt

unread,
Jan 3, 2002, 10:32:30 AM1/3/02
to
Hi Sherrine,

No your problem lies in your MainUI constructor.

Regards,
Manfred.

Darryl Kerkeslager

unread,
Jan 3, 2002, 12:00:11 PM1/3/02
to
"Sherrine" <san...@pacific.net.sg> wrote in message
news:a10pko$d8k$1...@newsie.singa.pore.net...

> I got the following error when I tried to run my file.
>
> Exception in thread "main" java.lang.NullPointerException
> at java.awt.Container.addImpl(Unknown Source)
> at java.awt.Container.add(Unknown Source)
> at MainUI.<init>(MainUI.java:49)
> at MainUI.main(MainUI.java:96)
[snip]

>
> public class MainUI extends JFrame{
>
> DriverOnePanel fdp;
> DriverTwoPanel sdp;
> PaymentPanel pp;
> BookingPanel bp;
> CarDetailsPanel cdp;
>
> JButton Open, Save, Clear;
>
> JLabel lOrderNo = new JLabel(" Order No.: ");
> JTextField tOrderNo = new JTextField (14);
>
> public MainUI() {
>
[snip]

> JButton open = new JButton ("Open", openIcon);
> button_Panel.add(Open);
>
> JButton save = new JButton(" Save ", saveIcon);
> button_Panel.add(Save);
>
> JButton clear = new JButton(" Clear ", clearIcon);
> button_Panel.add(Clear);
>

When you see a NullPointerException on adding a component, it usually means
that one of the components that you thought you created, you in fact
probably declared it, but then did not instantiate the object. In this
case, it was the three JButtons.

The immediate cause of the problem is that these lines (and the similar
lines):

> JButton save = new JButton(" Save ", saveIcon);
> button_Panel.add(Save);

contain to differnt JButton objects: save, and Save.

The reason it compiled, rather than generating a compile error, is that you
declared this above:

> JButton Open, Save, Clear;

Rather than let it go at that, let me say that there are two reasons you had
this error that go beyond just a typo:

1. What to name my variables? Sun has naming conventions (Google: Sun
naming conventions) on how to name variables. This alone probably would
have helped you avoid the problem - and made it easier to debug. Only class
and interfac names should start with a capital letter; all other variables
should be lower case or mixed case, with no underscore character (there's
more detailed rules on Sun's site). Try to come up with a clear, consistent
naming method that adhere's to Sun's. For JComponents, I use descriptive
name+short component name, such as:

JPanel buttonPanel;
JLabel statusLabel:
JTextField displayText;

2. Where do I declare my variables? You need to be consistent about where
you declare Swing component variables so that YOU will always look in the
same place. Here, you've declared quite a few components outside of the
constructor, and some inside. There are good reasons for doing one or the
other, but I would avoid doing both.

Since your constructor is the only method that uses the components, you
could declare them all inside the constructor, which makes Information
Hiding happy. However, there's a good chance that you will add other
methods or inner classes that use the components - meaning you'd probably
want them OUTSIDE the constructor. My <could be faulty>personal
recommendation</could be faulty> would be to decalre all Swing componts at
the top of the class in one place.

Darryl Kerkeslager


0 new messages