0

我在这个函数中收到了这个错误,不知道如何解决。“潜在的空指针访问:变量流在此位置可能为空”下面是代码:

public void downloadAudioIncrement(String mediaUrl) throws IOException {
        /*URLConnection cn = new URL(mediaUrl).openConnection(Proxy.NO_PROXY);  

        cn.connect();  
        InputStream stream = cn.getInputStream();*/
        URL url = new URL(mediaUrl);

        InputStream stream = url.openStream();
        //Toast.makeText(this.context, "here3", Toast.LENGTH_LONG);

        if (stream == null) {
            Log.e(getClass().getName(), "Não é possível criar InputStream para a url: " + mediaUrl);
        }

        //downloadingMediaFile = new File(context.getCacheDir(),"downloadingMedia_" + (counter++) + ".dat");
        downloadingMediaFile = new File(context.getCacheDir(),"downloadingMedia_.dat");
        FileOutputStream out = new FileOutputStream(downloadingMediaFile);   
        byte buf[] = new byte[16384];

        int totalBytesRead = 0, incrementalBytesRead = 0;
        do {
            int numread = ***stream***.read(buf);   
            if (numread <= 0)
                break;   

            out.write(buf, 0, numread);
            totalBytesRead += numread;
            incrementalBytesRead += numread;
            totalKbRead = totalBytesRead/1000;

            testMediaBuffer();
            fireDataLoadUpdate();
        } while (validateNotInterrupted());   

        if (validateNotInterrupted()) {
            fireDataFullyLoaded();
            //testMediaBuffer();
            //fireDataLoadUpdate();
        }
        ***stream***.close();
        out.close();
    }

如何修复此错误?错误发生在这里:

 numread ***stream***.Read = int (buf);

和这里:

 ***stream***.Close ();
4

2 回答 2

1
    if (stream == null) {
        Log.e(getClass().getName(), "Não é possível criar InputStream para a url: " + mediaUrl);
    }

在这里,您检查 if streamisnull并记录它,但您仍然继续使用该方法,并且您永远不会创建新的stream或任何东西。我的建议:在return;您的块中添加一个:

    if (stream == null) {
        Log.e(getClass().getName(), "Não é possível criar InputStream para a url: " + mediaUrl);
        return;
    }
于 2013-02-25T18:58:04.173 回答
0

将此行更改为阅读

 if (stream == null) {
            Log.e(getClass().getName(), "Não é possível criar InputStream para a url: " +     mediaUrl);
return;
    }

这样,错误的行将不会以空值到达。

于 2013-02-25T18:57:52.780 回答