Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

123markus123

Mitglied
Ich möchte einen Übersetzter programmieren, der z.B. deutsche in englische wörter übersetzt. Dazu habe ich folgende Quelltexte geschrieben:


Java:
package übersetzer;

public class Übersetzer {

 
    public static void main(String[] args) {
        ÜbersetzerFrame übersetzer = new ÜbersetzerFrame();
    }
}

Der Frame und der ActionListener des Buttons:

Java:
package übersetzer;

import java.awt.*;
import java.awt.event.*;

public class ÜbersetzerFrame extends Frame {

    private Label überschrift;
    private Label ausgabe;
    private TextField eingabe;
    private Button bestätigen;
    private String text;
    private Translator translator;

    ÜbersetzerFrame() {
        super("ÜbersetzerFrame");

        this.setLayout(null);

        überschrift = new Label("Geben sie hier den Text ein:");
        ausgabe = new Label();
        eingabe = new TextField();
        bestätigen = new Button("OK");

        this.add(überschrift, null);
        this.add(eingabe, null);
        this.add(ausgabe, null);
        this.add(bestätigen, null);

        überschrift.setBounds(20, 40, 200, 20);
        eingabe.setBounds(20, 70, 1400, 20);
        bestätigen.setBounds(70, 100, 50, 20);
        ausgabe.setBounds(20, 130, 1400, 20);

        bestätigen.addActionListener(new MyListener());

        this.setSize(1440, 300);
        this.setVisible(true);

    }

    public void setAusgabe(Label ausgabe) {
        this.ausgabe = ausgabe;
    }


    public TextField getEingabe() {
        return eingabe;
    }


    public Label getAusgabe() {
        return ausgabe;
    }


    public void setEingabe(TextField eingabe) {
        this.eingabe = eingabe;
    }

    class MyListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == bestätigen) {

                text = getEingabe().getText();
                getAusgabe().setText(translator.übersetze(text));

            }
        }
    }
}

Der Übersetzer:

Java:
package übersetzer;

import java.io.*;
import java.util.*;
import java.util.logging.*;

public class Translator {

    private String suchwort;

    public Translator() {
    }

    public String übersetze(String suchworte) {

        this.suchwort = suchwort;
        
        System.out.println("übersetze");

        String ausgabe = suchwort;
        try {
            BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Markus\\Documents\\NetBeansProjects\\Übersetzer\\src\\übersetzer\\Wörterbuch.dat"));
            try {
                while ((suchwort = reader.readLine()) != null) {
                    StringTokenizer tokenizer = new StringTokenizer(suchwort, " ");
                    if (tokenizer.nextToken() == suchwort) {
                        ausgabe = tokenizer.nextToken();
                    }

                }
            } catch (IOException ex) {
                Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex);
            }


        } catch (FileNotFoundException ex) {
            Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ausgabe;


    }
}

Die Wörter sind in der Datei Wörterbuch.dat:
gehen go
spielen play
rennen run

Wenn ich das Programm starte, etwas in dem Textfeld eingebe und auf den Button klicke kommt diese Fehlermeldung:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at übersetzer.ÜbersetzerFrame$MyListener.actionPerformed(ÜbersetzerFrame.java:86)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)


Ich hoffe ihr wisst, wie ich das Problem lösen kann, ich kann Java noch nicht so gut.
 

Final_Striker

Top Contributor
Java:
getAusgabe().setText(translator.übersetze(text));

Code:
translator
ist null, da du kein Objekt davon erstellst hast.
 

Final_Striker

Top Contributor
Wir denn bei
Java:
private Translator translator;
kein Objekt erstellt?

Nein, das ist eine Referenz die auf
Code:
null
zeigt.

Ein Objekt erstellst du mit dem
Code:
new
Operator, z.B:

Java:
überschrift = new Label("Geben sie hier den Text ein:");
 

pro2

