扫描仪NullPointerException

时间:2017-05-15 15:25:30

标签: nullpointerexception java.util.scanner

我是新来的,有个问题。我们目前正在编写一个小型MP3播放器作为大学的实践培训,但目前我不是最好的程序员(几个月前开始)。

我使用了Scanner并遇到了问题:

java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at java.util.Scanner.<init>(Scanner.java:702)
    at PlayList.loadFromM3U(PlayList.java:89)
    at PlayList.<init>(PlayList.java:19)
    at PlayListTest.testSaveAndLoadM3U(PlayListTest.java:202)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class PlayList<T> extends LinkedList<AudioFile> {

    private boolean tp_playMode;
    private int tp_PlayPosition;

    public PlayList() {
        super();
    }

    public PlayList(String pathname){
        super();
        this.loadFromM3U(pathname);
    }

    public int getCurrent() {
        return tp_PlayPosition;
    }

    public void setCurrent(int tp_PlayPosition) {

        this.tp_PlayPosition = tp_PlayPosition;
    }

    public AudioFile getCurrentAudioFile() {
        try {
            return this.get(getCurrent());
        } catch (Exception e) {
            return null;
        }
    }

    public void changeCurrent(){
        int listLength = this.size() - 1;
        if (getCurrent() >= listLength){
            if (tp_playMode){
                Collections.shuffle(this);
            }
            tp_PlayPosition = 0;
        }
        else{
            tp_PlayPosition++;
        }
    }

    public void setRandomOrder(boolean order) {
        if (order){
            Collections.shuffle(this);
        }
        this.tp_playMode = order;
    }

    public void saveAsM3U(String pathname){
        FileWriter writer = null;
        try {
            String newLine = "\n";
            writer = new FileWriter(pathname);
            writer.write("My best Songs TP" + newLine + newLine);
            for (AudioFile file : this) {
                if (file instanceof TaggedFile){
                    writer.write(((TaggedFile)file).getPathname() + newLine);
                    break;
                }
                if (file instanceof WavFile){
                    writer.write(((WavFile)file).getPathname() + newLine);
                    break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void loadFromM3U(String pathname){
        this.clear();
        Scanner scan = null;
        String line;
        try {
            scan = new Scanner(new File(pathname));
            while (scan.hasNextLine()){
                line = scan.nextLine().trim();
                if (!line.isEmpty() && !line.startsWith("#")){
                    try{
                    this.add(AudioFileFactory.getInstance(line));
                } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        } catch (IOException e) {
            throw new RuntimeException();
        } finally {
            try {
                scan.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}

有人能帮助我吗?这会很棒。 :)

0 个答案:

没有答案