org.springframework.batch.item.ItemStreamException: Failed to initialize the reader

Spring JIRA | Luke Woodward | 1 year ago
  1. 0

    The following code uses a StoredProcedureItemReader to connect to an Oracle database and call a stored procedure that returns a ref cursor in a parameter: {code:title=SpringItemReaderTest.java} package springitemreadertest; import java.util.Map; import javax.sql.DataSource; import oracle.jdbc.OracleTypes; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.database.StoredProcedureItemReader; import org.springframework.jdbc.core.ColumnMapRowMapper; import org.springframework.jdbc.core.SqlParameter; public class SpringItemReaderTest { public static void main(String[] args) throws Exception { DataSource dataSource = new MyDataSource(); StoredProcedureItemReader<Map<String, Object>> storedProcedureItemReader = new StoredProcedureItemReader<>(); storedProcedureItemReader.setDataSource(dataSource); storedProcedureItemReader.setProcedureName("PROC"); storedProcedureItemReader.setParameters(new SqlParameter[] { new SqlParameter(/*"p_out",*/ OracleTypes.CURSOR) }); storedProcedureItemReader.setRefCursorPosition(1); storedProcedureItemReader.setRowMapper(new ColumnMapRowMapper()); storedProcedureItemReader.open(new ExecutionContext()); storedProcedureItemReader.read(); } } {code} The stored procedure {{PROC}} doesn't do anything useful. Here's its definition: {code} procedure PROC (p_out OUT SYS_REFCURSOR) is begin open p_out for select * from dual; end; {code} The class MyDataSource isn't particularly interesting either. It implements javax.sql.DataSource but has default implementations for all methods other than {{getConnection()}}. Any DataSource that connects to an Oracle database can be used instead of it. When I run this code I get the following stacktrace: {noformat} Exception in thread "main" org.springframework.batch.item.ItemStreamException: Failed to initialize the reader at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147) at springitemreadertest.SpringItemReaderTest.main(SpringItemReaderTest.java:20) Caused by: java.lang.NullPointerException at org.springframework.jdbc.core.metadata.CallMetaDataContext.reconcileParameters(CallMetaDataContext.java:319) at org.springframework.jdbc.core.metadata.CallMetaDataContext.processParameters(CallMetaDataContext.java:293) at org.springframework.batch.item.database.StoredProcedureItemReader.openCursor(StoredProcedureItemReader.java:168) at org.springframework.batch.item.database.AbstractCursorItemReader.doOpen(AbstractCursorItemReader.java:406) at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ... 1 more {noformat} Of course, to fix the code, uncomment the parameter name I deliberately commented out. However, if I have unintentionally missed out the parameter name, or specified the parameter name and type the wrong way around (hence setting the type name instead of the parameter name), it would have been nice to have got an exception with a message that told me I needed to set a name for the SqlParameter. Incidentally, issue SPR-4814, which I found while seeing if this has already been reported, contains the following comment: bq. CallMetaDataContext reconcileParameters should check that a name exists for parameter to avoid NPE

    Spring JIRA | 1 year ago | Luke Woodward
    org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
  2. 0

    The following code uses a StoredProcedureItemReader to connect to an Oracle database and call a stored procedure that returns a ref cursor in a parameter: {code:title=SpringItemReaderTest.java} package springitemreadertest; import java.util.Map; import javax.sql.DataSource; import oracle.jdbc.OracleTypes; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.database.StoredProcedureItemReader; import org.springframework.jdbc.core.ColumnMapRowMapper; import org.springframework.jdbc.core.SqlParameter; public class SpringItemReaderTest { public static void main(String[] args) throws Exception { DataSource dataSource = new MyDataSource(); StoredProcedureItemReader<Map<String, Object>> storedProcedureItemReader = new StoredProcedureItemReader<>(); storedProcedureItemReader.setDataSource(dataSource); storedProcedureItemReader.setProcedureName("PROC"); storedProcedureItemReader.setParameters(new SqlParameter[] { new SqlParameter(/*"p_out",*/ OracleTypes.CURSOR) }); storedProcedureItemReader.setRefCursorPosition(1); storedProcedureItemReader.setRowMapper(new ColumnMapRowMapper()); storedProcedureItemReader.open(new ExecutionContext()); storedProcedureItemReader.read(); } } {code} The stored procedure {{PROC}} doesn't do anything useful. Here's its definition: {code} procedure PROC (p_out OUT SYS_REFCURSOR) is begin open p_out for select * from dual; end; {code} The class MyDataSource isn't particularly interesting either. It implements javax.sql.DataSource but has default implementations for all methods other than {{getConnection()}}. Any DataSource that connects to an Oracle database can be used instead of it. When I run this code I get the following stacktrace: {noformat} Exception in thread "main" org.springframework.batch.item.ItemStreamException: Failed to initialize the reader at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147) at springitemreadertest.SpringItemReaderTest.main(SpringItemReaderTest.java:20) Caused by: java.lang.NullPointerException at org.springframework.jdbc.core.metadata.CallMetaDataContext.reconcileParameters(CallMetaDataContext.java:319) at org.springframework.jdbc.core.metadata.CallMetaDataContext.processParameters(CallMetaDataContext.java:293) at org.springframework.batch.item.database.StoredProcedureItemReader.openCursor(StoredProcedureItemReader.java:168) at org.springframework.batch.item.database.AbstractCursorItemReader.doOpen(AbstractCursorItemReader.java:406) at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ... 1 more {noformat} Of course, to fix the code, uncomment the parameter name I deliberately commented out. However, if I have unintentionally missed out the parameter name, or specified the parameter name and type the wrong way around (hence setting the type name instead of the parameter name), it would have been nice to have got an exception with a message that told me I needed to set a name for the SqlParameter. Incidentally, issue SPR-4814, which I found while seeing if this has already been reported, contains the following comment: bq. CallMetaDataContext reconcileParameters should check that a name exists for parameter to avoid NPE

    Spring JIRA | 1 year ago | Luke Woodward
    org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
  3. 0

    Android: Saving Map State in Google map

    Stack Overflow | 11 months ago | Junie Negentien
    java.lang.RuntimeException: Unable to resume activity {com.ourThesis.junieNegentien2015/com.ourThesis.junieNegentien2015.MainActivity}: java.lang.NullPointerException
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.NullPointerException

      No message provided

      at org.springframework.jdbc.core.metadata.CallMetaDataContext.reconcileParameters()
    2. Spring Framework
      CallMetaDataContext.processParameters
      1. org.springframework.jdbc.core.metadata.CallMetaDataContext.reconcileParameters(CallMetaDataContext.java:319)
      2. org.springframework.jdbc.core.metadata.CallMetaDataContext.processParameters(CallMetaDataContext.java:293)
      2 frames
    3. Spring Batch Infrastructure
      AbstractItemCountingItemStreamItemReader.open
      1. org.springframework.batch.item.database.StoredProcedureItemReader.openCursor(StoredProcedureItemReader.java:168)
      2. org.springframework.batch.item.database.AbstractCursorItemReader.doOpen(AbstractCursorItemReader.java:406)
      3. org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
      3 frames
    4. springitemreadertest
      SpringItemReaderTest.main
      1. springitemreadertest.SpringItemReaderTest.main(SpringItemReaderTest.java:20)
      1 frame