Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的java空指针异常_Java_Android_Nullpointerexception - Fatal编程技术网

android中的java空指针异常

android中的java空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,在我的主要活动中: 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(f

在我的主要活动中:

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
所有活动都列在清单中。因此,我们非常感谢任何能解决这个问题的帮助

提前感谢。

更换

file.equals("textone");

您正在获取字符串文件的NPE。

替换

file.equals("textone");


您正在获取字符串文件的NPE。

这意味着您正在访问空值

file.equals("textone");
首先,需要使用字符串初始化文件

file = "textone";
file = "texttwo";
equals用于比较,不用于赋值


这里的文件为空

这意味着您正在访问一个空值

file.equals("textone");
首先,需要使用字符串初始化文件

file = "textone";
file = "texttwo";
equals用于比较,不用于赋值

这里的文件为空

代替

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

在使用方法之前,您应该采取一些步骤。看

请参阅方法的文档:

将此字符串与指定的对象进行比较

它不赋值。要指定值,请使用
=
运算符

以及它为什么抛出
NullPointerException

默认情况下,类的实例字段初始化为其默认值。引用类型被指定为默认值
null
。在
file.equals(“textone”)之前执行时,
文件
变量被赋值为
null
,并导致NPE。

代替

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

在使用方法之前,您应该采取一些步骤。看

请参阅方法的文档:

将此字符串与指定的对象进行比较

它不赋值。要指定值,请使用
=
运算符

以及它为什么抛出
NullPointerException


默认情况下,类的实例字段初始化为其默认值。引用类型被指定为默认值
null
。在
file.equals(“textone”)之前执行时,
文件
变量被赋值为
null
并导致NPE。

在getFileName()中,您是否初始化了文件对象?
等于
只需返回
真/假
而不是给其他变量赋值,使用
=/code>而不是
等于
谢谢。问题已解决。在getFileName()中,是否初始化了文件对象?
equals
只返回
true/false
而不是将值赋给其他变量使用
=
而不是
equals
。问题解决了,谢谢。问题解决了。我会在10分钟内接受你的回答(很高兴能帮上忙。)非常感谢您的详细解释。这对理解错误有很大帮助。谢谢你。问题解决了。我会在10分钟内接受你的回答(很高兴能帮上忙。)非常感谢您的详细解释。这对理解错误有很大帮助。谢谢PSR和ling.sThanks PSR和ling.s