PDA

View Full Version : Nullpointer



mavvy
11-26-2008, 11:19 PM
Hi people, I'm writing some code for a program which displays pictures. I've written a few classes and all seems fine but when I run it, this exception come up

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1019)
at java.awt.Container.add(Container.java:899)
at finalproject.MorphMainFrame.<init>(MorphMainFrame.java:56)
at finalproject.MorphMainFrame.main(MorphMainFrame.java:70)

this is my code:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

/**
*
*/
public class MorphMainFrame extends JFrame {

private JPanel phasePanel;
private MenuBar menuBar;
private ImagePanel imagePanel;
static JPanel contentPane;
private JPanel picture1;
private JPanel picture2;

/** Creates a new instance of MorphMainFrame */
public MorphMainFrame() {

Border blackLine = BorderFactory.createLineBorder(Color.black);
BorderLayout b = new BorderLayout();
setLayout(b);

imagePanel = new ImagePanel(this, 700, 390);

//Create a panel and make it the content pane.
contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createRaisedBevelBorder());

//create panel to show original pictures
picture1 = new JPanel();
picture1.setBackground(Color.YELLOW);
picture1.setBorder(BorderFactory.createLoweredBevelBorder());
picture1.setPreferredSize(new Dimension(500, 1000));

picture2 = new JPanel();
picture2.setBackground(Color.GREEN);
picture2.setBorder(BorderFactory.createLoweredBevelBorder());
picture2.setPreferredSize(new Dimension(500, 1000));

//add menu bar
menuBar = new MenuBar(this);
contentPane.add(menuBar);

contentPane.add(phasePanel, BorderLayout.SOUTH);
contentPane.add(picture1, BorderLayout.WEST);
contentPane.add(picture2, BorderLayout.EAST);

contentPane.add(phasePanel);
createToolbar();
pack();

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

JFrame frame = new MorphMainFrame();
//UIManager.setLookAndFeel(napkinlaf);
//SwingUtilities.updateComponentTreeUI(frame);
//frame.add(new RunProgram());
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setJMenuBar(bar);
frame.setContentPane(contentPane);
frame.setVisible(true);
}

Hope you can do better than I have.Thanks :)

imaginationac
01-28-2009, 07:46 PM
It's kind of hard to debug because you're using some a class, ImagePanel, which you haven't provided. Obviously, I can't try compiling it. Also, please use the code tags next time (the # symbol).

There's a couple of things to note though:
1. I'm curious as to why you would choose to use a MenuBar rather than a JMenuBar. Either way, the constructor for both has no parameters. The statement you have on line 42 should be:

menuBar = new MenuBar();
2. You can't add a MenuBar to a JPanel. You can set the MenuBar to the JFrame by using the setMenuBar(MenuBar menuBar) method. Line 43 should look like this:

this.setMenuBar(menuBar);
Technically you could omit the "this" reference but it's good programming practice in my opinion.