xumingxian
Programmer
I have a javaBean named edsSearch.Xtest1(num_ayyay). I try to use the javaBean to call a PL/SQL stored procedure named test_array( num_array ). The test_array output an oracle.sql.ARRAY num_array to the javaBean Xtest1(). At server site, I can print out the contents of num_array. I use a JSP to call the javaBean and try to display the elements of the array in the client site. It compiled OK. But I get the error as "java.lang.NullPointerException". Can anybody help me?
Source code :
/**1. Run the following scripts to create VARRAY types
and procedure before running the Java appication **/
drop procedure test_array;
drop type num_varray;
create type num_varray as varray(10) of number;
/
create or replace procedure test_array
(par1 in num_varray, par2 out num_varray) as
begin
par2 := num_varray();
par2.extend(par1.count);
for i in 1..par1.count loop
par2(i) := par1(i);
end loop;
end;
/
//2. The javaBean --
public ARRAY Xtest1()
{
try
{
if (rs != null)
{
rs.close();
}
Connect();
con.setAutoCommit (false);
// The sample passes "NUM_VARRAY" to PLSQL procedures
// and retrieves the same varray.
// materializes the object as an object of type ARRAY.
CallableStatement callStmt = con.prepareCall("{call test_array(?, ?)}");
int elements[] = {11, 22, 33, 44, 55};
ArrayDescriptor desc = ArrayDescriptor.createDescriptor("NUM_VARRAY", con);
ARRAY newArray = new ARRAY(desc, con, elements);
((OracleCallableStatement)callStmt).setArray(1, newArray);
((OracleCallableStatement)callStmt).registerOutParameter(2, OracleTypes.ARRAY,"NUM_VARRAY");
System.out.println("Registered all the parameters");
callStmt.executeQuery();
System.out.println("After Executing the Query");
System.out.println(" ");
ARRAY narray = ((OracleCallableStatement)callStmt).getARRAY(2);
//narray = ((OracleCallableStatement)callStmt).getARRAY(2);
System.out.println ("Array is of type "+narray.getSQLTypeName());
System.out.println ("Array element is of type code "+narray.getBaseType());
System.out.println ("Array element is of type "+narray.getBaseTypeName());
System.out.println ("Array is of length "+narray.length());
// get Array elements
BigDecimal[] values = (BigDecimal[]) narray.getArray();
for (int i=0; i<values.length; i++)
{
BigDecimal value = (BigDecimal) values;
System.out.println(">> index "+i+" = "+value.intValue());
}
}
catch (SQLException se)
{
System.out.println("SQL Exception: " + se.getMessage());
se.printStackTrace(System.out);
}
return narray;
}
<!--3. The JSP -->
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page language = "java" import="java.text.*" import="java.math.BigDecimal" import="oracle.sql.ARRAY" %>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>
Hello World
</TITLE>
</HEAD>
<BODY>
<H2>
The current time is:
</H2>
<P>
<% out.println((new java.util.Date()).toString()); %>
</P>
<jsp:useBean class="EDS.Search" id="edsSearch" scope="page">
<%
try
{
//oracle.sql.ARRAY myArray = null;
ARRAY myArray = edsSearch.Xtest1();
System.out.println("javaBean is executed.");
long myLen = myArray.getLength();
//ARRAY myArray = edsSearch.narray.length()
if (myLen != 0)
{
System.out.println("myArray is of length " + myLen);
}
else
{
System.out.println("The narray is empty");
}
//System.out.println( "Array length is " + myValues);
for (int i = 0; i < 2; i++)
{
//int myVal = myArray;
%>
<P>Index
</P>
<%
}
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
e.printStackTrace(System.out);
}
finally
{
edsSearch.Disconnect();
}
%>
</jsp:useBean>
</BODY>
</HTML>
<!-- End of Source code -->
Thanks in advance!
Source code :
/**1. Run the following scripts to create VARRAY types
and procedure before running the Java appication **/
drop procedure test_array;
drop type num_varray;
create type num_varray as varray(10) of number;
/
create or replace procedure test_array
(par1 in num_varray, par2 out num_varray) as
begin
par2 := num_varray();
par2.extend(par1.count);
for i in 1..par1.count loop
par2(i) := par1(i);
end loop;
end;
/
//2. The javaBean --
public ARRAY Xtest1()
{
try
{
if (rs != null)
{
rs.close();
}
Connect();
con.setAutoCommit (false);
// The sample passes "NUM_VARRAY" to PLSQL procedures
// and retrieves the same varray.
// materializes the object as an object of type ARRAY.
CallableStatement callStmt = con.prepareCall("{call test_array(?, ?)}");
int elements[] = {11, 22, 33, 44, 55};
ArrayDescriptor desc = ArrayDescriptor.createDescriptor("NUM_VARRAY", con);
ARRAY newArray = new ARRAY(desc, con, elements);
((OracleCallableStatement)callStmt).setArray(1, newArray);
((OracleCallableStatement)callStmt).registerOutParameter(2, OracleTypes.ARRAY,"NUM_VARRAY");
System.out.println("Registered all the parameters");
callStmt.executeQuery();
System.out.println("After Executing the Query");
System.out.println(" ");
ARRAY narray = ((OracleCallableStatement)callStmt).getARRAY(2);
//narray = ((OracleCallableStatement)callStmt).getARRAY(2);
System.out.println ("Array is of type "+narray.getSQLTypeName());
System.out.println ("Array element is of type code "+narray.getBaseType());
System.out.println ("Array element is of type "+narray.getBaseTypeName());
System.out.println ("Array is of length "+narray.length());
// get Array elements
BigDecimal[] values = (BigDecimal[]) narray.getArray();
for (int i=0; i<values.length; i++)
{
BigDecimal value = (BigDecimal) values;
System.out.println(">> index "+i+" = "+value.intValue());
}
}
catch (SQLException se)
{
System.out.println("SQL Exception: " + se.getMessage());
se.printStackTrace(System.out);
}
return narray;
}
<!--3. The JSP -->
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page language = "java" import="java.text.*" import="java.math.BigDecimal" import="oracle.sql.ARRAY" %>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>
Hello World
</TITLE>
</HEAD>
<BODY>
<H2>
The current time is:
</H2>
<P>
<% out.println((new java.util.Date()).toString()); %>
</P>
<jsp:useBean class="EDS.Search" id="edsSearch" scope="page">
<%
try
{
//oracle.sql.ARRAY myArray = null;
ARRAY myArray = edsSearch.Xtest1();
System.out.println("javaBean is executed.");
long myLen = myArray.getLength();
//ARRAY myArray = edsSearch.narray.length()
if (myLen != 0)
{
System.out.println("myArray is of length " + myLen);
}
else
{
System.out.println("The narray is empty");
}
//System.out.println( "Array length is " + myValues);
for (int i = 0; i < 2; i++)
{
//int myVal = myArray;
%>
<P>Index
</P>
<%
}
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
e.printStackTrace(System.out);
}
finally
{
edsSearch.Disconnect();
}
%>
</jsp:useBean>
</BODY>
</HTML>
<!-- End of Source code -->
Thanks in advance!