gpt4 book ai didi

java - 致命异常 : main and Nullpointer

转载 作者:行者123 更新时间:2023-12-02 07:42:18 24 4
gpt4 key购买 nike

我已经遇到这个非常烦人的问题几天了,而且我似乎无法完全解决它。我正在尝试打开 .csv 文件,因此我将其导入到项目的 res/raw/ 文件夹中。然后我尝试通过 getResources() 方法打开并读取它,这正是我得到 NullPointerException 的地方。下面是读取文件并用可用行填充数组的方法。

这是我的 Activity,我从 Words 类创建一个对象 newwords,然后我想调用 PlayWithRawFiles( )Words 中的方法。

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

这是 Word 类

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;
}
}

这是堆栈跟踪

07-10 13:28:58.558: E/AndroidRuntime(647): FATAL EXCEPTION: main

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): at android.view.View$PerformClick.run(View.java:8816)

07-10 13:28:58.558: E/AndroidRuntime(647): at android.os.Handler.handleCallback(Handler.java:587)

07-10 13:28:58.558: E/AndroidRuntime(647): at android.os.Handler.dispatchMessage(Handler.java:92)

07-10 13:28:58.558: E/AndroidRuntime(647): at android.os.Looper.loop(Looper.java:123)

07-10 13:28:58.558: E/AndroidRuntime(647): at 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 对象。

您需要在 onCreate 中的 onClick 之前添加类似的内容

newwords = new Words("");

关于java - 致命异常 : main and Nullpointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11414771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com