在内部存储NullPointerException异常的Android节省数据 [英] Android saving data in Internal Storage NullPointerException
问题描述
我有从我application.I'm刚开一个空指针异常写入内部存储的文件有点小问题,却找不到解决it..and却听不懂的哪一种方式元素为null。
下面是code,我使用:
散= jsonObj.getString(哈希);
Log.w(客户端验证HASH,SHOW客户端验证HASH:+散);
FileOutputStream中出= context.openFileOutput(autohash,context.MODE_PRIVATE);
out.write(hash.getBytes());
out.close();
在这里我想创建并写入该文件中的类不是一个活动
,它只是一些辅助类的,这就是为什么它给我的错误,当我尝试设置 =背景下这一点;
。
而实际上它在这行抛出的NullPointerException异常:的FileOutputStream OUT = context.openFileOutput(autohash,context.MODE_PRIVATE);
,我不能得到它,这导致此异常:
- 上下文
块引用>或
- autohash - 文件(存在或不)
。
块引用>第二场景:
我节省了内部存储的文件的相同的功能,但我打电话从其他activity.Here该方法的情况是:
我收到在互联网上不同的数据包,我在做这样的事情:
BasePacket包; //所有其它的包扩展此类。
字节[]缓冲区的字节= [1024];
//包计算
开关(packetType){
案件的StartPacket:
包=新的StartPacket(/ * * PARAMS /);
packet.startExecutePacket();
案例endPacket:
//相同的步骤
}和
startExecutePacket()
我想保存文件。因此,任何形式的帮助,欢迎!在此先感谢!
解决方案CallingActivity.java
的onCreate()助手类mHelper =新助手类(CallingActivity.this);
helperClass.java
//声明上下文
背景refContext;//构造
公共助手类(上下文mContext)
{
refContext = mContext;
}//你code 哈希= jsonObj.getString(哈希);
Log.w(客户端验证HASH,SHOW客户端验证HASH:+散);
FileOutputStream中出= refContext.openFileOutput(autohash,Context.MODE_PRIVATE);
out.write(hash.getBytes());
out.close();尝试用这个
I have a little problem with writing a file in internal storage from my application.I'm gettin a null pointer exception,but can't find the way to fix it..and actually can't understand which one of the elements is null.
Here is the code that I'm using :
hash= jsonObj.getString("hash"); Log.w("CLIENT AUTH HASH","SHOW CLIENT AUTH HASH : "+hash); FileOutputStream out = context.openFileOutput("autohash",context.MODE_PRIVATE); out.write(hash.getBytes()); out.close();
The class where I'm trying to create and write this file is not an
Activity
, it's just some kind of helper class and that's why it's giving me error when I try to setcontext=this;
. And actually the NullPointerException it thrown at this line :FileOutputStream out = context.openFileOutput("autohash",context.MODE_PRIVATE);
, and I can't get it, which cause this exception :
- The Context
or
- autohash - file (exist or don't).
Second scenario :
I have the same function of saving the file in internal storage,but I'm calling that method from another activity.Here is the situation:
I have different packets received over the internet and I'm doing something like this :
BasePacket packet; //all other packets extends this class. byte[] buffer=byte[1024]; //packet calculations switch(packetType){ case startPacket: packet = new startPacket(/*params*/); packet.startExecutePacket(); case endPacket: //same procedure }
and in
startExecutePacket()
I'm trying to save the file.So any kind of help are welcomed!Thanks in advance!
解决方案CallingActivity.java
onCreate() helperClass mHelper= new helperClass(CallingActivity.this);
helperClass.java
//declare a context context refContext; //constructor public helperClass(context mContext) { refContext=mContext; } //and you code hash= jsonObj.getString("hash"); Log.w("CLIENT AUTH HASH","SHOW CLIENT AUTH HASH : "+hash); FileOutputStream out = refContext.openFileOutput("autohash",Context.MODE_PRIVATE); out.write(hash.getBytes()); out.close();
try with this one
这篇关于在内部存储NullPointerException异常的Android节省数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!