【发布时间】:2014-07-19 20:32:21
【问题描述】:
我问的原因是我得到空指针异常。我有一个名为 SRSI.java 的类,它将解析图像转换为字节数组,我将这个字节数组存储到另一个类 Account 的 setter 方法中。现在我想要在另一个第三类中使用这个 setter 值让我们在 getter 方法的帮助下在动作类中说。但我得到了 NPE。 //下面是SRSI类的代码,我正在处理并存储到db中,同时我正在存储到setter方法中
String fromLocal ="D://1-123456.jpg";
long accountId = account.getId();
File file = new File(fromLocal);
System.out.println("Length--> " + file.length());
InputStream inputStream = null;
byte[] bFile= null;
byte[] imageData = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(file));
ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
bFile = new byte[8192];
int count;
while((count = inputStream.read(bFile))> 0){
baos.write(bFile, 0, count);
}
bFile = baos.toByteArray();
if(bFile.length > 0){
imageData = bFile;
}
baos.flush();
inputStream.close();
} catch (Exception ioe) {
//throw ioe;
}
//storing the above byte array which contains image into account class setter method here account is reference of account class.
account.setImageData(imageData);
//here what account class getter and setter methods looks like
private byte[] imageData;
public void setImageData(byte[] imageData){
if(null!=imageData){
this.imageData = imageData;
}
}
public byte[] getImageData(){
return imageData;
}
Now what i need this same value in getter method in 3rd class i.e.Action.What i am doing is
Action action = new Action();
action.getImageData();
我知道它会创建新对象以及它返回 NPE 的原因。但是我需要将相同的 setter 值放入 Action 类和 Action 类和 SRSI,Account 位于不同的包中。
提前致谢。
【问题讨论】:
标签: java reference nullpointerexception initialization