将对象[]传递给参数对象[]在Java中不起作用

时间:2011-09-30 20:04:45

标签: java object nullpointerexception

我知道这个问题类似于passing a object[] to a params object[] not working但是我在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);
}

我有一种感觉问题是由于传递一个对象在一个对象内部但我不知道如何解决这个问题,因为我对Java仍然有点新...我试着做Object [] Casting但它似乎没有用。

我得到的错误是java.lang.NullPointerException

我已使用Arrays.toString(objSingleTableColumns)返回 objSingleTableColumns ,并按预期输出列列表而不会出现问题......

澄清db.dbShowColumns()的内容,它根据提供的表名返回数据库列名对象。


更新: 我尝试初始化数组,因为@Mansuro建议,但这不起作用。我做了一个测试运行,以获得输出可能解决这个问题。

我的代码是否有可能创建一个多维数组,因为我将一个Object []传递给另一个Object []?如果是这样他们就有办法合并对象?因为我自己运行this.buildSingleModel并且它运行良好。


此输出未运行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]

以上输出的代码是:

   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);


}
}

这是运行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)

2 个答案:

答案 0 :(得分:0)

对于for循环中的一个调用,似乎objSingleTableColumns可能为null。 Arrays.toString()接受null,因此它可以正常工作。这只是猜测,因为没有提供堆栈跟踪。

答案 1 :(得分:0)

你必须初始化数组

objSingleTableColumns = new Object[ARRAY_SIZE];