Why does the compiler not run the program even tho

2019-07-28 03:20发布

问题:

I have a program that is not showing any errors in Eclipse, but whenever I click the run button, nothing happens. I get the code lines in the compiler console that I have attached below.

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
      }
   }

This next block is the console output for Eclipse when I try to run the program.

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:

You have a NullPointerException at line 61 in Transaction.java.

I suspect you need to initialize SummaryMenuItem before doing File.add(SummaryMenuItem);.