Thread: NPE in creating a SQLException

NPE in creating a SQLException

From
Peter Royal
Date:
I'm getting the below NPE:

java.lang.NullPointerException
         at java.io.PrintWriter.write(PrintWriter.java:247)
         at java.io.PrintWriter.print(PrintWriter.java:392)
         at java.io.PrintWriter.println(PrintWriter.java:529)
         at java.lang.Throwable.printStackTrace(Throwable.java:509)
         at java.sql.SQLException.<init>(SQLException.java:103)
         at
org.postgresql.util.PSQLException.<init>(PSQLException.java:19)
         at
org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connec
tion.java:480)
         at
org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connec
tion.java:461)
         at
org.postgresql.jdbc1.AbstractJdbc1Connection.setAutoCommit(AbstractJdbc1
Connection.java:942)

The offense starts here (SQLException:103)

    if (!(this instanceof SQLWarning)) {
        if (DriverManager.getLogWriter() != null) {
        printStackTrace(DriverManager.getLogWriter());
        }
    }

And then continues on to (Throwable:509)

         synchronized (s) {
             s.println(this);
             StackTraceElement[] trace = getOurStackTrace();


It is the "s.println(this)" that is causing errors... All of this is
happening in the constructor, so I guess that is why 'this' is null?
I'm not 100% sure and pretty confused by it all :/
-pete


Re: NPE in creating a SQLException

From
"Scot P. Floess"
Date:
Peter

Can you send more code?  Is "s" initialized in your synchronized block?  You might print s before the synchronized
blockto verify...  It could be your null pointer is there... 

Scot
-------Original Message-------
From: Peter Royal <proyal@pace2020.com>
Sent: 07/29/03 10:01 AM
To: pgsql-jdbc@postgresql.org
Subject: [JDBC] NPE in creating a SQLException

>
> I'm getting the below NPE:

java.lang.NullPointerException
         at java.io.PrintWriter.write(PrintWriter.java:247)
         at java.io.PrintWriter.print(PrintWriter.java:392)
         at java.io.PrintWriter.println(PrintWriter.java:529)
         at java.lang.Throwable.printStackTrace(Throwable.java:509)
         at java.sql.SQLException.<init>(SQLException.java:103)
         at
org.postgresql.util.PSQLException.<init>(PSQLException.java:19)
         at
org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connec
tion.java:480)
         at
org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connec
tion.java:461)
         at
org.postgresql.jdbc1.AbstractJdbc1Connection.setAutoCommit(AbstractJdbc1
Connection.java:942)

The offense starts here (SQLException:103)

       if (!(this instanceof SQLWarning)) {
           if (DriverManager.getLogWriter() != null) {
              printStackTrace(DriverManager.getLogWriter());
           }
       }

