Emiiiiiil
Emiiiiiil

Reputation: 1

Eclipse does not launch my program, but no errors

Here is my source Code:
The main class:

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

Here is my second class:

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 says that there is no errors, but when I click the Run-button nothing happens. I have checked my code several times, but I can't find the problem.

Upvotes: 0

Views: 79

Answers (3)

Nambi
Nambi

Reputation: 12042

Declare Public in the main Class

You are getting Null Pointer Exception because,You are using field1 and field2 without initializing it.

intialize first and then use it.

Do like this in your Gui class

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

And your apples class

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

Upvotes: 0

NumberTen
NumberTen

Reputation: 94

I've compiled your code, And I got those errors :

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

Upvotes: 0

robermann
robermann

Reputation: 1722

You are getting the error:

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

That is, in the line:

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

field2 is still null (also field3).

Upvotes: 1

Related Questions