首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java调试NullPointerException

Java调试NullPointerException
EN

Stack Overflow用户
提问于 2014-02-11 09:33:39
回答 3查看 579关注 0票数 0

我有这个java代码,并在运行它时捕获了一个nullPointerException。通过使用eclipse进行调试,我发现msiLine参数为null,但无法找到原因。

代码语言:javascript
复制
                String msiAbsolutePath = msiFiles.getAbsolutePath();
                String filePath = msiAbsolutePath.substring(0,msiAbsolutePath.lastIndexOf(File.separator));
                new File(filePath+"/Emboss").mkdir();
                new File(filePath+"/Report").mkdir();
                File files = sort(msiFiles,filePath);

                BufferedReader msiReader = new BufferedReader(new FileReader(files));
                BufferedReader embReader = new BufferedReader(new FileReader(embFiles));
                String[] msiTokens = msiFiles.getName().split("\\.(?=[^\\.]+$)");
                String[] embTokens = embFiles.getName().split("\\.(?=[^\\.]+$)");
                final String msiFileName = msiTokens[0];
                final String embFileName = embTokens[0];
                Date date = new Date();
                DateFormat dateFormatName = new SimpleDateFormat("MMdd");
                PrintWriter msiWrite = new PrintWriter(new BufferedWriter(new FileWriter(filePath+"/Emboss/"+msiFileName+".PRN")));
                PrintWriter embWrite = new PrintWriter(new BufferedWriter(new FileWriter(filePath+"/Emboss/"+embFileName+".PRN")));
                PrintWriter reportWrite = new PrintWriter(new BufferedWriter(new FileWriter(filePath+"/Report/CS"+dateFormatName.format(date)+".RPT")));


                cnt=totalLines=0;
                //String msiLine = msiReader.readLine();
                String msiLine="";
                String embLine = embReader.readLine();
        here>>> for(msiLine = msiReader.readLine(); msiLine.length() >= 60; msiLine = msiReader.readLine())
                {
                    embLine = embReader.readLine();
                    msiWrite.print();
                    :
                    :

                    }
                    statMessage = "Completed " + current + " out of " + lengthOfTask + ".";
                }
                :
                :
            }catch(IllegalStateException | IOException | NullPointerException d){
                d.printStackTrace();
            }
        }
    }

    File sort(File file, String filepath){
        File files = new File(filepath + "\\tempMSIfile.msi");
        try{

            BufferedReader reader = new BufferedReader(new FileReader(file));
            Map<String, String> map=new TreeMap<String, String>();
            String line=reader.readLine();
            for(line = reader.readLine(); line.length() >= 60; line = reader.readLine()){
                map.put(getField(line),line);
            }
            reader.close();
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(files)));
            for(String val : map.values()){
                writer.print("" +val);  
                writer.print(NEWLINE);
            }
            writer.close();

        }catch(IllegalStateException | IOException | NullPointerException d){
            LOG.fine("Error, " + d.getMessage());
        }
        return files;
    }

    String getField(String line) {
        return line.substring(10, 26);//extract value you want to sort on
    }

有人能解释一下为什么msiLine是空的吗?

我认为这涉及到File files = sort(msiFiles,filePath);,但是eclipse报告说files参数确实有正确的文件/东西(?)分配给它。

如果有人想知道,tempMSIfile.msi不是空的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-11 09:36:28

当到达输入结束时,Reader.readLine()返回null。因此,您的条件:

代码语言:javascript
复制
msiLine.length() < 60`

可以触发NullPointerException。将其更改为在测试长度之前添加一个空测试。

编辑:@KubaSpatny指出,将条件更改为:

代码语言:javascript
复制
msiLine != null && msiLine.length() < 60

或者,正如您的评论所建议的那样,将msiLine != null作为条件,并将其作为循环中的第一个指令:

代码语言:javascript
复制
if (msiLine.length() < 60)
    continue; // or break;, depending on what you want to do

您的sort()方法也面临同样的问题。

票数 3
EN

Stack Overflow用户

发布于 2014-02-11 09:39:08

阅读文献资料 for readLine()

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

这意味着:如果msiLine是循环中的null,则文件已被完全读取。在这种情况下,退出循环:

代码语言:javascript
复制
if(msiLine==null){
   break;
}

或者将检查添加到循环头中。

票数 2
EN

Stack Overflow用户

发布于 2014-02-11 09:38:46

java.io.BufferedReader#readLine()的Javadoc声明它返回

代码语言:javascript
复制
 A String containing the contents of the line, not including
 any line-termination characters, or null if the end of the
 stream has been reached

您可能遇到了文件的末尾。当您到达文件末尾时,为msiLine添加一个空检查以检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21697967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档