Java Listener关于处理文件的异常

时间:2017-11-07 00:07:22

标签: java java-ee web

  
    

我的代码有问题,我收到了这些消息:

  
java.lang.NullPointerException
  at java.io.Reader.<init>(Unknown Source)
  at java.io.InputStreamReader.<init>(Unknown Source)
  at org.mcgill.ccs2_505.assignment02.listeners.InventoryListener.contextInitialized(InventoryListener.java:90)
    public void contextInitialized(ServletContextEvent event)  { 

        // Get data from input file and store into the inventory 
        ServletContext context = event.getServletContext();  

        Inventory inventory = new Inventory();  

        String filePath = context.getInitParameter("inventory-file");

        InputStream is=null;  
        BufferedReader reader=null;  
        try{  
            is=context.getResourceAsStream(filePath); 
            reader=new BufferedReader(new InputStreamReader(is));  
            String record;  

            //read every record (one per line)  
            while((record = reader.readLine())!=null){  

                // Put the read line in string


                // Extract product information form each line

            }
            context.setAttribute("inventory", inventory);
            context.log("The inventory content has been loaded.");  
        }

        catch(Exception e){  
            context.log("Exception occured while processing the input file.",e);  
        }

        finally{  
            if(is!=null){  
                try{  
                    is.close();  
                }
                catch(Exception e){}  
            }  
            if(reader!=null){  
                try{  
                    reader.close();  
                }
                catch(Exception e){}  
            }  
        }  
    }
}
  

此代码用于从文件读取数据并将数据加载到java类中   结构体。但是,我在尝试时收到空指针异常   与文件建立连接。

0 个答案:

没有答案