1

我在尝试修复这段代码时遇到了一个巨大的问题,我不断收到一个巨大的错误:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at com.allcare.arfile.CloudStoreInfo.getStoreInfoFromProvider(CloudStoreInfo.java:62)
at com.allcare.arfile.ArFileJApplet.listBlobStoreContents(ArFileJApplet.java:959)
at com.allcare.arfile.ArFileJApplet.uploadFile(ArFileJApplet.java:938)
at com.allcare.arfile.ArFileJApplet.sendCloud_buttonActionPerformed(ArFileJApplet.java:807)
at com.allcare.arfile.ArFileJApplet.access$1400(ArFileJApplet.java:26)
at com.allcare.arfile.ArFileJApplet$14.actionPerformed(ArFileJApplet.java:445)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我认为问题出在我的任务上。我知道我得到的列表中至少有 1 个完整的部分变量。我已经尝试打印出 elements 变量的每个元素并且它们都在那里(但我必须在开始分配之前执行此操作,否则它不会到达函数),但它根本不会进入我的 print 语句(这也不是原因,我试过评论它)。我只是找不到究竟是什么空:

public void getStoreInfoFromProvider(BlobStore blobStore)
{
    ListContainerOptions listOptions = new ListContainerOptions();
    String temp = blobStore.list("", listOptions.recursive()).toString(); // returns a string containing all info of the filesystem on the cloud

    // remove the [ and ] characters as well as split whenever encounters [t
    temp = temp.replaceFirst("\\[\\[", "");
    temp = temp.replaceFirst("\\]\\]", "");
    String[] parts = temp.split("\\[t"); 

    for (int x = 0; x < 1; x++)
    {
        String[] elements = parts[x].split(", ");

        System.out.println(elements.length);
        System.out.println(elements[x]);

// this line below is where the code breaks, if i comment it out, then the next line breaks and so on and so forth
        type[x] = elements[0];
        providerId[x] = elements[1];
        name[x] = elements[2];
        location[x] = elements[3];
        scope[x] = elements[4];
        description[x] = elements[5];
        parent[x] = elements[6];
        isoCodes[x] = elements[7];
        metadata[x] = elements[8];
        uri[x] = elements[9];
        userMetadata[x] = elements[10];

        printSingleCloudStoreInfo(x);
    }
}

任何帮助或建议将不胜感激!

4

1 回答 1

2

听说过简化的 for 循环吗?好吧,有理由称它为简化...

public void getStoreInfoFromProvider (BlobStore blobStore)
{
    ListContainerOptions listOptions = new ListContainerOptions ();
    // returns a string containing all info of the filesystem on the cloud
    String temp = blobStore.list ("", listOptions.recursive ()).toString (); 

    temp = temp.replaceFirst("\\[\\[", "");
    temp = temp.replaceFirst("\\]\\]", "");
    String[] parts = temp.split("\\[t"); 

    for (String part: parts) 
    {
        String[] elements = part.split(", ");
        System.out.println(elements.length);
        // ...

我只是在猜测,第 62 行在哪里。您应该用评论标记它或通过引用它以某种方式描述它。

更新:

由于现在标记了第 62 行:

如果 type 设置为 null,则它不会被初始化,而是 null。你必须用你需要的大小来初始化它——例如,因为它是一个字符串数组:String [] type = new String[2];。如果不初始化,就没有预留空间来存储类型[0];

于 2012-05-11T09:07:31.013 回答