?
Solved

Null pointer exception when setting up a global oracle data source

Posted on 2009-06-30
6
Medium Priority
?
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 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