Solved

Null pointer exception when setting up a global oracle data source

Posted on 2009-06-30
6
521 Views
Last Modified: 2012-05-07
Hi,

In the following code i am trying to create a global oracle connection. But I am getting a null pointer exception when setting the oracle url. Any ideas why, also what is the best practise of creating a global database connection which can be used in all parts of the code
public class CheckFirst {
  
 
 
// Set up Oracle Data Structures
 
public static OracleDataSource ods;
public static Connection conn; 
public static ResultSet rset;
public static Statement stmt;
 
//Set up Teradata Data Structures
 
 
 
public static void openOrclConn()
{
	try
    {	
	ods.setURL("jdbc:oracle:thin:@//alpha.beta.abc.com:1521/alpha");
	ods.setUser("abc123");
	ods.setPassword("abc123");		
	ods.getConnection();
	conn.setAutoCommit (false);
	stmt = conn.createStatement ();
    } catch (Exception e) {
        e.printStackTrace();
   }
}
 
public static void closeOrclConn()
{
	try
    {
	rset.close();
	stmt.close();
	conn.close();
    } catch (Exception e) {
        e.printStackTrace();
   }
}
 
 
public static void main (String args []) 
 
{
	
	openOrclConn();
	
}
 
}

Open in new window

0
Comment
Question by:anshuma
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:sirtam
ID: 24748235
Hi.

Could you please post the error message?
0
 

Author Comment

by:anshuma
ID: 24749356
java.lang.NullPointerException
      at CheckFirst.openOrclConn(CheckFirst.java:58)
      at CheckFirst.main(CheckFirst.java:87)

The line numbers can be off a little bit because I removed two three lines when I posted the code

Its happening in this line

      ods.setURL("jdbc:oracle:thin:@//bia.vip.ebay.com:1521/bia");


0
 

Author Comment

by:anshuma
ID: 24749437
ok this is what i did and it seems to have fixed it

I added the line

ods = new OracleDataSource();  in the function

public static void openOrclConn()
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Expert Comment

by:sirtam
ID: 24749698
Yes, you cannot open an object before making it.

When you are using object like this the variable will be there, but empty, therefore you will get an null pointer.

And a tip:
If possible I would try to declare the variables  ods, conn, rset and stmt as not static.
If you use static variables you say its only one (ever) object of it, and that can make problems later.
0
 
LVL 2

Accepted Solution

by:
sirtam earned 500 total points
ID: 24749714
But to make this work you'll have to make all the mothods (except main) not static too. Dont bother fixing it if it works.

An example how to write it:
public class CheckFirst {
  
 
 
// Set up Oracle Data Structures
 
public OracleDataSource ods;
public Connection conn; 
public ResultSet rset;
public Statement stmt;
 
//Set up Teradata Data Structures
 
 
 
public void openOrclConn()
{
        try
    {   
        ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:@//alpha.beta.abc.com:1521/alpha");
        ods.setUser("abc123");
        ods.setPassword("abc123");              
        ods.getConnection();
        conn.setAutoCommit (false);
        stmt = conn.createStatement ();
    } catch (Exception e) {
        e.printStackTrace();
   }
}
 
public void closeOrclConn()
{
        try
    {
        rset.close();
        stmt.close();
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
   }
}
 
 
public static void main (String args []) 
 
{
        
        openOrclConn();
        
}
 
}

Open in new window

0
 

Author Closing Comment

by:anshuma
ID: 31598463
Thanks alot for this
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now