Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ActionListener gibt NullPointerException

Empfohlene Antworten

Hi,

mein ActionListener gibt mir einen NullPointer aus und ich weiß nicht woran es liegt. Vielleicht kann mir einer von euch helfen.

Der ActionEvent ist gekenzeichnet mit einer Zeile /***********/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

class PropertyGui extends JFrame implements ActionListener

{

static JTextField werttfd;

static JTextField beschreibungtfd;

static JComboBox auswahlcmb;

static Vector vrowData = new Vector();

Vector vcolumnNames = new Vector();

public void actionPerformed(ActionEvent e)

{

JPanel all = getContent();

getContentPane().add(all);

setLocation(150,80);

setSize(435,500);

setTitle("Property - Verwaltung");

setVisible(true);

addWindowListener(new WindowClose());

}

private JPanel getContent()

{

JPanel haupt = new JPanel();

haupt.setLayout(null);

JLabel auswahllb = new JLabel("Property - Auswahl");

auswahllb.setBounds(20, 15, 160, 25);

JLabel anzeigelb = new JLabel("Property - Anzeige");

anzeigelb.setBounds(20, 85, 160, 25);

JLabel wertlb = new JLabel("Wert");

wertlb.setBounds(20, 295, 160, 25);

JLabel beschreibunglb = new JLabel("Beschreibung");

beschreibunglb.setBounds(20, 365, 160, 25);

Vector inhalt = new Vector();

inhalt.add("Modus");

inhalt.add("Handbuch");

inhalt.add("Ergebnistyp");

/***************************************************/

auswahlcmb = new JComboBox(inhalt);

auswahlcmb.setBounds(20, 45, 220, 25);

auswahlcmb.addActionListener(new PropertyAuswahl());

JButton hinzufuegen = new JButton("Hinzufügen");

hinzufuegen.addActionListener(new Hinzufuegen());

hinzufuegen.setBounds(80, 255, 120, 25);

JButton entfernen = new JButton("Entfernen");

entfernen.addActionListener(new Entfernen());

entfernen.setBounds(215, 255, 120, 25);

JButton speichern = new JButton("Speichern");

speichern.addActionListener(new Speichern());

speichern.setBounds(80, 435, 120, 25);

JButton abbrechen = new JButton("Abbrechen");

abbrechen.addActionListener(new Abbrechen());

abbrechen.setBounds(215, 435, 120, 25);

werttfd = new JTextField();

werttfd.setBounds(20, 320, 390, 25);

beschreibungtfd = new JTextField();

beschreibungtfd.setBounds(20, 395, 390, 25);

vcolumnNames.add("Wert");

vcolumnNames.add("Beschreibung");

Vector vrowData1 = new Vector();

vrowData.add(vrowData1);

vrowData1.add("Test1");

vrowData1.add("Test");

JTable table = new JTable(vrowData,vcolumnNames);

JScrollPane scroll = new JScrollPane(table);

scroll.setBounds(20, 115, 390, 125);

haupt.add(auswahllb);

haupt.add(auswahlcmb);

haupt.add(anzeigelb);

haupt.add(scroll);

haupt.add(hinzufuegen);

haupt.add(entfernen);

haupt.add(wertlb);

haupt.add(werttfd);

haupt.add(beschreibunglb);

haupt.add(beschreibungtfd);

haupt.add(speichern);

haupt.add(abbrechen);

return haupt;

}

}

und das ist das Ziel vom ActionEvent

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class PropertyAuswahl implements ActionListener

{

String test;

public void actionPerformed(ActionEvent e)

{

System.out.println(PropertyGui.auswahlcmb.getSelectedItem());

test = (String) PropertyGui.auswahlcmb.getSelectedItem();

try

{

PropertyVerwaltung.rowVector.clear();

PropertyGui.vrowData.clear();

if (test == "Modus")

{

System.out.println("Modus - Datei gefunden");

PropertyVerwaltung.modusProperty();

}

else

{

if (test == "Ergebnistyp")

{

System.out.println("Ergebnistyp - Datei gefunden");

PropertyVerwaltung.ergebnistypProperty();

}

else

{

if (test == "Handbuch")

{

System.out.println("Handbuch - Datei gefunden");

PropertyVerwaltung.handbuchProperty();

}

else

{

System.out.println("Es wurde keine Datei gefunden");

}

}

}

}

catch(NullPointerException npe)

{

System.err.println(npe.getMessage());

}

}

}

