Home » Developer & Programmer » Precompilers, OCI & OCCI » NullReferenceException at OCIStmtExecute function
NullReferenceException at OCIStmtExecute function [message #112948] Wed, 30 March 2005 01:29 Go to next message
nemaria
Messages: 7
Registered: March 2005
Junior Member
Hi!
When I run my code, I get the following message:

"Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
At OCIStmtExecute(OCISvcCtx*, OCIStmt*, OciErr*, UInt32, UInt32,
OCISnapshot *, OCISnapshot *, UInt32)"

The message appears when I try to execute the select statement that returns something. What can be wrong?

My code:

#include "stdafx.h"

static OCIEnv *p_env;
static OCIError *p_err;
static OCISvcCtx *p_svc;
static OCIStmt *p_sql;
static OCIDefine *p_dfn = (OCIDefine *) 0;
static OCIBind *p_bnd = (OCIBind *) 0;

struct OCIEnv { };
struct OCIError { };
struct OCISvcCtx { };
struct OCIStmt { };
struct OCIDefine { };
struct OCIBind { };
struct OCISnapshot { };

/* Function prototypes for locally defined functions but with comments */

static void checkerr(OCIError *errhp, sword status);
static void cleanup(/*_ void _*/);
static void myfflush(/*_ void _*/);
void main(/*_ int argc, char *argv[] _*/);

static sword status;


void main()
{
int p_bvi;
char p_sli[20];
int rc;
char errbuf[100];
int errcode;

checkerr(p_err, OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0, /* Initialize OCI */
(dvoid * (*)(dvoid *, size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t))0,
(void (*)(dvoid *, dvoid *)) 0 ));

/* Initialize evironment */
checkerr(p_err, OCIEnvInit( (OCIEnv **) &p_env, OCI_DEFAULT, (size_t) 0, (dvoid **) 0 ));

/* Initialize handles */
checkerr(p_err, OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_err, OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0));
checkerr(p_err, OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_svc, OCI_HTYPE_SVCCTX,
(size_t) 0, (dvoid **) 0));

/* Connect to database server */
checkerr(p_err, OCILogon(p_env, p_err, &p_svc, (OraText*)"north8", 6, (OraText*)"demo", 4, (OraText*)"astusr", 6));

/* Allocate and prepare SQL statement */
checkerr(p_err, OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_sql,
OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0));

text *sqlstmt = (text *)"select ename from emp where deptno=:x" ;
deptno=:x" ;
checkerr(p_err, OCIStmtPrepare(p_sql, p_err, sqlstmt,
(ub4)strlen((char *)sqlstmt), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT));

/* Bind the values for the bind variables */
p_bvi = 10; /* Use DEPTNO=10 */
checkerr(p_err, OCIBindByName(p_sql, &p_bnd, p_err, (text *) ":x",
-1, (dvoid *) &p_bvi, sizeof(int), SQLT_INT, (dvoid *) 0,
(ub2 *) 0, (ub2 *) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));

/* Define the select list items */
checkerr(p_err, OCIDefineByPos(p_sql, &p_dfn, p_err, 1, (dvoid *) &p_sli,
(sb4)sizeof(p_sli), SQLT_STR, (dvoid *) 0, (ub2 *)0,
(ub2 *)0, OCI_DEFAULT));
/*checkerr(p_err, OCIDefineByPos(p_sql, &p_dfn, p_err, (ub4) 1, (dvoid *) &p_sli,
(sb4) sizeof(p_sli), (ub2) SQLT_STR, (dvoid *) 0, (ub2 *) 0,
(ub2 *) 0, (ub4) OCI_DEFAULT));*/

/* Execute the SQL statment */
checkerr(p_err, OCIStmtExecute(p_svc, p_sql, p_err, (ub4) 1, (ub4) 0,
(CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT));

while(rc != OCI_NO_DATA) { /* Fetch the remaining data */
printf("%s\n",&p_sli);
rc = OCIStmtFetch(p_sql, p_err, 1, 0, 0);
}

checkerr(p_err, OCILogoff(p_svc, p_err)); /* Disconnect */
checkerr(p_err, OCIHandleFree((dvoid *) p_sql, OCI_HTYPE_STMT)); /* Free handles */
checkerr(p_err, OCIHandleFree((dvoid *) p_svc, OCI_HTYPE_SVCCTX));
checkerr(p_err, OCIHandleFree((dvoid *) p_err, OCI_HTYPE_ERROR));

return;
}

void checkerr(OCIError *errhp, sword status)
{
text errbuf[512];
sb4 errcode = 0;

switch (status)
{
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
(void) printf("Error - OCI_SUCCESS_WITH_INFO\n");
break;
case OCI_NEED_DATA:
(void) printf("Error - OCI_NEED_DATA\n");
break;
case OCI_NO_DATA:
(void) printf("Error - OCI_NODATA\n");
break;
case OCI_ERROR:
(void) OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,
errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
(void) printf("Error - %.*s\n", 512, errbuf);
//exit(8);
break;
case OCI_INVALID_HANDLE:
(void) printf("Error - OCI_INVALID_HANDLE\n");
break;
case OCI_STILL_EXECUTING:
(void) printf("Error - OCI_STILL_EXECUTE\n");
break;
case OCI_CONTINUE:
(void) printf("Error - OCI_CONTINUE\n");
break;
default:
break;
}
}

[Updated on: Wed, 30 March 2005 04:38]

Report message to a moderator

Re: NullReferenceException at OCIStmtExecute function [message #115341 is a reply to message #112948] Sat, 09 April 2005 18:59 Go to previous message
Michael Hartley
Messages: 110
Registered: December 2004
Location: West Yorkshire, United Ki...
Senior Member


Your question is a reposting of [message #112681] to which I supplied a fix to your question with an explanation.


Kind regards,
Michael Hartley, http://www.openfieldsolutions.co.uk
Previous Topic: query does not work
Next Topic: Libraries - Trying to find some equivalencies
Goto Forum:
  


Current Time: Tue Dec 17 22:57:43 CST 2024