Android 在内部存储中保存数据 NullPointerException

发布于 2024-12-05 18:46:15 字数 1211 浏览 2 评论 0原文

我在从应用程序的内部存储中写入文件时遇到了一些问题。我遇到了空指针异常,但找不到修复它的方法。实际上无法理解哪个元素为空。

这是我正在使用的代码:

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); ,我无法得到它,这导致了这个异常:

  1. 背景

  1. 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 :

  1. The Context

or

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-12-12 18:46:15

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();

尝试使用这个

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文