• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with ArrayList / LinkedList .add() methods  RSS feed

 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - I'm trying to write an application that will deconstruct relational tables for analysis. I'm attempting to deconstruct the tables and store them as objects within a List collection, but whenever I try to add objects to the lists I get an exception. The faulty code is listed below:

Where sqlD is an object containing the deconstructed table being inserted (in this particular example) into a LinkedList (storage). The code works fine up until the point where it is inserted into the list, at which point an exception is thrown with the unhelpful "getSqlCacheFromDB failed because: null[Ljava.lang.StackTraceElement;@affc70" message.

Any pointers?
[ January 24, 2008: Message edited by: Jim Yingst ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us the full exception stack trace?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code above must be declared to throw SQLException, at least. Where's the code that catches these? Does it catch other exceptions? Is it responsible for the "getSqlCacheFromDB failed because" message? I suspect if you show us that catch block we can give you tips on how to get a more useful error messages. My guess is you've got something like

Don't use Exception.getMessage() - it loses valuable info (like the name of the exception). And you can't print an array using just a plus sign, or a toString() method. Use e.printStackTrace(), or maybe

Here, e.toString() includes the message (if not null) plus the name of the exception. And Arrays.toString() prints an array separated by commas. There are various other ways to format these. Really, it would be best to use a logging framework like log4j - then there are plenty of methods that will format exceptions nicely in your logs.
[ January 24, 2008: Message edited by: Jim Yingst ]
 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again - thanks for the exception handling tips, the try catch block looks like this (with suggested ammendments):

However - the real issue remains, why can't I get my sqlD objects into a list? The resulting exception message is displayed:

getSqlCacheFromDB failed because: java.lang.NullPointerException:[dbforensicanalysistool.PreProcessor.getSqlCacheFromDB(PreProcessor.java:74), dbforensicanalysistool.Main.preProcess(Main.java:84), dbforensicanalysistool.Main.main(Main.java:56)]

And I know my object is being created because the "System.out.println(sqlD); // testing and debugging" line results in "SYS select LOW_OPTIMAL_SIZE, HIGH_OPTIMAL_SIZE, OPTIMAL_EXECUTIONS, ONEPASS_EXECUTIONS, MULTIPASSES_EXECUTIONS, TOTAL_EXECUTIONS from GV$SQL_WORKAREA_HISTOGRAM where INST_ID = USERENV('Instance') 2008-01-24/13:52:57 0 3657695316 SYS$BACKGROUND null" - so where is the null pointer ocurring?
[ January 24, 2008: Message edited by: Jim Yingst ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure your storage variable (the one in your first post) is not null ?
[ January 24, 2008: Message edited by: Joanne Neal ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should have mentioned earlier - it will help if you Use Code Tags to preserve indentation in your posts. I've added them to your previous posts, around the code you showed. Much nicer, yes?

To diagnose the NullPointerException, you need to look at the line referenced in the stack trace. Which line is PreProcessor.java line 74? If it's the one that says "storage.add(sqlD);" then Joanne is right - the only way that line could throw a NullPointerException is if the variable storage is null.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!