Android 在内部存储中保存数据 NullPointerException
我在从应用程序的内部存储中写入文件时遇到了一些问题。我遇到了空指针异常,但找不到修复它的方法。实际上无法理解哪个元素为空。
这是我正在使用的代码:
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();
我尝试创建和写入此文件的类不是 Activity
,它只是某种帮助程序类,这就是为什么它在时给我错误我尝试设置context=this;
。 实际上它在这一行抛出了 NullPointerException : FileOutputStream out = context.openFileOutput("autohash",context.MODE_PRIVATE); ,我无法得到它,这导致了这个异常:
- 背景
或
- autohash - 文件(存在或不存在)。
第二种情况:
我具有将文件保存在内部存储中的相同功能,但我从另一个活动调用该方法。情况如下:
我通过互联网收到了不同的数据包,并且我'我正在做这样的事情:
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
}
在 startExecutePacket()
中我试图保存文件。
欢迎任何形式的帮助!提前致谢!
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 set context=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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CallingActivity.java
helperClass.java
尝试使用这个
CallingActivity.java
helperClass.java
try with this one