1

由于对 OP 代码进行了更改,这是一个后续问题。另一位用户建议我将另一个问题链接到这个问题。

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

我有一个可以编译但没有运行的代码。我试图让 GUI 运行,这样我就可以添加代码来执行我需要的功能。代码如下:

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 mp;
private JPanel tp;
private JPanel bp;
private JButton calcButton;    
private JButton exitButton; 
private JMenuItem summaryMenuItem;
private JMenuItem aboutMenuItem;
private JMenuItem exitMenuItem;

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

    mp = new JPanel();
    tp = new JPanel();
    bp = new JPanel();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new BorderLayout());

    menuPanel();        
    transactionPanel();
    BuildButtonPanel();     

    add(mp, BorderLayout.NORTH);
    add(tp, BorderLayout.WEST);
    add(bp, BorderLayout.SOUTH);

    pack();
    setVisible(true);
}

public void summary() {

}

private void menuPanel() {
    b
    menuBar = new JMenuBar();

    setJMenuBar(menuBar);
    setVisible(true);

    menuBar.add(file);
    menuBar.add(edit);
    menuBar.add(about);

    summaryMenuItem.addActionListener(new SummaryMenuListener());
    aboutMenuItem.addActionListener(new AboutMenuListener());

    file.add(transaction);
    file.add(summaryMenuItem);
    file.add(exitMenuItem);


}

private void BuildButtonPanel() {

     // Create a panel for the buttons.
      bp = 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.
      bp.add(calcButton);
      bp.add(exitButton);

}

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

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

    JRadioButton b2 = new JRadioButton("Savings");
    // 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
      }
   }

我在控制台中得到的错误如下:

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 Transaction.menuPanel(Transaction.java:64)
at Transaction.<init>(Transaction.java:37)
... 19 more
4

5 回答 5

7

好吧,这绝对不是您的实际代码,因为您b在方法开始时有一个流氓menuPanel。但是,您基本上需要查看堆栈跟踪:

Caused by: java.lang.NullPointerException
at Transaction.menuPanel(Transaction.java:64)
at Transaction.<init>(Transaction.java:37)

查看其中的那些行Transaction并找出空指针的位置。

看起来是这一行:

summaryMenuItem.addActionListener(new SummaryMenuListener());

您还没有初始化summaryMenuItem,所以当您尝试对其值调用方法时,您会遇到异常。

比修复这个特定的错误更重要的是学习如何修复这个错误:

  • 查看堆栈跟踪
  • 了解异常的含义
  • 如有必要,在调试器中运行代码,通常会在抛出未捕获的异常时暂停
  • 如果可能,编写一个单元测试,以便您可以自信地修复代码
于 2011-05-08T09:22:56.533 回答
3

Transaction.java 中第 64 行的内容为空

您必须设置菜单项。这 3 个不为空。

JMenuItem transaction = new JMenuItem("Transaction");
JMenuItem summary = new JMenuItem("Summary");
JMenuItem exit = new JMenuItem("Exit");

这3个是空的:

private JMenuItem summaryMenuItem;
private JMenuItem aboutMenuItem;
private JMenuItem exitMenuItem;

删除最后 3 个,并在代码中使用之前的 3 个。

您的 menuPanel 方法应如下所示:

private void menuPanel() {
    menuBar = new JMenuBar();

    setJMenuBar(menuBar);
    setVisible(true);

    menuBar.add(file);
    menuBar.add(edit);
    menuBar.add(about);

    summary.addActionListener(new SummaryMenuListener());
    //aboutMenuItem.addActionListener(new AboutMenuListener());

    file.add(transaction);
    file.add(summary);
    file.add(exit);
}

您的代码在更改后可以正常工作。

于 2011-05-08T09:22:21.913 回答
3

当代码编译时,它只是没有语法错误(告诉编译器如何做),但这并不意味着不存在语义错误(为了实现目标而做的事情)。

如果您仔细检查异常信息,您会在其底部发现:

Caused by: java.lang.NullPointerException
at Transaction.menuPanel(Transaction.java:64)
at Transaction.<init>(Transaction.java:37)

在 Transaction 类的 menuPanel 方法中使用了 Null 指针(可能是没有正确初始化的变量),该方法是从 transaction 的构造函数中调用的。

检查代码,您似乎实际上是在使用没有初始化的变量(即使用空值):

private JMenuItem summaryMenuItem;
private JMenuItem aboutMenuItem;
private JMenuItem exitMenuItem;

在 Transaction.menuPanel() 中:

private void menuPanel()
{
    //...
    summaryMenuItem.addActionListener(new SummaryMenuListener());
    aboutMenuItem.addActionListener(new AboutMenuListener());

    file.add(transaction);
    file.add(summaryMenuItem);
    file.add(exitMenuItem);
}
于 2011-05-08T09:26:56.067 回答
2

您在第 64 行收到 NullPointerException:

Caused by: java.lang.NullPointerException
at Transaction.menuPanel(Transaction.java:64)

这是那行:

summaryMenuItem.addActionListener(new SummaryMenuListener());

看起来summaryMenuItemnull,它似乎没有被设置为任何值。

于 2011-05-08T09:27:12.907 回答
-1

您需要一个主要的静态方法来开始一个程序。请参阅有关 java 的文档。

于 2011-05-08T09:25:35.113 回答