Eclipse不会启动我的程序,但没有错误

时间:2014-04-17 13:02:48

标签: java eclipse

这是我的源代码:
主要班级:

package daPackage;

import javax.swing.JFrame;

class apples{
    public static void main(String args[]){
        Gui go = new Gui();
        setUpGUI(go);
    }

    private static void setUpGUI(Gui g){
        g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        g.setVisible(true);
        g.setSize(375, 175);
    }
}

这是我的第二堂课:

package daPackage;

import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import javax.swing.*;
import javax.swing.event.*;

public class Gui extends JFrame{
    private JButton button;
    private JTextField field1;
    private JTextField field2;
    private JTextField field3;
    private int num1;
    private int num2;
    private int num3;

public Gui(){
    super("Testing!");
    setLayout(new FlowLayout());

    field1 = new JTextField("" + num1);
    num1 = Integer.parseInt(field2.getText()) + Integer.parseInt(field3.getText()); 
    add(field1);

    field2 = new JTextField("" + num2);
    num2 = Integer.parseInt(field1.getText()) + Integer.parseInt(field3.getText());
    add(field2);

    field3 = new JTextField("" + num3);
    num3 = Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText());
    add(field3);

    button = new JButton();
    button.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent event){
                field1.setText("" + num1);
                field2.setText("" + num2);
                field3.setText("" + num3);
            }
        }
    );
    add(button);    
}


}

Eclipse说没有错误,但是当我点击运行按钮时没有任何反应。我已多次检查我的代码,但我找不到问题。

3 个答案:

答案 0 :(得分:1)

您收到错误:

Exception in thread "main" java.lang.NullPointerException
    at Gui.<init>(Gui.java:21)
    at apples.main(apples.java:5)

即,在行中:

num1 = Integer.parseInt(field2.getText()) + Integer.parseInt(field3.getText()); 

field2仍然为空(也是field3)。

答案 1 :(得分:0)

在主类

中声明公开

你得到Null Pointer Exception,因为你正在使用field1和field2而没有初始化它。

首先初始化然后再使用它。

Gui

中这样做
class Gui extends JFrame{
    private JButton button;
    private JTextField field1;
    private JTextField field2;
    private JTextField field3;
    private int num1=0;
    private int num2=0;
    private int num3=0;

public Gui(){
    super("Testing!");
    setLayout(new FlowLayout());

    field1 = new JTextField("" + num1);
    field2 = new JTextField("" + num2);
    field3 = new JTextField("" + num3);
    num1 = Integer.parseInt(field2.getText()) + Integer.parseInt(field3.getText()); 
    add(field1);


    num2 = Integer.parseInt(field1.getText()) + Integer.parseInt(field3.getText());
    add(field2);


    num3 = Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText());
    add(field3);

    button = new JButton();
    button.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent event){
                field1.setText("" + num1);
                field2.setText("" + num2);
                field3.setText("" + num3);
            }
        }
    );
    add(button);    
}
}

你的苹果课

 public class apples{
        public static void main(String args[]){
            Gui go = new Gui();
            setUpGUI(go);
        }

        private static void setUpGUI(Gui g){
            g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            g.setVisible(true);
            g.setSize(375, 175);
        }
    }

答案 2 :(得分:0)

我编译了你的代码,我收到了这些错误:

Exception in thread "main" java.lang.NullPointerException
at daPackage.Gui.<init>(Gui.java:24)
at daPackage.Main.main(Main.java:7)
Java Result: 1