Dies ist eine statische Kopie unseres alten Forums. Es sind keine Interaktionen möglich.
This is a static copy of our old forum. Interactions are not possible.

The-Holy-One

Praktikant

  • "The-Holy-One" is male
  • "The-Holy-One" started this thread

Posts: 22

Date of registration: Oct 11th 2004

1

Saturday, July 2nd 2005, 4:21pm

Java Übung 7

So ich habe folgendes Problem.
Ich habe bei mir den Kopieren Button, wie bei der Aufgabe gefordert, standard mässig deaktiviert.

Wenn nun etwas in mein Textfeld eigegeben wird und Enter gedrückt wird, soll der Button aktivert werden (was eigegeben wird, ist momentan noch belanglos, das baue ich später ein.
Allerdings bekomme ich dann beim ausführen des Programms, und betätigung der Enter Taster (nachdem ich etwas ins Textfeld eigeben habe) folgende Fehlermeldung:
Zeile 90 ist die, in der ich meinen Button aud enabled setzten will.
Lasse ich den Teil mit dem button.setEnabled(true); weg, läuft alles einwandfrei.


Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DateiKopiererGUI.actionPerformed(DateiKopiererGUI.java:90)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:487)
at javax.swing.JTextField.postActionEvent(JTextField.java:668)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:7
83)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1571)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2730)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2765)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2693)
at java.awt.Component.processEvent(Component.java:5265)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.ja
va:1810)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboard
FocusManager.java:672)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeybo
ardFocusManager.java:920)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeybo
ardFocusManager.java:798)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFoc
usManager.java:636)
at java.awt.Component.dispatchEventImpl(Component.java:3841)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)



Hier mal mein relevanter Quelltext:

public class DateiKopiererGUI
extends Object
implements ActionListener{

private ApplicationFrame af;
private JTextField textField;
private JButton button;
private String datei;
......

public DateiKopiererGUI() {
......
JButton button = new JButton("Kopieren");
button.addActionListener(this);
contentPane.add(button);
button.setEnabled(false);
......

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == textField){
button.setEnabled(true);
.......


Wenn benötigt kann ich auch noch mehr vom Quelltext veröffentlichen (nur ich will ja hier keine Kopiervorlage veröffentlichen)

This post has been edited 4 times, last edit by "The-Holy-One" (Jul 2nd 2005, 4:25pm)


Dude

Junior Schreiberling

Posts: 181

Date of registration: Oct 11th 2004

2

Saturday, July 2nd 2005, 5:01pm

Ich habs eh schon vorgestellt, für mich wärs also keine Kopiervorlage ;)

Magst mir mal den code zukommen lassen oder zumindest die Implementierung des TextFields?

The-Holy-One

Praktikant

  • "The-Holy-One" is male
  • "The-Holy-One" started this thread

Posts: 22

Date of registration: Oct 11th 2004

3

Saturday, July 2nd 2005, 5:35pm

Du hast Post

Dude

Junior Schreiberling

Posts: 181

Date of registration: Oct 11th 2004

4

Saturday, July 2nd 2005, 5:55pm

Direkt "lösen" konnte ich dein Problem nicht, aber mit sehr wenig Aufwand umgehen ... zumindest scheint es so, kopiert jetzt die Dateien einwandfrei.

Ich schick dir den Code per PM zurück.

HELP

Praktikant

Posts: 17

Date of registration: Mar 12th 2005

5

Saturday, July 2nd 2005, 5:57pm

Also ich würde behaupten, dass du zwar button als Attribut der Klasse definiert hast, aber im Konstruktor nur der lokalen Variable button ein neues JButton-Objekt zugewiesen hast. In actionPerformed() wird aber auf das Klassenattribut button zugegriffen, welches aber 'null' ist -> NullPointerException.

Also ganz einfach im Konstruktor
button = new JButton("Kopieren");
schreiben, dann sollte es gehn.

The-Holy-One

Praktikant

  • "The-Holy-One" is male
  • "The-Holy-One" started this thread

Posts: 22

Date of registration: Oct 11th 2004

6

Saturday, July 2nd 2005, 6:52pm

Das hatte ich auch getan (siehe Source Code).

Aber dank der Hilfe von Dude kann ich es jetzt endlich fertig programmieren.
Ein dickes Dankeschön :)

Dude

Junior Schreiberling

Posts: 181

Date of registration: Oct 11th 2004

7

Saturday, July 2nd 2005, 7:53pm

Jederzeit wieder.