And then continues on to (Throwable:509)

         synchronized (s) {
             s.println(this);
             StackTraceElement[] trace = getOurStackTrace();


It is the "s.println(this)" that is causing errors... All of this is
happening in the constructor, so I guess that is why 'this' is null?
I'm not 100% sure and pretty confused by it all :/
-pete


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
>

Re: NPE in creating a SQLException

From
Peter Royal
Date:
On Tuesday, July 29, 2003, at 11:17  AM, Scot P. Floess wrote:
> Can you send more code?  Is "s" initialized in your synchronized
> block?  You might print s before the synchronized block to verify...
> It could be your null pointer is there...

Sorry I didn't include more.. This is with JVM 1.4.1, so all the java.*
stuff is the standard source for that.

>>
>> I'm getting the below NPE:
>
> java.lang.NullPointerException
>          at java.io.PrintWriter.write(PrintWriter.java:247)
>          at java.io.PrintWriter.print(PrintWriter.java:392)
>          at java.io.PrintWriter.println(PrintWriter.java:529)
>          at java.lang.Throwable.printStackTrace(Throwable.java:509)
>          at java.sql.SQLException.<init>(SQLException.java:103)
>          at
> org.postgresql.util.PSQLException.<init>(PSQLException.java:19)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Conne
> c
> tion.java:480)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Conne
> c
> tion.java:461)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.setAutoCommit(AbstractJdbc
> 1
> Connection.java:942)
>
> The offense starts here (SQLException:103)
>
>        if (!(this instanceof SQLWarning)) {
>            if (DriverManager.getLogWriter() != null) {
>               printStackTrace(DriverManager.getLogWriter());
>            }
>        }
>
> And then continues on to (Throwable:509)
>
>          synchronized (s) {
>              s.println(this);
>              StackTraceElement[] trace = getOurStackTrace();

Which is fully:

     public void printStackTrace(PrintWriter s) {
         synchronized (s) {
             s.println(this);
             StackTraceElement[] trace = getOurStackTrace();
             for (int i=0; i < trace.length; i++)
                 s.println("\tat " + trace[i]);

             Throwable ourCause = getCause();
             if (ourCause != null)
                 ourCause.printStackTraceAsCause(s, trace);
         }
     }

> It is the "s.println(this)" that is causing errors... All of this is
> happening in the constructor, so I guess that is why 'this' is null?
> I'm not 100% sure and pretty confused by it all :/

But 'this' is null.... which is very weird to me.
-pete


Re: NPE in creating a SQLException

From
"Scot P. Floess"
Date:
Peter:

Does your class have a toString() method?  You mention that this is occurring in a constructor.  Certainly if your
toString()has object members being printed in such a way as method calls are performed on those object prior to their
beingset to instances there could be issue (ie NullPointerException).  However, assuming this to be true, one would
hopethe stack trace would indicate the toString() method. 

Have you tried to evaluate "this" first...ie if ( this == null ) { do something }

If tried the following:

public final class NullTest
{
    public NullTest ()
    {
        System.out.println ( this == null );
    }

    public static void main ( String args[] )
    {
        NullTest nt = new NullTest ();
    }
}

I get "false" printed.

I would be very suprised if "this" ever equaled null...

Can you send the whole class source in question?

Scot

-------Original Message-------
From: Peter Royal <proyal@pace2020.com>
Sent: 07/29/03 11:26 AM
To: "Scot P. Floess" <floess@mindspring.com>
Subject: Re: [JDBC] NPE in creating a SQLException

>
> On Tuesday, July 29, 2003, at 11:17  AM, Scot P. Floess wrote:
> Can you send more code?  Is "s" initialized in your synchronized
> block?  You might print s before the synchronized block to verify...
> It could be your null pointer is there...

Sorry I didn't include more.. This is with JVM 1.4.1, so all the java.*
stuff is the standard source for that.

>>
>> I'm getting the below NPE:
>
> java.lang.NullPointerException
>          at java.io.PrintWriter.write(PrintWriter.java:247)
>          at java.io.PrintWriter.print(PrintWriter.java:392)
>          at java.io.PrintWriter.println(PrintWriter.java:529)
>          at java.lang.Throwable.printStackTrace(Throwable.java:509)
>          at java.sql.SQLException.<init>(SQLException.java:103)
>          at
> org.postgresql.util.PSQLException.<init>(PSQLException.java:19)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Conne
> c
> tion.java:480)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Conne
> c
> tion.java:461)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.setAutoCommit(AbstractJdbc
> 1
> Connection.java:942)
>
> The offense starts here (SQLException:103)
>
>           if (!(this instanceof SQLWarning)) {
>               if (DriverManager.getLogWriter() != null) {
>                     printStackTrace(DriverManager.getLogWriter());
>               }
>           }
>
> And then continues on to (Throwable:509)
>
>          synchronized (s) {
>              s.println(this);
>              StackTraceElement[] trace = getOurStackTrace();

Which is fully:

     public void printStackTrace(PrintWriter s) {
         synchronized (s) {
             s.println(this);
             StackTraceElement[] trace = getOurStackTrace();
             for (int i=0; i < trace.length; i++)
                 s.println("\tat " + trace[i]);

             Throwable ourCause = getCause();
             if (ourCause != null)
                 ourCause.printStackTraceAsCause(s, trace);
         }
     }

> It is the "s.println(this)" that is causing errors... All of this is
> happening in the constructor, so I guess that is why 'this' is null?
> I'm not 100% sure and pretty confused by it all :/

But 'this' is null.... which is very weird to me.
-pete

>

Re: NPE in creating a SQLException

From
Peter Royal
Date:
On Tuesday, July 29, 2003, at 11:50  AM, Scot P. Floess wrote:
> Does your class have a toString() method?  You mention that this is
> occurring in a constructor.  Certainly if your toString() has object
> members being printed in such a way as method calls are performed on
> those object prior to their being set to instances there could be
> issue (ie NullPointerException).  However, assuming this to be true,
> one would hope the stack trace would indicate the toString() method.

That's the rub, its not in my class :)

>> java.lang.NullPointerException
>>          at java.io.PrintWriter.write(PrintWriter.java:247)
>>          at java.io.PrintWriter.print(PrintWriter.java:392)
>>          at java.io.PrintWriter.println(PrintWriter.java:529)
>>          at java.lang.Throwable.printStackTrace(Throwable.java:509)
>>          at java.sql.SQLException.<init>(SQLException.java:103)
>>          at
>> org.postgresql.util.PSQLException.<init>(PSQLException.java:19)
>>          at
>> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Conn
>> e
>> c
>> tion.java:480)

But I think I tracked it down...

The PSQLException constructor that is being used is

    public PSQLException(String error)
    {
        super();
        translate(error, null);
    }

which does not set the 'message' variable.. Then the toString() of
PSQLException is:

    public String toString()
    {
        return message;
    }

so that is returning null.. and the cause of the NPE. I see the bug is
fixed in CVS :)

