Hi Leute,
das folgende Programm soll eine quadratische Gleichung lösen.
Das Programm wird ohne Fehler oder Warnungen kompiliert.
Allerdings wird beim ausgeführten Programm wenn man auf den Button "Nach x auflösen" klickt folgende Fehlermeldung auf der Konsole ausgegeben:
"java.lang.NullPointerException
at pq.actionPerformed(pq.java:98)
at java.awt.Button.processActionEvent(Button.java:382)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
"
Ich benutze den Java Compiler von SuSE Linux 9.2 Professional.
Hoffentlich könnt ihr mir helfen.
Vielen dank schon im voraus.
MfG Robel
das folgende Programm soll eine quadratische Gleichung lösen.
Code:
import java.lang.Double.*;
import java.lang.Math.*;
import java.awt.*;
import java.awt.event.*;
public class pq extends Frame implements ActionListener
{
private TextField[] eingabe = new TextField[5];
private double[] zahl;
public static void main(String[] args)
{
pq neu = new pq();
}
public pq()
{
setLayout(new FlowLayout(FlowLayout.CENTER,20,20));
setSize(525,550);
setLocation(160,160);
setTitle("Quadrati");
addWindowListener(new WindowClosingAdapter());
eingabe[0] = new TextField("",3);
eingabe[0].addActionListener(this);
add(eingabe[0]);
Label xx = new Label("x²");
add(xx);
eingabe[1] = new TextField("",3);
eingabe[1].addActionListener(this);
add(eingabe[1]);
Label x = new Label("x");
add(x);
eingabe[2] = new TextField("",3);
eingabe[2].addActionListener(this);
add(eingabe[2]);
Button aufloesen = new Button("Nach x auflösen");
aufloesen.addActionListener(this);
add(aufloesen);
Label x1 = new Label("x1 =");
add(x1);
eingabe[3] = new TextField("",3);
add(eingabe[3]);
Label x2 = new Label("x2 =");
add(x2);
eingabe[4] = new TextField("",3);
add(eingabe[4]);
setVisible(true);
}
public void quad(double p,double q)
{
double x1,x2,x3;
x3=Math.sqrt( (p / 2) * (p / 2) - q);
x1=(p/2.0) - x3;
x2=(p/2.0) + x3;
eingabe[3].setText(Double.toString(x1));
eingabe[4].setText(Double.toString(x2));
}
public double text2Double(String text)
{
double d;
try
{
d = Double.parseDouble(text);
return d;
}
catch(NumberFormatException e)
{
System.out.println("Fehler: " + e);
d=0;
return d;
}
}
public void actionPerformed(ActionEvent evt)
{
String[] input = new String[3];
Object obj = evt.getSource();
if(evt.getSource() instanceof Button)
{
if(evt.getActionCommand().equals("Nach x auflösen"))
{
eingabe[3].setText("");
eingabe[4].setText("");
for(int i=0;i<3;++i)
{
input[i] = eingabe[i].getText();
zahl[i] = text2Double(input[i]);
}
quad(zahl[1]/zahl[0],zahl[2]/zahl[0]);
}
}
if(evt.getSource() instanceof TextField)
{
for(int i=0;i<3;++i)
{
if(evt.getSource() == eingabe[i])
{
if(i == 2)
eingabe[0].requestFocus();
else
eingabe[i+1].requestFocus();
}
}
}
}
}
Das Programm wird ohne Fehler oder Warnungen kompiliert.
Allerdings wird beim ausgeführten Programm wenn man auf den Button "Nach x auflösen" klickt folgende Fehlermeldung auf der Konsole ausgegeben:
"java.lang.NullPointerException
at pq.actionPerformed(pq.java:98)
at java.awt.Button.processActionEvent(Button.java:382)
at java.awt.Button.processEvent(Button.java:350)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
"
Ich benutze den Java Compiler von SuSE Linux 9.2 Professional.
Hoffentlich könnt ihr mir helfen.
Vielen dank schon im voraus.
MfG Robel