Java NullPointerException mit String contains()

FrazeColder

Lt. Commander
Registriert
Okt. 2013
Beiträge
1.718
Hey Community,

Ich habe da ein Problem. Ich gebe in einer GUI zwei Strings ein, die auch Zahlen beinhalten. Mit der contains()-Methode möchte Ich überprüfen, welche Zahlen in den Strings sind. Allerdings bekomme Ich da dann diesen Error:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at gameHelper.getCardNumbers(gameHelper.java:363)
	at gameHelper.access$2(gameHelper.java:361)
	at gameHelper$2.actionPerformed(gameHelper.java:109)
	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.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$300(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

Der Code dazu:
Code:
 if (cardEins.contains("2")) { <-- ERROR
			zahl_cardEins = 2;
		} else if (cardEins.contains("3")) {
			zahl_cardEins = 3;
		} else if (cardEins.contains("4")) {
			zahl_cardEins = 4;
		}

cardEins ist der String. Wenn der String einer der Zahlen beinhaltet, möchte Ich mir diese Zahlen in einer Variable abspeichern, damit Ich damit arbeit kann und nicht immer wieder mit der Contains()-Methode das überprüfen muss...

Wo liegt da mein Fehler?

MfG und Danke
 
Debugge das ganze mal, das sieht stark danach aus, dass dein cardEins-Objekt null ist und somit die Methode contains() nicht angewendet werden kann. Das heißt, deine eingegebenen Strings stehen nicht in den Variablen drin.
 
Ah ja, Ich weiß woran es lag!
Vielen Dank.

Ich habe die Methode aufgerufen, bevor Ich aus der GUI mir den String auslesen lassen habe... Also war es Null...

Allerdings habe Ich jetzt noch ein Problem. Ich habe ein die Varibale definiert und in der GUI Methode dann stehen:
Code:
...
private static JTextArea textArea = null;
...

public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					gameHelper frame = new gameHelper();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public gameHelper() {
		
                final JTextArea textArea = new JTextArea();
                textArea.setEditable(false);
                textArea.setBounds(315, 31, 176, 98);
		contentPane.add(textArea);

Ich muss laut Eclipse die textArea als final definieren. Aber sobald Ich etwas dranhängen will, kommt nach dem erstmal dann:

Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at gameHelper.calculateStraightFlush(gameHelper.java:484)
	at gameHelper.checkStraightFlush(gameHelper.java:349)
	at gameHelper.access$9(gameHelper.java:346)
	at gameHelper$2.actionPerformed(gameHelper.java:137)
	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.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$300(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

Ich glaube, der Fehler liegt hierbei im final. Kann das sein?
Wie kann Ich eine TextArea erzeugen, welche in der gesamten Klasse und in allen Methoden verwendbar ist?
 
JTextArea ist ebenfalls mit null initialisiert.
Ergänzung ()

Initialisier es bereits im statischen Feld anstatt = null zu schreiben (= new JTextArea()) oder initialisier es im gameHelper ohne ein neues lokales Objekt mit gleichem Namen zu bilden (streich das final JTextArea davor)

Du kannst auch einen statischen Initialisierer benutzen, siehe weiter unten:
http://www.javaseiten.de/ch01s04.html
 
Zuletzt bearbeitet:
FrazeColder schrieb:
Code:
...
private static JTextArea textArea = null;
...

	public gameHelper() {
                final JTextArea textArea = new JTextArea();
                //...
        }

Der Fehler ist offensichtlich. Ist das hier eigentlich Copy-Paste-Code und du lässt das Forum den für dich richten?
FrazeColder schrieb:
Wie kann Ich eine TextArea erzeugen, welche in der gesamten Klasse und in allen Methoden verwendbar ist?
Lies dir ein Buch über Java durch; von Anfang bis Ende.
 
@nullPtr: Also so n scheiß kann Ich überhaupt nicht ab!
Unterstelle Leuten nicht einfach was aus freier Nase. Denn es ist eben kein Copy & Paste Code du Besserwisser...
Wenn Ich mir eine Text Area im Swing Designer einfüge, dann fügt er mit in der Methode gameHelper() das hier ein:

Code:
      JTextArea textArea = new JTextArea();
                textArea.setEditable(false);
                textArea.setBounds(315, 31, 176, 98);
		contentPane.add(textArea);

Und das ganz unten. Somit kann Ich die textArea nicht benutzen. Daher habe Ich den Code angepasst, allerdings fordert er mich dann auf, dass ganze als final zu schreiben... Pass das nächste mal mit deiner Wortwahl auf!

@Serious_Sam: Vielen Dank.
Habe textArea einfach jetzt direkt oben initialisiert. Daran lag es.
 
FrazeColder schrieb:
Wenn Ich mir eine Text Area im Swing Designer einfüge, dann fügt er mit in der Methode gameHelper() das hier ein:

Und das ganz unten. Somit kann Ich die textArea nicht benutzen. Daher habe Ich den Code angepasst, allerdings fordert er mich dann auf, dass ganze als final zu schreiben...

Du verstehst vermutlich nicht, warum diese Compiler-Meldung kommt und wie damit in dieser Situation am besten zu verfahren ist.

Die JTextArea Member Variable sollte übrigens niemals statisch sein...
 
Zurück
Oben