[英]FATAL EXCEPTION : main and Nullpointer
我已經有了幾天這個非常討厭的問題了,我似乎無法解決它。 我正在嘗試打開.csv
文件,因此我將其導入到項目的res/raw/
文件夾中。 然后我試圖通過getResources()
方法打開並讀取它,這正是我得到NullPointerException
。 這是讀取文件並使用可用行填充數組的方法。
這是我的Activity
,我從類Words
創建一個Object newwords
,然后我想從類Words
調用PlayWithRawFiles()
方法。
public class Swear_Activity extends Activity implements OnInitListener, OnClickListener {
private Words newwords;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swear);
}
public void onClick(View view){
try {
newwords.PlayWithRawFiles();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("greshka");
e.printStackTrace();
}
}
}
現在這是我得到錯誤的類
public class Words{
public Word[] wordsArray;
private String locale = "de";
public Words(String locale) {
if (locale != null ) {
this.locale = locale;
}
}
Context c;
public void PlayWithRawFiles() throws IOException {
String str="";
StringBuffer buf = new StringBuffer();
int i = 0;
InputStream is = c.getResources().openRawResource(R.raw.est);
BufferedReader reader = null;
try{
if (is != null) reader = new BufferedReader(new InputStreamReader(is));
}
catch(Exception e) {
System.out.println ("ss");
e.printStackTrace();
}
if (is!=null) {
while ((str = reader.readLine()) != null) {
Word wd = new Word(1,"str");
this.wordsArray[i] = wd;
i++;
}
}
is.close();
}
}
這是課堂詞
public class Word {
private int type;
private String data;
public Word(int type, String data){
this.type = type;
this.data=data;
}
public int getType(){
return this.type;
}
public String getData(){
return this.data;
}
}
這是Stack Trace
07-10 13:28:58.558:E / AndroidRuntime(647):致命異常:主要
07-10 13:28:58.558:E / AndroidRuntime(647):java.lang.NullPointerException
07-10 13:28:58.558:E / AndroidRuntime(647):at de.android.swearapp.Swear_Activity.onClick(Swear_Activity.java:32)
07-10 13:28:58.558:E / AndroidRuntime(647):at> android.view.View.performClick(View.java:2408)
07-10 13:28:58.558:E / AndroidRuntime(647):在android.view.View $ PerformClick.run(View.java:8816)
07-10 13:28:58.558:E / AndroidRuntime(647):在android.os.Handler.handleCallback(Handler.java:587)
07-10 13:28:58.558:E / AndroidRuntime(647):在android.os.Handler.dispatchMessage(Handler.java:92)
07-10 13:28:58.558:E / AndroidRuntime(647):在android.os.Looper.loop(Looper.java:123)
07-10 13:28:58.558:E / AndroidRuntime(647):在android.app.ActivityThread.main(ActivityThread.java:4627)
07-10 13:28:58.558:E / AndroidRuntime(647):at java.lang.reflect.Method.invokeNative(Native Method)
07-10 13:28:58.558:E / AndroidRuntime(647):at java.lang.reflect.Method.invoke(Method.java:521)
07-10 13:28:58.558:E / AndroidRuntime(647):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
07-10 13:28:58.558:E / AndroidRuntime(647):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-10 13:28:58.558:E / AndroidRuntime(647):at dalvik.system.NativeStart.main(Native Method)
我做錯了什么? 我真的被這個困住了。 先感謝您!
你從未實例化過newwords對象。
你需要在onClick之前在onCreate中添加這樣的東西
newwords = new Words("");
我認為問題在於你沒有給Context c
(在Words
)一個實際值,所以c.getResources()
會拋出一個NullPointerException。
我在這里看到的兩件事,
newwords
在newwords = new Words("")
; c
為null,未分配上下文。 我建議你像這樣重新編寫類:
public class Words{
public Word[] wordsArray;
private String locale = "de";
private Context c;
public Words(String locale, Context paramContext) {
if (locale != null ) {
this.locale = locale;
}
c = paramContext;
}
// Rest of code ok.
//
// Remove the Context c variable further on as it is declared above!
}
所做的是,我們簡單地將Context
添加為類Words
的構造函數的一部分。 然后實例化的類Words
做到這一點,從您的SwearActivity
類:
public void onClick(View view){
newwords = new Words("", getApplicationContext());
try {
newwords.PlayWithRawFiles();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("greshka");
e.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.