Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null

fragen1985

Mitglied
Hallo ,

ich schreibe mein erster Gui :lol: und daher keine Größe Erfahrung,

mein Code Lautet

Java:
package MathematischeRechnungen;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import MathematischeRechnungen.XQuadratBerechnung;

public class MainWindow extends JFrame implements ActionListener {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static String zahl1;
    public static String zahl2;
    public static  String zahl3;
    
    static XQuadratBerechnung mainB = new XQuadratBerechnung();
    //public static String[] results;
    
    
    JLabel noThing1 = new JLabel("");
    JLabel noThing2 = new JLabel("");
    JLabel noThing3 = new JLabel("");
    JLabel noThing4 = new JLabel("");
    JLabel x2 = new JLabel(" X² ");
    JLabel x1 = new JLabel(" X ");
    JLabel plus1 = new JLabel("  +  ");
    JLabel plus2 = new JLabel("  +  ");
    JLabel equal = new JLabel("  =  ");

    JTextField x2Zahl = new JTextField();
    JTextField x1Zahl = new JTextField();
    JTextField aZahl = new JTextField();
    JTextField bZahl = new JTextField();

    JButton cal = new JButton("Calculate");

    public MainWindow() {
        // TODO Auto-generated constructor stub
        super(" Calculate Programm ");
        initComponents();
        this.addListeners();
    }
    
    
    private static void resultat() {
        JFrame Result = new JFrame("Result");
        Result.setLayout(new GridLayout(2, 0));
        
        Result.add(new JLabel(" X1 = " + MainWindow.Berechnung()[0]));
        Result.add(new JLabel(" X2 = " + MainWindow.Berechnung()[1]));
                
        Result.setLocation(400, 400);
        Result.setPreferredSize(new Dimension(160, 100)); 
        Result.setVisible(true);
        Result.pack();
    }

