[英]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.