Hallo,

Objekte vergleicht man mit equals, String ist ein Object.

Wo tritt die Exception auf?

Packe Source Code im Forum immer über die Buttons # oder PHP ein, dann wirds übersichtlicher.

Gruß Jaraz

beim Try und catch Block

Ach ne wirklich, da wäre ich jetzt nicht drauf gekommen. ;)

Schreibe in den catch Block mal npe.printStackTrace() und poste dann in welcher ZEILE er die Exception wirft.

Gruß Jaraz

ich darf den Befehl npe.printStackTrace() nicht benutzen da kommt folgender Fehler "void" type not allowed here

So:


try{


} catch (xxxException ex) {

      ex.printStackTrace();

}

nicht so:

try{


} catch (xxxException ex) {

      System.err.println(ex.printStackTrace());

}

Generell ist dein Code sehr komisch geschrieben.

Gruß Jaraz

das ist der Fehler

java.lang.NullPointerException

at PropertyAuswahl.actionPerformed(PropertyAuswahl.java:18)

at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1196)

at javax.swing.JComboBox.setSelectedItem(JComboBox.java:561)

at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:597)

at javax.swing.plaf.basic.BasicComboPopup$ListMouseHandler.mouseR

(BasicComboPopup.java:749)

at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster

28)

at java.awt.Component.processMouseEvent(Component.java:5021)

at javax.swing.plaf.basic.BasicComboPopup$2.processMouseEvent(Bas

Popup.java:452)

at java.awt.Component.processEvent(Component.java:4818)

at java.awt.Container.processEvent(Container.java:1380)

at java.awt.Component.dispatchEventImpl(Component.java:3526)

at java.awt.Container.dispatchEventImpl(Container.java:1437)

at java.awt.Component.dispatchEvent(Component.java:3367)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.ja

)

at java.awt.LightweightDispatcher.processMouseEvent(Container.jav

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:28

at java.awt.Container.dispatchEventImpl(Container.java:1423)

at java.awt.Window.dispatchEventImpl(Window.java:1566)

at java.awt.Component.dispatchEvent(Component.java:3367)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDis

read.java:190)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispa

ad.java:144)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.ja

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.ja

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

Leider kann man nicht erkennen, was Zeile 18 ist aber ich nehme an, du hast PropertyVerwaltung.rowVector noch nicht mit new Vector() initialisiert und deswegen schlägt PropertyVerwaltung.rowVector.clear(); fehl.

Gruß Jaraz

danke jetzt funktioniert es

Bitte.

Ich hoffe du hast nicht nur gelernt, was der Fehler war, sondern auch wie man Fehler findet. ;)

Gruß Jaraz

sei mir nicht böse, aber ich sehe leider an der Fehlermeldung nicht wo der Fehler ist. Kannst du mir es bitte kurz erklären?

java.lang.NullPointerException

at PropertyAuswahl.actionPerformed(PropertyAuswahl.java:18)

sagt, das eine NullPointerException in der Klasse PropertyAuswahl, in der Methode actionPerformed aufgetreten ist.

Und zwar in Zeile 18.

Zeile 18:

PropertyVerwaltung.rowVector.clear();

An dieser Zeile sind 2 Objecte beteiligt.

1.tes Object = PropertyVerwaltung

2.tes Object = Vector

eines dieser Objecte oder beide ist/sind nicht initialisiert, da es sich um eine NullPointerExeption handelt.

Da dein Vector static ist, brachst du PropertyVerwaltung nicht Initialisieren. Es bleibt also nur der Vector übrig.

Gruß Jaraz

jetzt habe ich es verstanden. Danke

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.