簡體   English   中英

嘗試存儲字節數組時,為什么會出現java.lang.NullPointerException?

[英]Why do I get a java.lang.NullPointerException when I try to store a Array of Bytes?

嘗試存儲字節數組時,為什么會出現java.lang.NullPointerException? 您能否向我解釋為什么這種方式不起作用? 沒有NullPointerException,我該如何做得更好?

Exception in thread "Thread-3" java.lang.NullPointerException
    at test.Screenblocks_Data.set_Image(Screenblocks_Data.java:13)
    at desktop_share_client.ScreenBlocks.run(ScreenBlocks.java:120)

public class Screenblocks_Data implements java.io.Serializable {
    public int Screenblocks_Counter = 0;
    public int[][] positions = new int[200][2];
    public Jpeg[] sub_images = new Jpeg[200];

    public Screenblocks_Data() {

    }

    public void set_Image(byte[] temp_image) {
        sub_images[Screenblocks_Counter].set_sub_image(temp_image);
    }

    public byte[] get_Image(int position) {
        return sub_images[Screenblocks_Counter].sub_image;
    }
}

public class Jpeg {

    public byte[] sub_image = null;

    public void set_sub_image(byte[] temp_image) {

        sub_image = new byte[temp_image.length];

        sub_image = temp_image;
    }
}

您剛買了一個可以裝200個蘋果的袋子。 但是您沒有裝滿袋子,而是想吃一個蘋果:)。

您從未初始化第零(或第Screenblocks_Counter個元素)元素。 首先,您必須在零位置添加元素,然后訪問它。

 sub_images[Screenblocks_Counter] = new Jpeg();
 sub_images[Screenblocks_Counter].set_sub_image(temp_image);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM