GnuPG class throwing null pointer exception

Robert J. Hansen rjh at
Wed May 28 01:07:30 CEST 2014

On 5/27/2014 5:26 AM, winifred quartey-papafio wrote:
> I'm having a problem encrypting a String text using the GnuPG class. I'm
> using the encrypt and decrypt class
> from
> which is based on the GnuPG class
> from

First, that code is kind of ... ouch.  "Bug-ridden" may be an
understatement.  Take a look at, e.g., how it reads data from GnuPG:

class ProcessStreamReader extends Thread {
    StringBuffer stream;
    InputStreamReader in;
    final static int BUFFER_SIZE = 1024;

    ProcessStreamReader(InputStream in) {
        super(); = new InputStreamReader(in); = new StringBuffer();

    public void run() {
        try {
            int read;
            char[] c = new char[BUFFER_SIZE];
            while ((read =, 0, BUFFER_SIZE - 1)) > 0)
                stream.append(c, 0, read);
                if (read < BUFFER_SIZE - 1) break;
        catch (IOException io) {

    String getString() {
        return stream.toString();

So let's say you've got this thread running, and it's cheerfully
spinning along adding data to stream.  While it's doing this, you call
its getString() method... and bam, immediate race condition, because the
call to stream.append() is neither atomic nor locked by a mutex, and
there's no guarantee that when you read the stream's contents the stream
will be in a consistent state!

On top of that, getString() really should clear the contents of stream
prior to return.  Otherwise if you get "abc" the first time you call
getString(), the second time you won't get "def" -- you'll get "abcdef".

On top of *that*, this thread will quite possibly bail out before it's
finished reading data.  It loops *fast* -- there's not a single sleep()
call in there -- and as soon as it receives less than a full buffer of
data, it assumes there's no more and bails out, rather than thinking,
"gee, maybe the other end of this communication channel is delayed for
disk I/O or somesuch."

So, yeah.  I would be wary of using this code in a production
environment.  It does not appear to be ready.

> However I keep getting a null pointer exception. I don't know what I'm
> doing wrong. I'd appreciate your help with this

Happy to help.  The first question is, what OS are you running it on,
and where is GnuPG located on your machine?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: OpenPGP digital signature
URL: </pipermail/attachments/20140527/c099015d/attachment-0001.sig>

More information about the Gnupg-users mailing list