为什么我的程序可以编译但不能运行? [英] Why does my program compile but it not run?

查看:178
本文介绍了为什么我的程序可以编译但不能运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a follow up question due to changes being made to the OP code. Another user suggested I link the other question to this one.

OP是:我有一个可以编译的代码,但是它没有运行.我试图让GUI运行,以便随后可以添加代码来执行所需的功能.代码如下:

I have a code that compiles but it does not run. I am trying to get the GUI to run so that I can then add the code to perform the functions I need it to. the code follows as:

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

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

The errors that I get in the console are as follows:

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

推荐答案

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

Something is null on line 64 in Transaction.java

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

You have to sets of menu items. These 3 aren't null.

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个.

Remove the last 3 ones, and use your previous 3 ones in the code.

然后您的menuPanel方法应如下所示:

Your menuPanel method should then look something like this:

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

更改后,您的代码即可正常工作.

Your code is working after that change.

这篇关于为什么我的程序可以编译但不能运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