为什么即使没有任何错误,编译器也不运行程序?
我有一个程序在 Eclipse 中没有显示任何错误,但是每当我单击运行按钮时,什么也没有发生。我在下面附加的编译器控制台中得到了代码行。
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 的控制台输出。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Transaction.java
中的第 61 行出现NullPointerException
。我怀疑您需要在执行
File.add(SummaryMenuItem);
之前初始化SummaryMenuItem
。You have a
NullPointerException
at line 61 inTransaction.java
.I suspect you need to initialize
SummaryMenuItem
before doingFile.add(SummaryMenuItem);
.