1

I have problem with the code, I got these messages:

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){}  
            }  
        }  
    }
}

This code is used to read from file and load data into java class structure. However, I receive the null pointer exception when trying to establish connection with file.

Fadi Mont
  • 11
  • 1
  • inventory-file /inventory.csv – Fadi Mont Nov 07 '17 at 00:10
  • What is the value for String filepath on debug? is it null or does it show /inventory.csv – HARDI Nov 07 '17 at 00:13
  • C:\Users\fady_\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\assignment02\inventory.csv – Fadi Mont Nov 10 '17 at 14:54
  • Are you still facing this issue? – HARDI Nov 10 '17 at 17:18
  • What happens when you try the file path directly instead of from context.getInitParameter("inventory-file"). Note: you may need to have forward slash instead of back slash when trying this. – HARDI Nov 10 '17 at 17:26
  • I still have the same problem, and trying all possible solutions. But still have the same exception. I also print the file path, and the value is the real path not null. I tried to use context.getRealPath, and got the same error. – Fadi Mont Nov 12 '17 at 14:07
  • Description Resource Path Location Type Classpath entry /assignment02/WebContent/inventory.csv will not be exported or published. Runtime ClassNotFoundExceptions may result. assignment02 P/assignment02 Classpath Dependency Validator Message – Fadi Mont Nov 13 '17 at 03:04
  • Description Resource Path Location Type Archive for required library: 'WebContent/inventory.csv' in project 'assignment02' cannot be read or is not a valid ZIP file assignment02 Build path Build Path Problem – Fadi Mont Nov 13 '17 at 03:05
  • Please advise to fix this errors. – Fadi Mont Nov 13 '17 at 03:12
  • Try adding the CSV to the the folder path defined in the error: WebContent/inventory.csv. For some reason the ServletContext is expecting your file to be there. That's my best guess based on the error. – HARDI Nov 13 '17 at 17:16

0 Answers0