Android NullPointerException lock == null

时间:2014-08-31 21:48:18

标签: java android csv nullpointerexception

我尝试使用AsyncTask解析服务器上的csv文件。这是我的代码:

        @Override
        protected String doInBackground(String... params) {

            InputStream inStream = null;


            URL stockURL = null;
            try {
                stockURL = new URL("http://antoniofalcone.it/fantavoti/gazz.csv");
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            BufferedReader input = null;
            try {
                input = new BufferedReader(new InputStreamReader(stockURL.openStream()));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }           
            String line = "";

                    try {
                        while ((line = input.readLine()) != null) {
                        String[] colums = line.split(",");
                            if (colums.length > 13) {
                                Log.d("CSVParser", "Skipping Bad CSV Row");
                                continue;
                            }

                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }



   return "All Done!";
}

我得到一个NullPointerException:lock == null。我该如何解决这个问题?这是完整的logcat:

08-31 21:42:28.918: E/OpenGLRenderer(2774): Getting MAX_TEXTURE_SIZE from GradienCache
08-31 21:42:28.918: E/AndroidRuntime(2774): FATAL EXCEPTION: AsyncTask #1
08-31 21:42:28.918: E/AndroidRuntime(2774): Process: com.example.task, PID: 2774
08-31 21:42:28.918: E/AndroidRuntime(2774): java.lang.RuntimeException: An error occured while executing doInBackground()
08-31 21:42:28.918: E/AndroidRuntime(2774):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.lang.Thread.run(Thread.java:841)
08-31 21:42:28.918: E/AndroidRuntime(2774): Caused by: java.lang.NullPointerException: lock == null
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.io.Reader.<init>(Reader.java:64)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at com.example.task.MainActivity$PostTask.doInBackground(MainActivity.java:85)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at com.example.task.MainActivity$PostTask.doInBackground(MainActivity.java:1)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-31 21:42:28.918: E/AndroidRuntime(2774):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-31 21:42:28.918: E/AndroidRuntime(2774):     ... 4 more
08-31 21:42:28.922: E/OpenGLRenderer(2774): MAX_TEXTURE_SIZE: 16384
08-31 21:42:28.926: E/OpenGLRenderer(2774): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
08-31 21:42:28.934: E/OpenGLRenderer(2774): MAX_TEXTURE_SIZE: 16384

1 个答案:

答案 0 :(得分:0)

正如我在其他答案和评论中向您指出的那样 - 捕捉异常然后继续是完全错误的,并且最有可能是您崩溃的原因。

话虽如此,这是打开URL流并解析内容的更好方法。

在我们处理它的同时,让我们将内容的下载与内容的解析分开。这使得调试更容易,更容易测试。为此,我们定义一个小帮助函数one I've used in my own code,将文件下载到一个字符串中,通过http。

public class ContentDownloader
{
    public static String downloadString(String urlstring) throws IOException
    {
        URL url = new URL(urlstring);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();

        connection.setReadTimeout(30000);
        connection.setConnectTimeout(30000);

        connection.connect();

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        StringBuilder sb = new StringBuilder();
        char [] buffer = new char[5000];

        while (true)
        {
            int result = reader.read(buffer);
            if (result < 0)
            {
                break;
            }

            sb.append(buffer, 0, result);
        }

        String response = sb.toString();
        return response;
    }
}

现在让我们定义一个小解析助手,它可以将下载的字符串转换为多行,并将每个CSV行拆分成列。您可以根据需要进行修改

public class CSVParser
{
    public static ArrayList<String[]> parse(String str)
    {
        ArrayList<String[]> parsed_lines = new ArrayList<String[]>();

        String lines [] = str.split("\\r?\\n");
        for (String row : lines)
        {
            row = row.trim();
            if (row.length == 0)
            {
                continue;
            }
            String [] columns = row.split(",");
            for (int x = 0; x < columns.length; x++)
            {
                columns[x] = columns[x].trim();
            }
            parsed_lines.add(columns);
        }
        return parsed_lines;
    }
}

现在,你的asynctask只是这个:

protected String doInBackground(String... params) {

    String content = null;
    ArrayList<String[]> parsed_lines = null;

    try {
        content = ContentDownloader.downloadString();
        parsed_lines = CSVParser.parse(content);
    }
    catch(IOException ioex) {
        Log.d(TAG, "IOException", ioex);
        return;  // DO NOT CONTINUE IF WE FAILED TO DOWNLOAD
    }

    // if we get to this point here, parsed_lines
    // will be a parsed CSV file
}

欢迎你。