Nullpointerexception beim Start

Angus13

Mitglied
Hey leute

ich habe folgendes problem: ich soll einen chatt schreiben, doch wenn ich ihn öffnen will bekomm ich die fehlermeldung : NullPointerException.
Woran liegt das ?
Hier mein Code:
Java:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;

public class chatapplet extends Applet implements Runnable
{
	public static final int PORT = 8765;
	Socket socket;
	DataInputStream in;
	PrintStream out;
	TextField inputfield;
	TextArea outputarea;
	Thread thread;

	public void init()
	{
		inputfield = new TextField();
		outputarea = new TextArea();
		outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
		outputarea.setEditable(false);

		this.setLayout(new BorderLayout());
		this.add("South", inputfield);
		this.add("Center", outputarea);

		this.setBackground(Color.lightGray);
		this.setForeground(Color.black);
		inputfield.setBackground(Color.white);
		outputarea.setBackground(Color.white);
	}

	public void start()
	{
		try
		{
			socket = new Socket(this.getCodeBase().getHost(), PORT);
			in = new DataInputStream(socket.getInputStream());
			out = new PrintStream(socket.getOutputStream());
		} catch (IOException e)
		{
			this.showStatus(e.toString());
			say("Verbindung zum Server fehlgeschlagen!");
			System.exit(1);
		}

		say("Verbindung zum Server aufgenommen...");
		public class MyClass
		{
		}
		if (thread == null)
		{
			thread = new Thread(this);
			thread.setPriority(Thread.MIN_PRIORITY);
			thread.start();
		}
	}


	public void stop()
	{
		try
		{
			socket.close();
		} catch (IOException e)
		{
			this.showStatus(e.toString());
		}

		if ((thread !=null) && thread.isAlive())
		{
			thread.stop();
			thread = null;
		}
	}


	public void run()
	{
		String line;

		try
		{
			while(true)
			{
				line = in.readLine();
				if(line!=null)
					outputarea.appendText(line+'\n' );
			}
		} catch (IOException e) { say("Verbindung zum Server abgebrochen"); }
	}


	public boolean action(Event e, Object what)
	{
		if (e.target==inputfield)
		{
			String inp=(String) e.arg;

			out.println(inp);
			inputfield.setText("");
			return true;
		}

		return false;
	}


	public void say(String msg)
	{
		outputarea.appendText("*** "+msg+" ***\n");
	}
}
 
Zuletzt bearbeitet von einem Moderator:
Hi und Willkommen bei tutorials.de,

wie lautet der Stacktrace dazu?
(die Zeilen darunter, durch die man die Codestelle des Fehlers rausfinden kann)

Bitte Codetags verwenden und Fragen nicht als Tutorials einreichen.
 
Hey

da steht rein garnichts drunter :-/
Werde ich das nächste mal tun kenne mich hier noch nicht aus.
Kannst du vielleicht den Code mal überlfiegen?
 
Hi

Bei mir startet er einwandfrei, außer eben der Fehlermeldung, dass kein Server gefunden wurde und sich gleich wieder schließst.
Dass du keine StackTraces bekommst liegt daran dass du nirgends in Exceptions ein e.printStackTrace(); verwendest. Zusätzlich werden einige Streams nicht vor der Verwendung überprüft.
Hier mal mit StackTraces
Java:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;

public class chatapplet extends Applet implements Runnable {
	public static final int PORT = 8765;
	Socket socket;
	DataInputStream in;
	PrintStream out;
	TextField inputfield;
	TextArea outputarea;
	Thread thread;

	public void init() {
		inputfield = new TextField();
		outputarea = new TextArea();
		outputarea.setFont(new Font("Dialog", Font.PLAIN, 12));
		outputarea.setEditable(false);

		this.setLayout(new BorderLayout());
		this.add("South", inputfield);
		this.add("Center", outputarea);

		this.setBackground(Color.lightGray);
		this.setForeground(Color.black);
		inputfield.setBackground(Color.white);
		outputarea.setBackground(Color.white);
	}

	public void start() {
		try {
			socket = new Socket("127.0.0.1", PORT);
			in = new DataInputStream(socket.getInputStream());
			out = new PrintStream(socket.getOutputStream());
		} catch (IOException e) {
			this.showStatus(e.toString());
			say("Verbindung zum Server fehlgeschlagen!");
			e.printStackTrace();
//			System.exit(1);   Fehler im Chatfenster kann man lesen :D
		}

		say("Verbindung zum Server aufgenommen...");
		class MyClass {
		}
		if (thread == null) {
			thread = new Thread(this);
			thread.setPriority(Thread.MIN_PRIORITY);
			thread.start();
		}
	}

	public void stop() {
		try {
			if(socket!=null)
			socket.close();
		} catch (IOException e) {
			this.showStatus(e.toString());
			e.printStackTrace();
		}

		if ((thread != null) && thread.isAlive()) {
			thread.stop();
			thread = null;
		}
	}

	public void run() {
		String line;
		
		try {
			if(in!=null)
			while (true) {
				line = in.readLine();
				if (line != null)
					outputarea.appendText(line + '\n');
			}
		} catch (IOException e) {
			say("Verbindung zum Server abgebrochen");
			e.printStackTrace();
		}
	}

	public boolean action(Event e, Object what) {
		if (e.target == inputfield) {
			String inp = (String) e.arg;

			out.println(inp);
			inputfield.setText("");
			return true;
		}

		return false;
	}

	public void say(String msg) {
		outputarea.appendText("*** " + msg + " ***\n");
	}
}

Lg hendl
 
Zuletzt bearbeitet:
Kommt wirklich gar keine Fehlermeldung?
Auch nicht mit dem von mir geposteten Code?
 
Wenn ich es öffnen will steht da nur :

NullPointerException

Bei der Ausführung der Anwendung ist ein Fehler aufgetreten

Bin echt am Verzweifeln :(
 
Hmm hab keine Idee mehr was fehlerhaft sein könnte da es bei mir normal läuft und sonst auch keine Fehler auftreten sollten.
 
Diese fehlermeldung t aucht immer auf auch mit den StackTraces
Hast du vielleicht einen simplen chat code im kopf der laufen könnte?
 
Hmm habe villeicht den Fehler gefunden.
Java:
socket = new Socket(this.getCodeBase().getHost(), PORT);

Dieser this.getCodeBase().getHost() Aufruf liefert bei mir nur einen leeren String und wenn dich dann verbinden willst bekommst du eine NullPointerException.
Ist mir nicht aufgefallen da ich es einfach durch 127.0.0.1 ersetzt habe.

Lg hendl
 

Neue Beiträge

Zurück