Problem z writeUTF(String);

0

Niestety... mam kolejny problem :(
Mam klasę:

import java.io.*;
class Numer {
int numer;
String nazwa;
int logowanie;
String haslo;
int fontface, fontstyle, fontsize;
boolean getStatOnStart;
boolean StatOnList;
boolean AutoConnect;
int ticker;
int tickerOnList;
int vibraOnMessage;
int vibraOnStat;
int soundOnMessage;
int soundOnStat;
int arhives;
boolean allArhives;
int koszt, jkoszt, taktkoszt;
int defstatus;
String sdefstatus;
int iopisy;
String [] opisy;
int maxopisy;
public byte [] getBytes () {
	ByteArrayOutputStream bs = new ByteArrayOutputStream();
	try {
		DataOutputStream os = new DataOutputStream(bs);
		os.writeInt (numer);
		os.writeUTF (nazwa);
		os.writeInt (logowanie);
		os.writeUTF (haslo);
		os.writeInt (fontface);
		os.writeInt (fontstyle);
		os.writeInt (fontsize);
		os.writeBoolean (getStatOnStart);
		os.writeBoolean (StatOnList);
		os.writeBoolean (AutoConnect);
		os.writeInt (ticker);
		os.writeInt (tickerOnList);
		os.writeInt (vibraOnMessage);
		os.writeInt (vibraOnStat);
		os.writeInt (soundOnMessage);
		os.writeInt (soundOnStat);
		os.writeInt (arhives);
		os.writeBoolean (allArhives);
		os.writeInt (koszt);
		os.writeInt (jkoszt);
		os.writeInt (taktkoszt);
		os.writeInt (defstatus);
		os.writeUTF (sdefstatus);
		os.writeInt (iopisy);
		for (int i=0; i< iopisy; i++) os.writeChars (opisy[i]);
		os.writeInt (maxopisy);
	} catch (IOException e) {}
	return bs.toByteArray();
}
public boolean setBytes (byte [] dane) {
	try {
	DataInputStream is = new DataInputStream(new ByteArrayInputStream(dane));
	numer = is.readInt ();
	nazwa = is.readUTF ();
	logowanie = is.readInt ();
	haslo = is.readUTF ();
	fontface = is.readInt ();
	fontstyle = is.readInt ();
	fontsize = is.readInt ();
	getStatOnStart = is.readBoolean ();
	StatOnList = is.readBoolean ();
	AutoConnect = is.readBoolean ();
	ticker = is.readInt ();
	tickerOnList = is.readInt ();
	vibraOnMessage = is.readInt ();
	vibraOnStat = is.readInt ();
	soundOnMessage = is.readInt ();
	soundOnStat = is.readInt ();
	arhives = is.readInt ();
	allArhives = is.readBoolean ();
	koszt = is.readInt ();
	jkoszt = is.readInt ();
	taktkoszt = is.readInt ();
	defstatus = is.readInt ();
	sdefstatus = is.readUTF ();
	iopisy = is.readInt ();
	for (int i=0; i< iopisy; i++) opisy[i] =  is.readUTF ();
	maxopisy = is.readInt ();
	return true;
	} catch (IOException e) {
		return false;
	}
}
public byte [] getMiniBytes () {
	try {
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		DataOutputStream os = new DataOutputStream(bs);
		os.writeInt (numer);
		os.writeUTF (nazwa);
		return bs.toByteArray();
	} catch (IOException e) {
		return null;
	}
}
public boolean setMiniBytes (byte [] dane) {
	try {
	DataInputStream is = new DataInputStream(new ByteArrayInputStream(dane));
	numer = is.readInt ();
	nazwa = is.readUTF ();
	return true;
	} catch (IOException e) {
		return false;
	}
}
public Numer (int numerek, String nazwanko) {
numer = numerek;
nazwa = nazwanko;
logowanie = 0;
String haslo = "";
fontface = 0;
fontstyle = 0;
fontsize = 0;
getStatOnStart = true;
StatOnList = true;
AutoConnect = false;
ticker = 0;
tickerOnList = 0;
vibraOnMessage = 0;
vibraOnStat = 0;
soundOnMessage = 0;
soundOnStat = 0;
arhives = 0;
allArhives = false;
koszt = 0;
jkoszt = 0;
taktkoszt = 0;
defstatus = 0;
sdefstatus = "";
iopisy = 0;
maxopisy = 9;
}
public Numer (byte [] dane) {
	setBytes (dane);
}
}

Może ktoś z Was mi powie czemu wywołanie metody getBytes () powoduje wyjątek?
Metoda ta jest wywoływana w taki sposób:

public boolean addNumer (String nazwa, int numer) {
	if (isInList(lnumery, nazwa)) return false;
	Numer cnumer = new Numer (numer, nazwa);
	try {
		RecordStore numery = RecordStore.openRecordStore ("numery", true);
		byte [] bajty = cnumer.getBytes ();
		numery.addRecord (bajty,0,bajty.length);
		numery.closeRecordStore ();
	} catch (Exception e) {
		return false;
	}
	return true;
}

Po wielu próbach spostrzegłem że gdy pominę te 2 linie z metody getBytes to nie ma wyjątku i cała operacja kończy się powodzeniem:

		os.writeUTF (haslo);
		os.writeUTF (sdefstatus);

Jeśli zapiszę jakiś ciąg znaków... to nie ma wyjątku... np:

os.writeUTF ("tekst");

Ale jeśli w konstruktorze napiszę:

String haslo = "tekst";

I w metodzie getBytes:

os.writeUTF (haslo);

To wywołuje wyjątek :(.
Kompletnie nie wiem co jest grane. Proszę o pomoc.

0

Pytanie jaki wyjątek.

0

java.lang.NullPointerException

0

Po pierwsze czy masz pewność, że zmienna ma wartość inną niż null w tym zasięgu. Innymi słowy czy jest zainicjowana, warto przed wypisaniem sprawdzić ifem czy zmienna nie jest null. Sam kod:

String d = "asd";
DataOutputStream os = new DataOutputStream(System.out);
os.writeUTF("asd");
os.writeUTF(d);

Działa i jest poprawny.
Sprawdź też czy strumień, do którego piszesz nie jest nullem.

0

Faktycznie... te Stringi nie były zainicjowane. Sprawdziłem czy == null. I były równe null.
Teraz w konstruktorze dałem tak:

haslo = new String ("");
sdefstatus = new String ("");

I teraz działa :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1