    private void initComponents() {

        Container c = this.getContentPane();
        GridBagLayout gbl = new GridBagLayout();
        c.setLayout(gbl);

        // x y w h wx wy
        this.addComponent(c, gbl, new JLabel("" + "Example : X²"), 0, 0, 0, 1,
                1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " + "), 3, 0, 0, 1, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " 4 "), 4, 0, 0, 1, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " X "), 5, 0, 0, 1, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " + "), 6, 0, 0, 1, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " 4 "), 7, 0, 0, 1, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " = "), 8, 0, 0, 1, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " 0 "), 9, 0, 0, 1, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 1, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 2, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 3, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 4, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 5, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 6, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 7, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 8, 1, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 9, 1, 0, 3, 1.0, 1.0);

        this.addComponent(c, gbl, this.x2Zahl, 0, 3, 1, 1, 1.0, 1.0);
        this.x2Zahl.setBorder(BorderFactory.createEtchedBorder());
        this.addComponent(c, gbl, this.noThing2, 1, 3, 1, 1, 1.0, 1.0);
        this.x2.setPreferredSize(new Dimension(5, 5));
        this.addComponent(c, gbl, this.x2, 2, 3, 1, 1, 1.0, 1.0);

        this.addComponent(c, gbl, this.plus1, 3, 3, 1, 1, 1.0, 1.0);
        this.addComponent(c, gbl, this.x1Zahl, 4, 3, 1, 1, 1.0, 1.0);
        this.x1Zahl.setBorder(BorderFactory.createEtchedBorder());
        this.addComponent(c, gbl, this.x1, 5, 3, 1, 1, 1.0, 1.0);
        this.addComponent(c, gbl, this.plus2, 6, 3, 1, 1, 1.0, 1.0);
        this.addComponent(c, gbl, this.aZahl, 7, 3, 1, 1, 1.0, 1.0);
        this.aZahl.setBorder(BorderFactory.createEtchedBorder());
        this.addComponent(c, gbl, this.equal, 8, 3, 1, 1, 1.0, 1.0);
        this.bZahl.setBorder(BorderFactory.createEtchedBorder());
        this.addComponent(c, gbl, this.bZahl, 9, 3, 1, 1, 1.0, 1.0);

        this.addComponent(c, gbl, new JLabel("" + " "), 1, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 2, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 3, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 4, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 5, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 6, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 7, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 8, 4, 0, 2, 1.0, 1.0);
        this.addComponent(c, gbl, new JLabel("" + " "), 9, 4, 0, 3, 1.0, 1.0);
        this.addComponent(c, gbl, this.cal, 1, 20, 8, 1, 1.0, 1.0);

        this.setLocation(255, 255);
        this.setPreferredSize(new Dimension(400, 130)); // hehe, du hast...

        this.setVisible(true);
        this.pack();
    }

    void addComponent(Container cont, GridBagLayout gbl, Component c, int x,
            int y, int width, int height, double weightx, double weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        gbl.setConstraints(c, gbc);
        cont.add(c);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MainWindow s = new MainWindow();
        

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource().equals(this.x2Zahl)) {
            
            try {
                MainWindow.zahl1 = this.x2Zahl.getSelectedText();

            } catch (NumberFormatException exc) {
                //ignore
            }
        }
        if (e.getSource().equals(this.x1Zahl)) {
            MainWindow.zahl2 = this.x1Zahl.getSelectedText();
        }
        if (e.getSource().equals(this.aZahl)) {
            MainWindow.zahl3 = this.aZahl.getSelectedText();
        }

        if (e.getSource().equals(this.cal)) {
                MainWindow.resultat();
                
            
            
            
        }
    }

    private void addListeners() {
        this.cal.addActionListener(this);
        this.x2Zahl.addActionListener(this);
        this.x1Zahl.addActionListener(this);
        this.aZahl.addActionListener(this);
        this.bZahl.addActionListener(this);
    }

    public static String[] Berechnung(){
        int d = Integer.parseInt(MainWindow.zahl1);
        int c = Integer.parseInt(MainWindow.zahl2);
        int e = Integer.parseInt(MainWindow.zahl3);
        // Test fuer alle Zahlen
        if (
        d == 0 || 
        c == 0 || 
        e == 0) {
            throw new IllegalArgumentException("Bitte Zahlen Angabe kontrollieren");
        }
        
        // dieser Fall ist für x^2 ist nicht 0
        if (
        Integer.parseInt(MainWindow.zahl1) > 1 || 
        Integer.parseInt(MainWindow.zahl1) < 0) {
            
        c = c /d;
            
        e = e / d;
        }
        
        
        int x = - c;
        
        // der Fall fuer x1 < 0
        if (x > 0) {
            for (int i = 1; i <= x; i++) {
                for (int j = 1; j <= i; j++) {
                    if ( i + j == x && i * j == 
        Integer.parseInt(MainWindow.zahl3)) {
                        String[] results = {Integer.toString(i),Integer.toString(j)};
                        return results;
                    }
                }
            }
        }
        
     // der Fall fuer x1 > 0
        if (x < 0) {
            for (int i = 1; i <= -x; i++) {
                for (int j = 1; j <= i; j++) {
                    if ( i + j == -x && i * j == 
        Integer.parseInt(MainWindow.zahl3)) {
                        String[] results = {Integer.toString(-i),Integer.toString(-j)};
                        return results;
                    }
                }
            }
        }
            
        String[] error = {"ERROR"}; 
        return  error;
    }
}

Danke :rtfm:
 
Wäre schön, wenn du die Zeile angeben könntest, wo der Fehler auftritt.
Selbst ausprobieren kann ich das Programm nicht, weil ich keine Klasse XQuadratBerechnung habe.
 
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at MathematischeRechnungen.MainWindow.Berechnung(MainWindow.java:195)
at MathematischeRechnungen.MainWindow.resultat(MainWindow.java:64)
at MathematischeRechnungen.MainWindow.actionPerformed(MainWindow.java:178)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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)
 
Indem du zum Beispiel Exceptions nicht einfach ignorierst:

[JAVA=163]try {
MainWindow.zahl1 = this.x2Zahl.getSelectedText();

} catch (NumberFormatException exc) {
//ignore
}[/code]
Wenn es an dieser Stelle schief geht (und ich kann nur raten, ob es das tut), dann erfährt niemand etwas davon. Schreib doch zumindest
Java:
exc.printStackTrace();
in den catch-Block…

Und überhaupt: Warum sollte das eine NumberFormatException geben? zahl1 ist ein String (wobei es ein sehr unglücklich gewählter Bezeichner ist) und was aus getSelectedText() rauskommt, ist auch ein String…
 

Zurück
Oben