Home » Archived » BIRT » For Jason:Null Ptr Exceptn on running MS SQL Query from a scripted database
For Jason:Null Ptr Exceptn on running MS SQL Query from a scripted database [message #201839] |
Mon, 20 November 2006 04:20  |
Eclipse User |
|
|
|
Hi,
I am getting a Null Pointer Exception when I try to preview the results of
a scripted dataset. The java class for my scripted datasource executes a
query to get results from SQL Server, get the results and return that to
BIRT in form of array of POJOs. The NPe error happens on the line that
executes the SQL Query. I dont get this NPE error when I run the class
directly as a Java application.
Steps to re-create the issue:
Wrote up a Java class (say MyDataSource.java) that
- gets connection object for connection to SQL Server in it's constructor.
the connection object is a class member
- Has a public getData() method that executes a SQL query, loads up the
results in an Array of POJOs and returns the array.
I run this class from the command prompt. It works fine i.e. no Null
pointer exception. Connection is established, query executed, and results
returned.
Next:
- I created a .rptdesign file.
- Added a scripted Datasource and a scripted dataset.
- Specified the output column names for the scripted dataset
- Implemented the dataset javascript methods of Open, Fetch and Close to
instantiate my java Class MyDataSource.java, and call the public getData()
method of this class that returns the Array of POJOs to BIRT. All these
steps are as per the tutorial for creating scripted datasources.
Now, when I double click on the scripted data set and try to preview the
results, I get a Null Pointer Exception in getData() method of
MyDataSource.java.
In my class file, I could narrow down the issue to the try/catch block
where I prepare the SQL statement and execute it... have pasted the code
below. NOTE that the connection object is a class member of my java class
and has been created in the constructor of the class.
// Code of public method of MyDataSource.getData()
public GraphData[] getData() {
GraphData[] returnData = new GraphData[24];
String sql = "select time, plot_1, plot_2 from plot_data_table";
try {
pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
// Next step is to assemble the result returned into the Array List of
GraphData class
}
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
// returning the GraphData POJOs as a plain array
return returnData;
}
}
// Java Script code of the scripted data set::
OPEN():
importPackage(Packages.mypackage);
count = 0;
datasource = new
GraphDataSource("jdbc:sqlserver://localhost\SQLEXPRESS;databaseName=cace_nuh",
"remoteuser", "centricity");
resultset = datasource.getData();
FETCH():
if (count < resultset.length){
row["time"] = resultset[count].getTime();
row["Plot_1"] = resultset[count].getPlot_1();
row["Plot_2"] = resultset[count].getPlot_2();
count ++;
return true;
}
return false;
|
|
|
Re: For Jason:Null Ptr Exceptn on running MS SQL Query from a scripted database [message #201878 is a reply to message #201839] |
Mon, 20 November 2006 10:30   |
Eclipse User |
|
|
|
Can you test the connection to see if it is null?
Verify that the sql server driver is in the classpath for BIRT.
Jason
"Pratyusha Pallavi" <pratyusha.pallavi@med.ge.com> wrote in message
news:2590a2bcc97eafcec8e95ab8b599ce12$1@www.eclipse.org...
> Hi,
>
> I am getting a Null Pointer Exception when I try to preview the results of
> a scripted dataset. The java class for my scripted datasource executes a
> query to get results from SQL Server, get the results and return that to
> BIRT in form of array of POJOs. The NPe error happens on the line that
> executes the SQL Query. I dont get this NPE error when I run the class
> directly as a Java application.
>
>
> Steps to re-create the issue:
> Wrote up a Java class (say MyDataSource.java) that - gets connection
> object for connection to SQL Server in it's constructor. the connection
> object is a class member
> - Has a public getData() method that executes a SQL query, loads up the
> results in an Array of POJOs and returns the array.
>
> I run this class from the command prompt. It works fine i.e. no Null
> pointer exception. Connection is established, query executed, and results
> returned.
>
> Next:
> - I created a .rptdesign file. - Added a scripted Datasource and a
> scripted dataset. - Specified the output column names for the scripted
> dataset
> - Implemented the dataset javascript methods of Open, Fetch and Close to
> instantiate my java Class MyDataSource.java, and call the public getData()
> method of this class that returns the Array of POJOs to BIRT. All these
> steps are as per the tutorial for creating scripted datasources.
> Now, when I double click on the scripted data set and try to preview the
> results, I get a Null Pointer Exception in getData() method of
> MyDataSource.java.
>
> In my class file, I could narrow down the issue to the try/catch block
> where I prepare the SQL statement and execute it... have pasted the code
> below. NOTE that the connection object is a class member of my java class
> and has been created in the constructor of the class.
>
> // Code of public method of MyDataSource.getData()
> public GraphData[] getData() {
> GraphData[] returnData = new GraphData[24]; String sql = "select time,
> plot_1, plot_2 from plot_data_table";
>
> try {
> pstmt = con.prepareStatement(sql);
> ResultSet rs = pstmt.executeQuery();
> while (rs.next()) {
>
> // Next step is to assemble the result returned into the Array List of
> GraphData class
> }
> con.close();
> } catch (SQLException e) {
> e.printStackTrace();
> }
>
> // returning the GraphData POJOs as a plain array
> return returnData;
> }
> }
>
>
>
> // Java Script code of the scripted data set::
> OPEN():
> importPackage(Packages.mypackage);
> count = 0;
> datasource = new
> GraphDataSource("jdbc:sqlserver://localhost\SQLEXPRESS;databaseName=cace_nuh",
> "remoteuser", "centricity");
> resultset = datasource.getData();
>
> FETCH():
> if (count < resultset.length){
> row["time"] = resultset[count].getTime();
> row["Plot_1"] = resultset[count].getPlot_1();
> row["Plot_2"] = resultset[count].getPlot_2();
> count ++;
> return true;
> }
> return false;
>
|
|
|
Re: For Jason:Null Ptr Exceptn on running MS SQL Query from a scripted database [message #202746 is a reply to message #201878] |
Thu, 23 November 2006 08:48   |
Eclipse User |
|
|
|
Hi Jason,
I have included the driver in the class path.
The connection should be fine, because I can create a JDBC datasoure in
BIRT with the same url, username and password and when I click on the the
Test Connection button, I get a connection OK.
Also , when I run the scripted datasource class file directly from the
command prompt or even from within eclipse as a java application, it
returns the reqired data from the database.
But, how do I check for a scripted datasource whether the connection
object that is instantiated in the constructor of the scripted datasource
class is not null when the readData() method of the scripted data source
class is called from open() method of the dataset? Here is my open()
method code.... the parameters to the GraphDataSource constructor are the
connection parameters.
importPackage(Packages.mypackage);
count = 0;
datasource = new
GraphDataSource("jdbc:sqlserver://localhost\SQLEXPRESS;databaseName=cace_nuh",
"remoteuser", "centricity");
resultset = datasource.readData();
|
|
|
Re: For Jason:Null Ptr Exceptn on running MS SQL Query from a scripted database [message #203021 is a reply to message #202746] |
Mon, 27 November 2006 10:51  |
Eclipse User |
|
|
|
Try putting
if (datasource){
resultset = datasource.readData();
}
Can you post the complete error you are getting?
Jason
"Pratyusha Pallavi" <pratyusha.pallavi@med.ge.com> wrote in message
news:3882a21d416387781bdf62cb9ef52f06$1@www.eclipse.org...
> Hi Jason,
>
> I have included the driver in the class path.
> The connection should be fine, because I can create a JDBC datasoure in
> BIRT with the same url, username and password and when I click on the the
> Test Connection button, I get a connection OK.
> Also , when I run the scripted datasource class file directly from the
> command prompt or even from within eclipse as a java application, it
> returns the reqired data from the database.
>
> But, how do I check for a scripted datasource whether the connection
> object that is instantiated in the constructor of the scripted datasource
> class is not null when the readData() method of the scripted data source
> class is called from open() method of the dataset? Here is my open()
> method code.... the parameters to the GraphDataSource constructor are the
> connection parameters.
>
> importPackage(Packages.mypackage);
> count = 0;
> datasource = new
> GraphDataSource("jdbc:sqlserver://localhost\SQLEXPRESS;databaseName=cace_nuh",
> "remoteuser", "centricity");
> resultset = datasource.readData();
>
>
>
|
|
|
Goto Forum:
Current Time: Wed May 07 12:40:52 EDT 2025
Powered by FUDForum. Page generated in 0.25240 seconds
|