简体   繁体   中英

Java Method returning null

i want to read an input stream and my problem is that this method always returns null although there is no Exception output and the String output (see below) is correctly.. Does anybody have an idea how i can fix that problem?

private ArrayList<String> getPlayer(String name){

    try{
        byte[] b = new byte[1024];
        int numBytes;
        InputStream in = openFileInput(name);
        ArrayList<String> content = new ArrayList<String>();

        while((numBytes = in.read(b)) != 0){
            content.add(new String(b, 0, numBytes, "UTF-8"));
            String string = new String(b, 0, numBytes, "UTF-8");
            System.out.println(string);     //StringOutput 
        }
        return content;


    }catch(Exception e){
        System.out.print(e.getMessage());   //ExceptionOutput 
        return null;
    }
}

The Exception, that is thrown and makes the application stop:

  E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: auldo.trainersoccer, PID: 7733
                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                                                                   at auldo.trainersoccer.ManageSpielerActivity$1.onItemClick(ManageSpielerActivity.java:60)
                                                                   at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                                                                   at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
                                                                   at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
                                                                   at android.widget.AbsListView$3.run(AbsListView.java:3903)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

There is a problem with your while loop condition

while((numBytes = in.read(b)) != 0){

Change this to:

while((numBytes = in.read(b)) > 0){

There is a StringIndexOutOfBoundsException that occurs when you use the != condition because when you reach end of the file, the in.read method returns a -1

The error happens because of an StringIndexOutOfRangeException . You are not checking if the end of the stream has been reached. As the Java doc states the end of the stream is indicated by a return value of -1 . So you should do ((numBytes = in.read(b) != -1) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM