English 中文(简体)
Why does the compiler not run the program even though there are not any errors?
原标题:

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
最佳回答

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

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

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签