由于对OP代码进行了更改,因此这是一个后续问题。另一位用户建议我将另一个问题与此问题联系起来。
OP是:Why does the compiler not run the program even though there are not any errors?
我有一个编译的代码,但它没有运行。我试图让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
答案 0 :(得分: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
,因此当您尝试在其值上调用方法时,您将获得异常。
修复此特定错误的重点是学习如何修复错误:
答案 1 :(得分:3)
Transaction.java
中第64行的某些内容为null您必须有多组菜单项。这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);
}
您的代码在更改后正在运行。
答案 2 :(得分:3)
当代码编译时,它只会出现语法错误(如何向编译器讲述事情),但这并不意味着不存在语义错误(为了实现目标需要做些什么) )。
如果仔细检查异常信息,可以在底部找到:
Caused by: java.lang.NullPointerException
at Transaction.menuPanel(Transaction.java:64)
at Transaction.<init>(Transaction.java:37)
在Transaction类的menuPanel方法中使用了一个Null指针(可能是一个没有正确初始化的变量),它是从事务的构造函数调用的。
检查代码,似乎你实际上在没有初始化的情况下使用变量(即使用空值):
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);
}
答案 3 :(得分:2)
您在第64行收到NullPointerException:
Caused by: java.lang.NullPointerException
at Transaction.menuPanel(Transaction.java:64)
这就是那条线:
summaryMenuItem.addActionListener(new SummaryMenuListener());
summaryMenuItem
看起来像null
,似乎没有设置为任何值。
答案 4 :(得分:-1)
您需要一个主静态方法来开始一个程序。请参阅有关java的文档。