开发者

Passing an object[] to parameter object[] is not working in Java

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.Lightwe开发者_StackOverflowightDispatcher.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)


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.


You have to initialize the array

objSingleTableColumns = new Object[ARRAY_SIZE];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