Bekanntes Mitglied
Jein, erstellt wird es erst, wenn du sowas schreibst wie
Code:
translator = new Translator();
, also der Konstruktor aufgerufen wird. Sonst das ist ganz eben leer ;-)
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
B Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException Java Basics - Anfänger-Themen 8
S Java memory fehler: Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap spa Java Basics - Anfänger-Themen 5
D Exception in thread "AWT-EventQueue-0" Java Basics - Anfänger-Themen 8
C Exception in thread "AWT-EventQueue-0 Java Basics - Anfänger-Themen 15
M Exception in thread "AWT-EventQueue-0" Java Basics - Anfänger-Themen 7
J Datentypen Exception in thread "AWT-EventQueue-0"?? Java Basics - Anfänger-Themen 4
Spin Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: data must Java Basics - Anfänger-Themen 10
F Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at main.main(main.java:11) Java Basics - Anfänger-Themen 2
M Exception in thread "main" java.util.NoSuchElementException Java Basics - Anfänger-Themen 2
B Compiler-Fehler Fehlermeldung Exception in thread, falsche Eingabewert Java Basics - Anfänger-Themen 2
S Kriege Fehler "Exception in thread" beim Benutzen von SubStrings. Java Basics - Anfänger-Themen 2
O Exception in thread "main" java.lang.ArithmeticException: / by zero Java Basics - Anfänger-Themen 4
R Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 5
S Compiler-Fehler Exception in thread "main" java.lang.Error: Unresolved compilation problem: Java Basics - Anfänger-Themen 6
I Compiler-Fehler Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 Java Basics - Anfänger-Themen 3
R Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 10
C Compiler-Fehler Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 Java Basics - Anfänger-Themen 3
J Exception in thread "main" Java Basics - Anfänger-Themen 1
L Fehler: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException Java Basics - Anfänger-Themen 4
N Threads Exception in thread "main"... Feher bei dem Versuch ein Radius zu berechnen Java Basics - Anfänger-Themen 4
A Code läuft nicht, Fehlermeldung Exception in thread "main" java.lang.Error: Unresolved compilation " Java Basics - Anfänger-Themen 11
V Threads Exception in Thread behandeln Java Basics - Anfänger-Themen 3
P Exception in thread "main" java.lang.NoClassDefFoundError: Java Basics - Anfänger-Themen 1
K Exception in thread "main" Java Basics - Anfänger-Themen 7
L Compiler-Fehler Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 2
F Exception in thread main java.lang.StackOverflowError Java Basics - Anfänger-Themen 3
A Compiler-Fehler Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 7
T Problem mit Eclipse? Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 4
M Exception in thread "main" java.lang.NoClassDefFoundError: MeineKlasse Java Basics - Anfänger-Themen 12
S Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 11
S Umgebungsvariable Exception in thread "main" java.lang.UnsatisfiedLinkError: no J3D in java.librar y.path Java Basics - Anfänger-Themen 15
M Klassen Exception in thread "main" java.lang.NoClassDefFoundError: Java Basics - Anfänger-Themen 2
D Exception in thread "main" Java Basics - Anfänger-Themen 8
A Exception in thread "main" Java Basics - Anfänger-Themen 7
A Exception aus Thread werfen Java Basics - Anfänger-Themen 14
S Exception in thread "main" Java Basics - Anfänger-Themen 3
B Exception in thread "main" java.lang.NullPointerException Fehler Hilfe! Java Basics - Anfänger-Themen 4
T Thread 2x starten verursacht Exception Java Basics - Anfänger-Themen 3
L Bubblesort: Exception in Thread "main" Java Basics - Anfänger-Themen 5
Y Exception in thread Java Basics - Anfänger-Themen 11
A Exception in thread "main" java.lang.NullPointerException Java Basics - Anfänger-Themen 16
A GELÖST -- Exception in thread "main" Java Basics - Anfänger-Themen 3
B Fehlermeldung - Exception in thread "main" java.lang.Error: Unresolved compilation pr Java Basics - Anfänger-Themen 16
E Fehler: "Exception in thread "main" java.lang.NoSuchMethodError" Java Basics - Anfänger-Themen 15
G Fehler: Exception in thread main java.lang.noClassDefFound Java Basics - Anfänger-Themen 7
L Exception in thread "main" java.util.NoSuchElement Java Basics - Anfänger-Themen 4
A Do/While Problem (Exception in thread "main" java. Java Basics - Anfänger-Themen 4
M "exception in thread "main" java.lang.NullPoi Java Basics - Anfänger-Themen 2
S Exception in thread "main" java.lang.UnsupportedCl Java Basics - Anfänger-Themen 4
P Exception in thread "main" Java Basics - Anfänger-Themen 4
O java.lang.NoClassDefFoundError Exception in thread "mai Java Basics - Anfänger-Themen 5
V Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 21
S Exception in thread "main" java.lang.NoSuchMethodE Java Basics - Anfänger-Themen 3
S Exception in thread Java Basics - Anfänger-Themen 7
NightmareVirus Exception in thread "main" java.lang.NoSuchMethodE Java Basics - Anfänger-Themen 8
A "Exception in thread "main" java.lang.NoCLass Java Basics - Anfänger-Themen 10
N exception in thread main . Java Basics - Anfänger-Themen 3
A Exception in thread "Thread-33" java.lang.NoClassD Java Basics - Anfänger-Themen 10
C Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 9
vogella Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 4
F Fehlermeldung: Exception in thread. Java Basics - Anfänger-Themen 17
R Exception in thread "main" java.lang.NoSuchMethodE Java Basics - Anfänger-Themen 6
C exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 7
A Fehlermeldung: Exception in thread "main" java.lan Java Basics - Anfänger-Themen 3
H Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 3
G Exception in thread Java Basics - Anfänger-Themen 9
R Exception in Thread "main" . Kommandozeile/Netbean Java Basics - Anfänger-Themen 8
L Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 4
R Exception in thread "main" java.lang.NullPointerEx Java Basics - Anfänger-Themen 10
B Exception in thread "main"... Java Basics - Anfänger-Themen 3
H exception in thread mainjava.lang.noclass ... Java Basics - Anfänger-Themen 3
L Exception in thread "main" java.lang.NoSuchMethodE Java Basics - Anfänger-Themen 3
K Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 8
J Exception in thread "main" java.langClassNoFoundEr Java Basics - Anfänger-Themen 2
H Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 5
B Fehler: Exception in Thread "main" java.lang.NoCla Java Basics - Anfänger-Themen 2
G Exception in thread "main" java.lang.NoClassDefFou Java Basics - Anfänger-Themen 2
I Exception wird gefangen, aber trotzdem in Error Log? Java Basics - Anfänger-Themen 10
W Null-Pointer Exception beim Programmstart Java Basics - Anfänger-Themen 8
Ostkreuz String Exception Java Basics - Anfänger-Themen 8
Fiedelbambu Exception in Application constructor Java Basics - Anfänger-Themen 3
S leeres Array statt Null Pointer Exception ausgeben Java Basics - Anfänger-Themen 20
F abbruch Exception lässt sich nicht erstellen Java Basics - Anfänger-Themen 2
U Warum kriege ich hier eine nullpointer exception, sehe den Fehler nicht (swing) Java Basics - Anfänger-Themen 1
N Exception beim Verwenden von Arraylist? Java Basics - Anfänger-Themen 10
S JavaKara Null Exception Error Java Basics - Anfänger-Themen 4
T Eigene Exception - ohne werfen abfangen Java Basics - Anfänger-Themen 2
LiFunk Exception: es dürfen nur Nummern eingelesen werden Java Basics - Anfänger-Themen 6
low_in_the_head Eigene Exception nutzen Java Basics - Anfänger-Themen 4
1 Exception Java Basics - Anfänger-Themen 2
I JAX-RS Exception Handling Java Basics - Anfänger-Themen 4
L Meine erste eigene Exception Klasse Java Basics - Anfänger-Themen 10
J null exception Array Java Basics - Anfänger-Themen 5
H Frage zu Throw Exception Java Basics - Anfänger-Themen 2
M Wie kann ich bei int-Variablen im exception handler auf bestimmte Strings reagieren? Java Basics - Anfänger-Themen 5
C Exception-Frage Java Basics - Anfänger-Themen 3
I Exception bei Button mit wait() und notifyAll() Java Basics - Anfänger-Themen 3
N Wie teste ich eine geworfene Exception? Java Basics - Anfänger-Themen 8
R Methoden ArrayList clonen wirft exception Java Basics - Anfänger-Themen 3
D Scanner- Exception NoSuchElementException Java Basics - Anfänger-Themen 2

Ähnliche Java Themen

Neue Themen


Oben