PDA

View Full Version : Java Issue


Shattered Skies
11-26-2006, 10:27 AM
Okay, Ive been up for way too long and don't feel like staring more at this code trying to figure out why its not compiling correctly for me. So, anyone care to see why Im getting these errors:


init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.NullPointerException
at JNotePad.MenuBar.createFileMenu(MenuBar.java:84)
at JNotePad.MenuBar.<init>(MenuBar.java:15)
at JNotePad.MenuBar.main(MenuBar.java:104)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)



With this code:


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

public class MenuBar extends JFrame
{
private JMenuBar menubar;

public MenuBar()
{
setTitle("Test");

createFileMenu();
// createEditMenu();
// createHelpMenu();

setJMenuBar(menubar);

setSize(400,400);
setVisible(true);
}

private void createFileMenu()
{
JMenu menu = new JMenu("File"); // Create File menu.
menu.setMnemonic(KeyEvent.VK_F); // Underlined F

JMenuItem menuItem = new JMenuItem("Save", null);
menuItem.setMnemonic(KeyEvent.VK_S); // Underline S

menuItem.setAccelerator(KeyStroke.getKeyStroke(Key Event.VK_S, Event.CTRL_MASK));
menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDialog("Are you sure you want to save?", "Save?",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
});
menu.add(menuItem);

menuItem = new JMenuItem("Save As...", null);
menuItem.setMnemonic(KeyEvent.VK_A);

menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDialog("Are you sure you wish to save all?", "Save All",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
});
menu.add(menuItem);
menu.addSeparator();

menuItem = new JMenuItem("Delete", null);
menuItem.setMnemonic(KeyEvent.VK_D);

menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDialog("Are you sure you want to delete? Information cant be recovered!", "Delete?",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
});
menu.add(menuItem);
menu.addSeparator();

menuItem = new JMenuItem("Exit", null);
menuItem.setMnemonic(KeyEvent.VK_X);

menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDialog("Are you sure you want to exit?", "Exit",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
});
menu.add(menuItem);
menubar.add(menu);
}
/*
private void createEditMenu()
{

}

private void createHelpMenu()
{

}
*/
public void showDialog(String message, String title, int option, int icon)
{
JOptionPane.showInternalConfirmDialog(null, message, title, option, icon);
}

public static void main(String[] args)
{
MenuBar mb = new MenuBar();
mb.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}


As simple as the program is I cant figure it out. Meh.. sleep sucks.

ractoc
11-26-2006, 02:38 PM
Very simple:
You define an instance variable called menuBar. You don't initialise it though. But you DO call a member method on it on line 84.

Shattered Skies
11-27-2006, 01:24 PM
Eh, I knew lack of sleep would kill me. I just changed:

private JMenuBar menubar;


to


private JMenuBar menubar = new JMenuBar();


and the thing loaded up fine. Now its just a matter of getting the rest of the errors worked out when clicking on something in the menu.

Glad Im not doing anything at work today.

Thanks :)

Renamation
01-03-2007, 08:43 AM
Very simple:
You define an instance variable called menuBar. You don't initialise it though. But you DO call a member method on it on line 84.

yes.. this is correct, i found that out myself