Thread: java.lang.NullPointerException

java.lang.NullPointerException

From
"Kevin W. Nikiforuk"
Date:
-----BEGIN PGP SIGNED MESSAGE-----

Forgive me if this is the wrong mailing list for this, but it seemed the
most appropriate.

I've got a database up and running under PostgreSQL called kwn.  When I
try to use the following Java code to connect to it:

    try {
      Class.forName("postgresql.Driver");
    }
    catch (Exception e) {
      System.out.println(e);
    }

    postgresql.Connection conn = null;

    try {
      conn =
(postgresql.Connection)DriverManager.getConnection("jdbc:postgresql:kwn");
    }
    catch (Exception e) {
      System.out.println(e);
      e.printStackTrace();
    }

I get the following stack trace from the second try block:

java.lang.NullPointerException:
java.lang.NullPointerException:
        at java.lang.String.<init>(String.java)
        at postgresql.Connection.<init>(Connection.java:71)
        at postgresql.Driver.connect(Driver.java:87)
        at java.sql.DriverManager.getConnection(DriverManager.java:91)
        at java.sql.DriverManager.getConnection(DriverManager.java:149)
        at CookBook.<init>(CookBook.java:165)
        at CookBook.main(CookBook.java:335)

==============================================================================

The program was compiled with the command line:

javac -d . -classpath /usr/share/java/postgresql.jar:/usr/lib/jdk-1.1.3/classes.zip:. test.java

and my CLASSPATH env variable is:
/usr/lib/jdk-1.1.3/lib/classes.zip:/usr/share/java/postgresql.jar:.

As far as I can tell, the Class.forName() call doesn't throw any
exceptions.

Ideas, hints or suggestions greatly appreciated.

Kevin

- --
Kevin Nikiforuk                    knikifor@gpu.srv.ualberta.ca
- ----------------------------------------------------------------------------
    http://pgp5.ai.mit.edu/pks-commands-beta.html for my PGP public key

-----BEGIN PGP SIGNATURE-----
Version: 2.6.2
Comment: Processed by mkpgp2.1, a Pine/PGP interface.

iQCVAwUBNWMqUMsSwbLUdSRVAQHRoQP/U+JxivVNCxS3t4vPAtZyLTkS7TTceh1v
e3da72tXLmNIODfWcuo8f19p4yoIeOUjOSu13HlKgO0s5lX94dbDe6aB4fly/tGu
ufVB2jdlm8KUT6LInfnx84eQj9BdT82NfWC9waS5I/Q9b+YybLyzwL65tWI8L/8z
BmkG9Byrtoc=
=mv4I
-----END PGP SIGNATURE-----


Re: [GENERAL] java.lang.NullPointerException

From
"Kevin W. Nikiforuk"
Date:
-----BEGIN PGP SIGNED MESSAGE-----

On Wed, 20 May 1998, Kevin W. Nikiforuk wrote:

>     try {
>       conn =
> (postgresql.Connection)DriverManager.getConnection("jdbc:postgresql:kwn");
>     }
>     catch (Exception e) {
>       System.out.println(e);
>       e.printStackTrace();
>     }

I managed to correct the problem by changing the argument passed to
getConnection() to:

"jdbc:postgresql:kwn", "kwn", ""

where kwn is my userid on my Linux box.

Kevin

- --
Kevin Nikiforuk                    knikifor@gpu.srv.ualberta.ca
- ----------------------------------------------------------------------------
    http://pgp5.ai.mit.edu/pks-commands-beta.html for my PGP public key

-----BEGIN PGP SIGNATURE-----
Version: 2.6.2
Comment: Processed by mkpgp2.1, a Pine/PGP interface.

iQCVAwUBNWNNJcsSwbLUdSRVAQEvRwP8CfsCmmrB0XB+ZbjLOnke/gzAtMXMIP5l
I8VNM+l5+VCp48unO1qb650pzuttqpLsp2hQsFNK5aFG/6eIRGAjNe8nvZP06t/2
hXRVTBKTqyZVRxYeCGyQLhpWDk9KF4/B/5M60DQ14VFcJFCH8vXyY5aBHNSAiUMX
NTJZjPnWLzI=
=17jH
-----END PGP SIGNATURE-----