I am trying to connect to a terminal emulator using a library in android, this will connect to a serial device and should show me sent/received data. To attach to a terminal session I need to provide an inputstream to setTermIn(InputStream) and an outputstream to setTermOut(OutputStream).
However when I declare the streams as follows they default to null don’t they?
private OutputStream bos;
private InputStream bis;
...
//inside onCreate() method
session.setTermIn(bis);
session.setTermOut(bos);
/* Attach the TermSession to the EmulatorView. */
mEmulatorView.attachSession(session)
So when I try to attach my streams to the session i get an error:
FATAL EXCEPTION: TermSession input reader
java.lang.NullPointerException at jackpal.androidterm.emulatorview.TermSession$2.run(TermSession.java:137)
which points to this code in the library:
public void run()
{
try
{
while (true)
{
int read = mTermIn.read(mBuffer);
if (read == -1)
{
// EOF -- process exited
return;
}
mByteQueue.write(mBuffer, 0, read);
mMsgHandler.sendMessage(mMsgHandler.obtainMessage(NEW_INPUT));
}
}
catch (IOException e)
{
}
catch (InterruptedException e)
{
}
}
This is because I haven’t started sending and receiving data yet so I haven’t assigned the data to the streams. What do i do, set up some spurious data to assign to a stream?
I tried that and it complains still. Perhaps I am doing it wrong.
To send and receive data i have the following methods, although I shouldn’t be setting the stream every time I call them I imagine.
Overriding the method that sends data over serial to set up the stream, runs whenever I press enter:
public void sendData(byte[] data)
{
bos = new ByteArrayOutputStream(data.length);
}
Runs whenever data is received:
public void onDataReceived(int id, byte[] data)
{
bis = new ByteArrayInputStream(data);
}
How do I initialize these correctly? And should I really be setting the stream every time I send and receive data, does it matter? Or should I do some check?
You must initialize
bisandbosbefore using themYou can put it just before using these two line