Results 1 to 7 of 7
  1. #1
    murphyjirus is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default Error connecting to postgresql

    Error Displayed:

    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at StartOsmis.main(StartOsmis.java:66)
    Caused by: java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at za.co.kgatelopele.ossms.database.DBHelper.<init>(D BHelper.java:75)
    at za.co.kgatelopele.ossms.view.OsmisFrame.main(Osmis Frame.java:1135)
    ... 5 more

    Source Code:

    package za.co.kgatelopele.ossms.database;

    import za.co.kgatelopele.ossms.datamodel.Proxy;
    import java.sql.*;
    import java.util.*;
    import java.io.*;

    /**TODO: move the dirty list stuff into a class of its own, as the DBHelper should consist of purely connection pool functionality. (Rename dbhelper class).
    * add transaction handling stuff to the appropriate places
    * make statements preparedStatements
    * define a helper interface
    * use java logging rather then system.err...
    * add methods as neded to proxy, helper, instance interfaces
    */
    public class DBHelper
    {
    public DBHelper(String filePath)
    {
    this.filePath = filePath;
    DBHelper.prop = new Properties();

    try
    {
    File propFile = new File(filePath);
    if (propFile.exists())
    {
    prop.load(new FileInputStream(propFile));
    }
    else
    {
    System.out.println("Global file not found");
    System.exit(-1);
    }
    }
    catch (FileNotFoundException fe)
    {
    fe.printStackTrace();
    }
    catch (IOException io )
    {
    io.printStackTrace();
    }

    DBHelper.user = prop.getProperty("postgres");
    DBHelper.password = prop.getProperty("123@abc");
    DBHelper.databaseURL = prop.getProperty("jdbc:postgresql://localhost:5432/postgres");

    try
    {
    Class.forName(prop.getProperty("postgresql.Driver" ));
    }
    catch (ClassNotFoundException cnfe)
    {
    System.err.println("Unable to initialize class");
    cnfe.printStackTrace();
    System.exit(-1);
    }
    }

    static Properties prop;
    static String databaseURL;
    static String user;
    static String password;
    static String filePath;

    // create a new connection. This should be upgraded to become a connection pool so that the overhead of
    // creating connecitons is removed, but anyway...
    public static Connection getConnection()
    {
    try
    {
    return DriverManager.getConnection(databaseURL,user,passw ord);
    }
    catch (SQLException se)
    {
    System.err.println("fatal error creating connection");
    se.printStackTrace();
    System.exit(-1);
    return null;
    }
    }

    public static void freeConnection(Connection conn)
    {
    try
    {
    conn.close();
    }
    catch (SQLException se)
    {
    System.err.println("fatal error freeing connection");
    se.printStackTrace();
    System.exit(-1);
    }
    }

    static Set dirtySet = new HashSet();

    public static void markDirty(Proxy p)
    {
    dirtySet.add(p);
    }

    public static void commitAllDirty()
    {
    Iterator i=dirtySet.iterator();
    while (i.hasNext())
    ((Proxy) i.next()).commitToDatabase();
    dirtySet.clear();
    }

    }

    i need help

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Try printing out
    Java Code:
    prop.getProperty("postgresql.Driver" )
    I have the feeling it is producing null, meaning that that key is not defined. If prop were null, the null pointer exception stack trace would start there, rather than inside of the Class.forName0 method.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    (DBHelper.java:75)
    What code is at line 75?

  4. #4
    murphyjirus is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default What is at line 75?

    The following code is what is at line 75


    Code:
    Class.forName(prop.getProperty("postgresql.Driver" ));

    hope this helps

  5. #5
    murphyjirus is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default prop is a Properties

    the declaration for the properties is the prop. please kindly help me. i still have error on my program

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Like I said do
    Java Code:
    System.out.println("Driver Class: " + prop.getProperty("postgresql.Driver" ));
    right before
    Java Code:
    Class.forName(prop.getProperty("postgresql.Driver" ));
    I can guarantee that it is null.

    One thing though, if your class variables are static, they should be defined in a static block, not a constructor.
    Last edited by masijade; 10-31-2008 at 07:21 AM.

  7. #7
    murphyjirus is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Like I said do
    Code:

    System.out.println("Driver Class: " + prop.getProperty("postgresql.Driver" ));

    right before
    Code:

    Class.forName(prop.getProperty("postgresql.Driver" ));

    I can guarantee that it is null.
    i tried it but displayed null. tank you.

Similar Threads

  1. Error In RAD while connecting Hibernate
    By tobalajia in forum JDBC
    Replies: 0
    Last Post: 05-08-2008, 03:37 PM
  2. PostgreSQL driver
    By bugger in forum JDBC
    Replies: 3
    Last Post: 01-11-2008, 09:45 AM
  3. Error while connecting to a database
    By Fukushuusha in forum JDBC
    Replies: 2
    Last Post: 12-16-2007, 09:40 AM
  4. Error While connecting to Oracle thru JAVA
    By javaneed in forum Advanced Java
    Replies: 1
    Last Post: 08-11-2007, 10:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •