0

I know this question is similar to passing a object[] to a params object[] not working however I am having a similar problem in Java.

try {
  Object[] objSingleTableColumns = null;
  DatabaseActions db = new DatabaseActions();
  db.dbConnect(sDatabase);
  for (int i=0 ; i < objTableList.length; i++) {
    objSingleTableColumns = db.dbShowColumns(objTableList[i].toString());
    this.buildSingleModel(objTableList[i].toString(), sDatabase, objSingleTableColumns, false);
  }
  db.dbClose();
} catch (Exception e) {
  System.out.println("Error with Multiple Columns" + e);
}

I have a feeling the issue is being caused by passing a object which is inside an object but I am not sure how to fix this issue as I am still a bit new to Java.. I tried to do Object[] Casting but it did not seem to work.

The error I get is java.lang.NullPointerException

I have returned objSingleTableColumns using Arrays.toString(objSingleTableColumns) and it outputs the column lists as expected without an issue...

To clarify what db.dbShowColumns() does it returns an object of database column names based on the table name provided.


UPDATE: I tried initializing the array as @Mansuro suggested, but this did not work. I did a test run to get the output to maybe resolve this issue.

Would it be possible that my code is creating a multidimensional array because I am passing an Object[] into another Object[]? If that's they case is there a way to merge the objects? Because I have ran this.buildSingleModel on its own and it works perfectly.


This output is without running this.buildSingleModel

objTableList = [glossary, messages, prodfeatures, renters, source, test_table]
objTableList.length = 6
objSingleTableColumns = [gid, gname, gmeaning]
objSingleTableColumns = [mid, msubject, mtype, mread, mcid, mmessage, mtimedate, mproduct, mstar]
objSingleTableColumns = [fid, fpid, ftext, ftype, fsort, fonline]
objSingleTableColumns = [rid, fname, lname, phone, email]
objSingleTableColumns = [sid, sw]
objSingleTableColumns = [tid, tname, tdesc]

The code for the above output is:

   public void  buildMultipleModels(String sDatabase, Object[] objTableList)  {


try {

      Object[] objSingleTableColumns = new Object[100];

      DatabaseActions db = new DatabaseActions();
      db.dbConnect(sDatabase);


       System.out.println("objTableList = " + Arrays.toString(objTableList));
       System.out.println("objTableList.length = " + objTableList.length);

      for (int i=0 ; i < objTableList.length; i++) {
            objSingleTableColumns = db.dbShowColumns(objTableList[i].toString());

            System.out.println("objSingleTableColumns = " + Arrays.deepToString(objSingleTableColumns));

           // this.buildSingleModel(objTableList[i].toString(), sDatabase, objSingleTableColumns, false);
      }
      db.dbClose();


} catch (Exception e) {

   System.out.println("Error with Multiple Columns --> Exception =" + e);

    StringWriter sw = new StringWriter();
    new Throwable("").printStackTrace(new PrintWriter(sw));
    String stackTrace = sw.toString();


   System.out.println("Stack trace = " + stackTrace);


}
}

And this is the output when running this.buildSingleModel

objTableList = [glossary, messages, prodfeatures, renters, source, test_table]
objTableList.length = 6
objSingleTableColumns = [gid, gname, gmeaning]
Error with Multiple Columns --> Exception =java.lang.NullPointerException
Stack trace = java.lang.Throwable: 
        at genModel.buildMultipleModels(genModel.java:170)
        at genModel.doBuildMultipleModels(genModel.java:67)
        at frmMain.btnGenerateMultipleModelsActionPerformed(frmMain.java:530)
        at frmMain.access$600(frmMain.java:44)
        at frmMain$7.actionPerformed(frmMain.java:322)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6267)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6032)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2478)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Community
  • 1
  • 1
jason
  • 1,132
  • 14
  • 32
  • 1
    Why can't you use stacktrace's line number to figure out where the NPE is generated, and test all the suspects? – Dilum Ranatunga Sep 30 '11 at 20:07
  • 2
    on which line exactly does the exception occur? – Bozho Sep 30 '11 at 20:07
  • Where you initialize objTableList? –  Sep 30 '11 at 20:13
  • From the code provided, and the lack of where the actual NPE occurs, my guess would be that objTableList[i] (for at least one value of i) is null, hence causing an NPE. – mth Sep 30 '11 at 20:14

2 Answers2

0

It seems that objSingleTableColumns may be null for one of the calls inside the for loop. Arrays.toString() accepts null so it may work fine. This is just guess as stack trace is not provided.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
0

You have to initialize the array

objSingleTableColumns = new Object[ARRAY_SIZE];
Mansuro
  • 4,558
  • 4
  • 36
  • 76
  • the object seems to be initilized on: objSingleTableColumns = db.dbShowColumns(objTableList[i].toString()); –  Sep 30 '11 at 20:26
  • Im on mobile, so I cant look at the code right now, ... How can I set the array size if the size will very in length – jason Sep 30 '11 at 20:28
  • @jason arrays are not dynamic in java, so you can do it like in [this question](http://stackoverflow.com/questions/1647260/java-dynamic-array-sizes) or use something dynamic like an arraylist or a collection – Mansuro Sep 30 '11 at 20:35
  • @SérgioMichels an array has to be initialized with the new keyword and the size of the array – Mansuro Sep 30 '11 at 20:38
  • 1
    @Mansuro no need this when you have one method that return one array. I just made one simple test here: create one int[] a = null, after I set the value of "a" with the return of one static function and it works... –  Sep 30 '11 at 20:44