简体   繁体   English

尝试存储字节数组时,为什么会出现java.lang.NullPointerException?

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

Why do I get a java.lang.NullPointerException when I try to store a Array of Bytes? 尝试存储字节数组时,为什么会出现java.lang.NullPointerException? Can you maybe explain to me why it doesn't work this way? 您能否向我解释为什么这种方式不起作用? How can I do it better without 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;
    }
}

You just bought a bag to carry 200 apples. 您刚买了一个可以装200个苹果的袋子。 But without filling the bag, you are trying to eat an apple :). 但是您没有装满袋子,而是想吃一个苹果:)。

You never initialized zeroth (or Screenblocks_Counter th element) element. 您从未初始化第零(或第Screenblocks_Counter个元素)元素。 First you have to add element at zero position and then access it. 首先,您必须在零位置添加元素,然后访问它。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试向数组添加值时出现java.lang.NullPointerException - java.lang.NullPointerException when I try to add value to Array 当我创建一个新类的实例时,为什么会出现java.lang.NullPointerException? - Why do I get a java.lang.NullPointerException when I make a new Instance of a my class? 通过RDP连接或更改分辨率时,为什么会出现java.lang.NullPointerException? - Why do I get java.lang.NullPointerException when I connect via RDP or change the resolution? 当我尝试保留时出现java.lang.NullPointerException - java.lang.NullPointerException when i try to persist 当我尝试在Studio中切换活动时,为什么会收到java.lang.NullPointerException错误? - Why am I getting a java.lang.NullPointerException error when I try to switch activities in studio? 当我尝试在Vaadin View中初始化方法时,得到java.lang.NullPointerException:null - I get java.lang.NullPointerException: null when I try to initialize the method in Vaadin View 为什么在调用JasperFillManager时在java.lang.Class.isAssignableFrom(Native Method)中得到java.lang.NullPointerException? - Why do I get java.lang.NullPointerException at java.lang.Class.isAssignableFrom(Native Method) when calling JasperFillManager? 当我尝试在android中的片段之间获取数据时出现java.lang.NullPointerException - java.lang.NullPointerException when I try to get Data between fragments in android 为什么我要为String数组获取java.lang.NullPointerException? - Why am I getting java.lang.NullPointerException for a String array? 为什么我不断收到Java.Lang.NullPointerException - Why do I keep getting Java.Lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM