Java Forum / General / September 2007
How do i get rid of this error!
kath - 17 Sep 2007 06:06 GMT hi,
1. I have a stateless session bean(TestBean) 2. I have helper classes(TestHelper, which implements operations like db connectivity, retrieve/update/insert data from/to db) 3. I have a model class(say, TestModel), which is a complext datastructure. I has attributes(members) like (String, ArrayLList)
Now i have a business method in bean(TestBean), say getSomthing(). when this business method is called it creates an object of the helper class(TestHelper), and calls its member function(say, dbGetSomething()). Where, dbGetSomething returns array of my model class(TestModel[]).
Now i will show, psuedo code of the above explained, class TestHelper {
public TestHelper(){ // establishes a connection to db, and works fine ... }
public TestModel[] dbGetSomething(){ ArrayList list = new ArrayList(); // Create a prepare statement, for SELECT query // execute above prepare statement. while(rs.next()){ TestModel t = new TestModel();
// set attributes of class from record set. ...
list.add(t); } TestModel[] model = new TestModel[ list.size() ]; list.toArray(model);
return model; } }
Now this function works fine when i have some results for the SELECT query. But NullPointerException otherwise.
So, i did which replaces code after while statement like, if ( list.isEmpty() ){ TestModel temp = new TestModel(); TestModel tModel = new TestModel[1];
tModel[0] = temp; return tModel; }else{ TestModel[] model = new TestModel[ list.size() ]; list.toArray(model);
return model; }
But still i get NullPointerException...
can any one pls explain, Why im getting NullPointerException, even though i returning a value of object-reference, which i have created.
thanks in advance, kath.
mmoski - 17 Sep 2007 06:11 GMT > hi, > [quoted text clipped - 65 lines] > thanks in advance, > kath. Which line is throwing the exception?
kath - 17 Sep 2007 06:33 GMT > Which line is throwing the exception? hi, thanks for quick reply... sorry, i forgot to mention that. Actually i have business method called getSomething() which throws this exception.
and the implementation of the same is like, try { TestHelper modelDB = (TestHelper ) Class .forName("com.pak.test.persistence.TestHelper ") .newInstance();
// here im getting exception. return modelDB.dbGetSomething(); } catch (NullPointerException e) {
... }
thanks in advance, kath.
kcwong - 17 Sep 2007 07:30 GMT > Now i will show, psuedo code of the above explained, Please don't post psuedo code... you could be omitting the part of the code containing the error. If there are anything sensitive, change the names and such before posting.
It would also make the diagnosis much easier if you could post the *complete* stack trace. Wrap the block of code you suspect to contain the error in a try...catch block (catch Exception), and call printStackTrace() to output the complete stack trace to the error stream.
kath - 17 Sep 2007 07:46 GMT > > Now i will show, psuedo code of the above explained, > [quoted text clipped - 7 lines] > printStackTrace() to output the complete stack trace to the error > stream. ok, here is the code, which is webservice method in bean class,
public SBRModel[] getCreatedScenarioRequests() throws SBRException{ try { SBRdbconn modelDB = (SBRdbconn) Class .forName("com.sap.replication.sbr.persistence.SBRdbconn") .newInstance(); SBRModel[] model; return modelDB.dbGetCreatedScenarioRequests();
} catch (NullPointerException e) { e.printStackTrace(); Throwable ee = e.fillInStackTrace(); ee.printStackTrace(); throw new SBRException( "Unable to get created scenario projects! " + e.getLocalizedMessage() +"\ngetLocalizedMessage(): "+ee.getLocalizedMessage() +"\ngetMessage: "+ee.getMessage() +"\ntoString: "+ee.toString()); }
and once i execute the webservice i get the response as, ----------------------------------------------------------------------------------------- HTTP/1.1 500 Internal Server Error Connection: close Server: SAP J2EE Engine/7.00 Content-Type: text/xml; charset=UTF-8 Set-Cookie: <value is hidden> Date: Mon, 17 Sep 2007 06:39:02 GMT
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/ envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><SOAP- ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</ faultcode><faultstring>Unable to get created scenario projects! null getLocalizedMessage(): null getMessage: null toString: java.lang.NullPointerException</ faultstring><detail><ns1:getCreatedScenarioRequests_com.sap.replication.sbr.model.SBRException xmlns:ns1='urn:AproSBRWsd/AproSBRVi'></ ns1:getCreatedScenarioRequests_com.sap.replication.sbr.model.SBRException></ detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope> -----------------------------------------------------------------------------------------
kath.
kath - 17 Sep 2007 07:58 GMT > > Now i will show, psuedo code of the above explained, > [quoted text clipped - 7 lines] > printStackTrace() to output the complete stack trace to the error > stream. im sorry, if it is really big(really it is... ;)) ok, let me give a small description. before you go through it
- getCreatedScenarioRequests() is a webservice which, calls dbGetCreatedScenarioRequests() method. - dbGetCreatedScenarioRequests returns an array of model class. - finally at the end i have shown what i get as response.
public SBRModel[] dbGetCreatedScenarioRequests() throws SQLException, SBRException{ ArrayList reqList = new ArrayList();
PreparedStatement ps1 = null; ResultSet rs1 = null; PreparedStatement ps2 = null; ResultSet rs2 = null; PreparedStatement ps3 = null; ResultSet rs3 = null; PreparedStatement ps4 = null; ResultSet rs4 = null; PreparedStatement ps5 = null; ResultSet rs5 = null;
try{ ps1 = conn.prepareStatement ("select SCENARIO_ID, INUMBER, CREATED_TIME, EXPIRE_DATE, DESCRIPTION, EMAIL_ID, " +"GROUP, CODELINE_ID, LOC_ID, CREATED_DATE, SCENARIO_NAME from REPL_SCENARIO_MASTER_TEMP where EDITED_STATUS=?");
ps2 = conn.prepareStatement ("select LOC_NAME FROM REPL_LOCATION_MASTER where LOC_ID=?");
ps3 = conn.prepareStatement ("select PROJECT_ID from REPL_SCENARIO_PROJECTS where SCENARIO_ID=?");
ps4 = conn.prepareStatement ("select CODELINE_NAME from REPL_CODELINE_MASTER where CODELINE_ID=?");
ps5 = conn.prepareStatement ("select PROJECT_NAME from REPL_CODELINE_PROJECTS where PROJECT_ID=?");
ps1.setString(1, SBRConstants.STATUS_CREATED); rs1 = ps1.executeQuery(); // if (!rs1.next()){ // reqList.add(new SBRModel()); // } // else{ while(rs1.next()){ SBRModel obj = new SBRModel();
obj.setScenarioID(rs1.getString(1)); obj.setUserID(rs1.getString(2)); obj.setCreatedTime(rs1.getTime(3).toString()); obj.setExpireDate(rs1.getDate(4).toString()); obj.setDescription(rs1.getString(5)); obj.setUserEmail(rs1.getString(6)); obj.setGroupID(rs1.getString(7)); obj.setCodelineID( String.valueOf(rs1.getInt(8)) ); obj.setCreatedDate(rs1.getDate(10).toString()); obj.setScenarioName(rs1.getString(11));
ps2.setInt(1, rs1.getInt(9)); rs2 = ps2.executeQuery();
rs2.next(); obj.setLocation( rs2.getString(1) );
//ps4.setInt(1, Integer.parseInt(obj.getCodelineID().trim()) ); ps4.setInt(1, rs1.getInt(8)); rs4 = ps4.executeQuery();
rs4.next(); obj.setCodelineName(rs4.getString(1));
ps3.setString(1, obj.getScenarioID()); rs3 = ps3.executeQuery();
while( rs3.next() ){ ps5.setInt(1, rs3.getInt(1)); rs5 = ps5.executeQuery(); rs5.next();
obj.createProjectIDList.add(rs5.getString(1)); } reqList.add(obj); } //} }catch (SQLException e){ Throwable ee = e.fillInStackTrace(); ee.printStackTrace(); throw new SBRException ("SBRException:\n--------------------" +e.getMessage() + e.getErrorCode() + e.getSQLState() +"\ngetLocalizedMessage(): "+ee.getLocalizedMessage() +"\ngetMessage: "+ee.getMessage() +"\ntoString: "+ee.toString() +"\n--------------------" ); } -----------------------------------------------------------------------------------------
ok, here is the code, which is webservice method in bean class,
public SBRModel[] getCreatedScenarioRequests() throws SBRException{ try { SBRdbconn modelDB = (SBRdbconn) Class .forName("com.sap.replication.sbr.persistence.SBRdbconn") .newInstance(); SBRModel[] model; return modelDB.dbGetCreatedScenarioRequests();
} catch (NullPointerException e) { e.printStackTrace(); Throwable ee = e.fillInStackTrace(); ee.printStackTrace(); throw new SBRException( "Unable to get created scenario projects! " + e.getLocalizedMessage() +"\ngetLocalizedMessage(): "+ee.getLocalizedMessage() +"\ngetMessage: "+ee.getMessage() +"\ntoString: "+ee.toString()); }
and once i execute the webservice i get the response as, ----------------------------------------------------------------------------------------- HTTP/1.1 500 Internal Server Error Connection: close Server: SAP J2EE Engine/7.00 Content-Type: text/xml; charset=UTF-8 Set-Cookie: <value is hidden> Date: Mon, 17 Sep 2007 06:39:02 GMT
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/ envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ><SOAP- ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</ faultcode><faultstring>Unable to get created scenario projects! null getLocalizedMessage(): null getMessage: null toString: java.lang.NullPointerException</ faultstring><detail><ns1:getCreatedScenarioRequests_com.sap.replication.sbr.model.SBRException xmlns:ns1='urn:AproSBRWsd/AproSBRVi'></ ns1:getCreatedScenarioRequests_com.sap.replication.sbr.model.SBRException></ detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope> -----------------------------------------------------------------------------------------
kath.
kcwong - 17 Sep 2007 09:02 GMT <snip>
> ----------------------------------------------------------------------------------------- > HTTP/1.1 500 Internal Server Error [quoted text clipped - 17 lines] > detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope> > ----------------------------------------------------------------------------------------- Could you get the error log from the server? It contains the output from the printStackTrace calls, which can tell the file and line number where the NullPointerException occurred.
kath - 17 Sep 2007 12:18 GMT > Could you get the error log from the server? It contains the output > from the printStackTrace calls, which can tell the file and line > number where the NullPointerException occurred. Hi kcwong,
thanks for your time. I could find myself, what was throwing NullPointerException,
After my catch (NullPointerException e){...}, from my code, i had finally block, where i was closing rs2, rs3, rs4, rs5 result sets. If there are no recordsets, then control will not go into while() loop, hence, all rs2, rs3, rs4, rs5 will still have null. And i was trying to close these result sets, which points to nothing. Hence the exception.
thanks for your time, I surely would look into server log file in the future...
thank you and best regards, kath.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|