thanks for the help!
-pete


Re: NPE in creating a SQLException

From
Michael Stephenson
Date:
> > It is the "s.println(this)" that is causing errors... All of this is
> > happening in the constructor, so I guess that is why 'this' is null?
> > I'm not 100% sure and pretty confused by it all :/
>
> But 'this' is null.... which is very weird to me.

'this' isn't null, this.toString() is.

The toString() method used to return 'message' irrespective of whether it
was null.

This looks to have been fixed in CVS on March 8th, now it will return the
empty string if message is null.

Oh, and I'm not sure posting Sun code on here is legal, not everyone will
have agreed to the licence. :)

Michael

--
Web Applications Developer
Open World Ltd, 11 Riverside Court, Riverside Road, Bath, BA2 3DZ.
Tel: +44 1225 444950  Fax: +44 1225 336738  http://www.openworld.co.uk/

CONFIDENTIALITY NOTICE
The information contained in this message is confidential, intended only for
the use of the individual or the entity named as recipient. If the reader of
this message is not that recipient, you are notified that any
dissemination,
distribution or copy of this message is strictly prohibited. If you have
received this message in error, please immediately notify us by telephone on
the number above. Your co-operation is appreciated.


Re: NPE in creating a SQLException

From
Csaba Nagy
Date:
Yep, that's it ! The toString method returning null...

Cheers,
Csaba.


On Tue, 2003-07-29 at 18:16, Peter Royal wrote:
> On Tuesday, July 29, 2003, at 11:50  AM, Scot P. Floess wrote:
> > Does your class have a toString() method?  You mention that this is
> > occurring in a constructor.  Certainly if your toString() has object
> > members being printed in such a way as method calls are performed on
> > those object prior to their being set to instances there could be
> > issue (ie NullPointerException).  However, assuming this to be true,
> > one would hope the stack trace would indicate the toString() method.
>
> That's the rub, its not in my class :)
>
> >> java.lang.NullPointerException
> >>          at java.io.PrintWriter.write(PrintWriter.java:247)
> >>          at java.io.PrintWriter.print(PrintWriter.java:392)
> >>          at java.io.PrintWriter.println(PrintWriter.java:529)
> >>          at java.lang.Throwable.printStackTrace(Throwable.java:509)
> >>          at java.sql.SQLException.<init>(SQLException.java:103)
> >>          at
> >> org.postgresql.util.PSQLException.<init>(PSQLException.java:19)
> >>          at
> >> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Conn
> >> e
> >> c
> >> tion.java:480)
>
> But I think I tracked it down...
>
> The PSQLException constructor that is being used is
>
>     public PSQLException(String error)
>     {
>         super();
>         translate(error, null);
>     }
>
> which does not set the 'message' variable.. Then the toString() of
> PSQLException is:
>
>     public String toString()
>     {
>         return message;
>     }
>
> so that is returning null.. and the cause of the NPE. I see the bug is
> fixed in CVS :)
>
> thanks for the help!
> -pete
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
>                http://www.postgresql.org/docs/faqs/FAQ.html
>



Re: NPE in creating a SQLException

From
Csaba Nagy
Date:
I'm pretty sure the PSQLException#toString() method itself returns null
for some strange reason... if you take a look at PrintWriter:392,
PrintWriter:247 and String:2177, that's the only reason I can think of
based on the stack trace.
I've checked my local sources (don't know the version),
PSQLException#toString there won't return null. Can you check your
sources ?

Cheers,
Csaba.


On Tue, 2003-07-29 at 16:01, Peter Royal wrote:
> I'm getting the below NPE:
>
> java.lang.NullPointerException
>          at java.io.PrintWriter.write(PrintWriter.java:247)
>          at java.io.PrintWriter.print(PrintWriter.java:392)
>          at java.io.PrintWriter.println(PrintWriter.java:529)
>          at java.lang.Throwable.printStackTrace(Throwable.java:509)
>          at java.sql.SQLException.<init>(SQLException.java:103)
>          at
> org.postgresql.util.PSQLException.<init>(PSQLException.java:19)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connec
> tion.java:480)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connec
> tion.java:461)
>          at
> org.postgresql.jdbc1.AbstractJdbc1Connection.setAutoCommit(AbstractJdbc1
> Connection.java:942)
>
> The offense starts here (SQLException:103)
>
>     if (!(this instanceof SQLWarning)) {
>         if (DriverManager.getLogWriter() != null) {
>         printStackTrace(DriverManager.getLogWriter());
>         }
>     }
>
> And then continues on to (Throwable:509)
>
>          synchronized (s) {
>              s.println(this);
>              StackTraceElement[] trace = getOurStackTrace();
>
>
> It is the "s.println(this)" that is causing errors... All of this is
> happening in the constructor, so I guess that is why 'this' is null?
> I'm not 100% sure and pretty confused by it all :/
> -pete
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>