我的JApplet中的线程“AWT-EventQueue-1”java.lang.NullPointerException中的异常

时间:2012-05-11 06:06:23

标签: java netbeans applet nullpointerexception japplet

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

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)

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

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

非常感谢任何帮助或建议!

1 个答案:

答案 0 :(得分:2)

有没有听说过简化的for-loop?好吧,有理由把它简化为......

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数组:String [] type = new String [2];。如果你没有初始化它,你没有保留存储类型[0]的空间;