为什么即使没有任何错误,编译器也不会运行程序?

时间:2011-05-07 07:08:40

标签: java swing jmenubar jmenu

我有一个程序在Eclipse中没有显示任何错误,但每当我点击运行按钮时,都没有任何反应。我在下面附带的编译器控制台中获得了代码行。

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

import java.awt.event.*;

public class Transaction extends JFrame {

private static final long serialVersionUID = 1L;
JFrame frame = new JFrame("Bank Account - S Standridge");
JMenuBar MenuBar;
JMenu File = new JMenu("File");
JMenu Edit = new JMenu("Edit");
JMenu About = new JMenu("About");
JMenuItem Transaction = new JMenuItem("Transaction");
JMenuItem Summary = new JMenuItem("Summary");
JMenuItem Exit = new JMenuItem("Exit");
private JPanel MenuPanel;
private JPanel TransactionPanel;
private JPanel ButtonPanel;
private JButton calcButton;    
private JButton exitButton; 
private JMenuItem SummaryMenuItem;
private JMenuItem AboutMenuItem;
private JMenuItem ExitMenuItem;

public Transaction() {
    setTitle("Bank Account - S Standridge");

    MenuPanel = new JPanel();
    TransactionPanel = new JPanel();
    ButtonPanel = new JPanel();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new BorderLayout());

    MenuPanel();        
    TransactionPanel();
    BuildButtonPanel();     

    add(MenuPanel, BorderLayout.NORTH);
    add(TransactionPanel, BorderLayout.WEST);
    add(ButtonPanel, BorderLayout.SOUTH);

    pack();
    setVisible(true);
}

private void MenuPanel() {

    MenuBar = new JMenuBar();

    frame.setJMenuBar(MenuBar);
    frame.setVisible(true);

    MenuBar.add(File);
    MenuBar.add(Edit);
    MenuBar.add(About);

    File.add(Transaction);
    File.add(SummaryMenuItem);
    File.add(ExitMenuItem);

    SummaryMenuItem.addActionListener(new SummaryMenuListener());
    AboutMenuItem.addActionListener(new AboutMenuListener());
}

private void BuildButtonPanel() {

     // Create a panel for the buttons.
      ButtonPanel = new JPanel();

      // Create the buttons.
      calcButton = new JButton("Calculate");
      exitButton = new JButton("Exit");

      // Register the action listeners.
      calcButton.addActionListener(new CalcButtonListener());
      exitButton.addActionListener(new ExitButtonListener());

      // Add the buttons to the button panel.
      ButtonPanel.add(calcButton);
      ButtonPanel.add(exitButton);

}

private void TransactionPanel()
{
    setLayout(new FlowLayout());

    JRadioButton b1 = new JRadioButton("A");
    // b1.addActionListener(this);
    add(b1);

    JRadioButton b2 = new JRadioButton("B");
    // b2.addActionListener(this);
    add(b2);



    ButtonGroup bg = new ButtonGroup();
    bg.add(b1);
    bg.add(b2);


    JTextField tf = new JTextField(5);
    add(tf);
  }

}

class CalcButtonListener implements ActionListener
    {
       public void actionPerformed(ActionEvent e)
   {

   }
}

class SummaryMenuListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {

      }
   }

class ExitButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
          System.exit(0);
      }
   }

class AboutMenuListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
       // Displays Message Box
      }
   }

当我尝试运行程序时,下一个块是Eclipse的控制台输出。

java.lang.reflect.InvocationTargetException
IWAV0052E Invocation Target Exception creating Transaction
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.eclipse.ve.internal.java.vce.launcher.remotevm.JFCLauncher$1.run(JFCLauncher.java:59)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JPopupMenu.add(Unknown Source)
at javax.swing.JMenu.add(Unknown Source)
at Transaction.MenuPanel(Transaction.java:61)
at Transaction.<init>(Transaction.java:37)
... 19 more

1 个答案:

答案 0 :(得分:6)

NullPointerException的第61行有Transaction.java

我怀疑您需要在执行SummaryMenuItem之前初始化File.add(SummaryMenuItem);