编译器也不会运行程序

编译器也不会运行程序

本文介绍了为什么即使没有任何错误,编译器也不会运行程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序在Eclipse中没有显示任何错误,但是每当我单击运行按钮时,什么都不会发生.我在下面附加的编译器控制台中找到了代码行.

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

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

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

推荐答案

您在Transaction.java的第61行有一个NullPointerException.

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

我怀疑您需要先进行SummaryMenuItem初始化,然后再执行File.add(SummaryMenuItem);.

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

这篇关于为什么即使没有任何错误,编译器也不会运行程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:24