问题描述
下面是code,它是获得NullPointerException异常错误:
的InputStream是= getAssets()开(twentyone.txt)。
InputStreamReader的IZ =新InputStreamReader的(是);
的BufferedReader BR =新的BufferedReader(IZ);
可能是什么问题呢?
*编辑:在的printStackTrace
03-19 18:20:18.662:E / AndroidRuntime(929):由:显示java.lang.NullPointerException
编辑2:code,直到异常:
公共类ListViewAa3扩展ListViewA {
公众的String []的过程(字符串CTI)抛出IOException异常{
字符串CTID = CTI;
Log.d(外尝试调用,标签1);
尝试{
Log.d(开始尝试引用,标签2);
尝试{
InputStream的是= getAssets()开(USCOUNTIES.txt)。
InputStreamReader的IZ =新InputStreamReader的(是);
的BufferedReader BR =新的BufferedReader(IZ);}赶上(例外五){e.printStackTrace();}
OK,我知道了。我不得不主要活动的范围内传递给这个类,然后用 context.getAssets.open(twentyone.txt);
要谁的人有同样的问题,这样做:将这个在类活动的onCreate功能: 上下文语境= getApplicationContext();
传递上下文到新类的函数(在我的情况的过程(一个字符串,上下文的背景下))然后输入此过程中的功能:
的InputStream是= context.getAssets()开(twentyone.txt)。
我花了4小时图这样的傻事了。
Here is the code that is getting the NullPointerException error:
InputStream is = getAssets().open("twentyone.txt");
InputStreamReader iz=new InputStreamReader(is);
BufferedReader br = new BufferedReader(iz);
What could be going wrong?
*Edit: the printStackTrace
03-19 18:20:18.662: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
Edit 2: Code till the exception:
public class ListViewAa3 extends ListViewA{
public String[] process(String cti)throws IOException{
String ctid=cti;
Log.d("Outside try invoked","tag1");
try{
Log.d("beginning of try invoked","tag2");
try{
InputStream is = getAssets().open("USCOUNTIES.txt");
InputStreamReader iz=new InputStreamReader(is);
BufferedReader br = new BufferedReader(iz);}catch(Exception e){e.printStackTrace();}
OK, I got it. I had to pass the context of the main activity to this class, then use context.getAssets.open("twentyone.txt");
To anyone who has the same problem, do this:Put this in the onCreate function of the class with activity: Context context=getApplicationContext();
Pass context to the new class's function(in my case "process(String a,Context context)")Then type this in the process function:
InputStream is = context.getAssets().open("twentyone.txt");
Took me 4 hours to figure such a silly thing out.
这篇关于返回的InputStream NullPointerException异常(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!