gpt4 book ai didi

android中java空指针异常

转载 作者:行者123 更新时间:2023-12-01 17:25:12 27 4
gpt4 key购买 nike

在我的主要 Activity 中:

count=count+1;
//the problem seems to be in this line of code below
String fileName=f.getFileName(count).toString();
// the method below invokes a method from another class. it works fine so please ignore
helloTxt.setText(readText.readTxt(fileName));

f 是 FileName 类的一个对象,我从中获取文件名:

public class FileName {

String file;
public String getFileName(int count) {
// TODO Auto-generated method stub

if (count==1)
{
file.equals("textone");
}

if(count==2)
{
file.equals("texttwo");
}
return file;

}



}

日志猫:

03-19 12:02:00.554: D/AndroidRuntime(5550): Shutting down VM
03-19 12:02:00.554: W/dalvikvm(5550): threadid=1: thread exiting with uncaught exception (group=0x418342a0)
03-19 12:02:00.562: E/AndroidRuntime(5550): FATAL EXCEPTION: main
03-19 12:02:00.562: E/AndroidRuntime(5550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.MainActivity}: java.lang.NullPointerException
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.os.Looper.loop(Looper.java:137)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.ActivityThread.main(ActivityThread.java:4895)
03-19 12:02:00.562: E/AndroidRuntime(5550): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 12:02:00.562: E/AndroidRuntime(5550): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 12:02:00.562: E/AndroidRuntime(5550): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-19 12:02:00.562: E/AndroidRuntime(5550): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-19 12:02:00.562: E/AndroidRuntime(5550): at dalvik.system.NativeStart.main(Native Method)
03-19 12:02:00.562: E/AndroidRuntime(5550): Caused by: java.lang.NullPointerException
03-19 12:02:00.562: E/AndroidRuntime(5550): at com.example.testflashfile.FileName.getFileName(FileName.java:11)
03-19 12:02:00.562: E/AndroidRuntime(5550): at com.example.testflashfile.MainActivity.onCreate(MainActivity.java:48)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.Activity.performCreate(Activity.java:5163)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-19 12:02:00.562: E/AndroidRuntime(5550): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-19 12:02:00.562: E/AndroidRuntime(5550): ... 11 more
03-19 12:02:11.164: I/Process(5550): Sending signal. PID: 5550 SIG: 9

所有 Activity 都列在 list 中。因此,我们非常感谢任何能够解决此问题的帮助。

提前致谢。

最佳答案

代替

file.equals("textone");
file.equals("texttwo");

使用

file = "textone";
file = "texttwo";

在使用方法之前,您应该执行一些步骤。请参阅API .

请参阅 equals 的文档方法:

Compares this string to the specified object.

它不分配值。要分配值,请使用 = 运算符。

And why it is throwing NullPointerException:

默认情况下,类的实例字段被初始化为其默认值。引用类型被分配默认的null。当 file.equals("textone"); 执行之前,file 变量被赋值为 null 并导致 NPE。

关于android中java空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493055/

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