Results 1 to 7 of 7
  1. #1
    mhcmd is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Question While() stops when encountering null

    I have a form that could upload a file, but sometimes users doesn't need to upload a file.
    Whenever the upload input is empty or null the reading of data stops even though i have a code to filter null item .

    here's the sample code
    Java Code:
    try {
    		List<FileItem> fileItemsList = uploader.parseRequest(request);
    		Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
    		while(fileItemsIterator.hasNext()){
    			FileItem fileItem = fileItemsIterator.next();
    			
    			
    			if(fileItem.isFormField()){
    				String item = fileItem.getString();
    				
    				dataIngr[ctr]= item;
    				out.print(ctr+" "+dataIngr[ctr]);
    
    				
    			}else{
    				
    				if (fileItem.equals(null) || fileItem ==null){
    					dataIngr[ctr]=fileItem.getName();
    					out.print(ctr+" "+dataIngr[ctr]);
    				}else{
    			
    			File file = new File("C:/somepath/files"+File.separator+fileItem.getName());
    			fileItem.write(file);
    			out.println("File "+fileItem.getName()+ " uploaded successfully.");
    			dataIngr[ctr]=fileItem.getName();
    			out.print(ctr+""+dataIngr[ctr]);
    				}
    		}
    			ctr=ctr+1;
    			
    				
    			
    			out.println("<br>");
    			
    		}
    } catch (FileUploadException e) {
    		out.println("Exception in uploading file-1.");
    	} catch (Exception e) {
    		out.println("Exception in uploading file.");
    	}
    sample output (if upload input is not null)
    0 1
    1 name
    2 File name.jpg uploaded succesfully
    3 desc
    4 other

    sample output ( if upload input is null)
    0 1
    1 name
    Exception in uploading file


    Thanks in advance :)
    Last edited by mhcmd; 02-17-2014 at 07:00 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: While() stops when encountering null

    What is the exception? What line throws the exception? What value do the variables on that line have when the exception is thrown?

    Edit: if fileItem is null, this line will always throw an NPE:

    fileItem.equals(null) || fileItem ==null)

    You can't dereference (use a . after) a variable with a null value. Stick to the == for checking for null values.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,766
    Rep Power
    5

    Default Re: While() stops when encountering null

    You need to post the exception output. And make certain the line numbers on the exception match the shown code line numbers.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    mhcmd is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: While() stops when encountering null

    Hi I already add the exception lines :)

    may I ask what is NPE?

    Thanks

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,985
    Rep Power
    9

    Default Re: While() stops when encountering null

    Quote Originally Posted by mhcmd View Post
    Hi I already add the exception lines :)
    You're ignoring the stack trace information, which will tell you exactly which line the error is on. Use e.printStackTrace() until you understand what's going on.

    The problem is you're dereferencing a null variable.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,766
    Rep Power
    5

    Default Re: While() stops when encountering null

    Not sufficient to aid in debugging. Change your code to something like:

    Java Code:
    catch (FileUploadException e) {
            e.printStackTrace();
    } catch (Exception e) {
            e.printStackTrace();
    }
    Regards,
    Jim
    Last edited by jim829; 02-17-2014 at 07:17 PM. Reason: Nuts!!!!!
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    mhcmd is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Re: While() stops when encountering null

    Hi, problem solved :)

    I forgot to put .getName() under my if condition.. it should be (fileItem.getName()==null)

    Thanks :))

Similar Threads

  1. My program stops
    By manou2324 in forum New To Java
    Replies: 4
    Last Post: 05-17-2013, 08:02 PM
  2. Replies: 17
    Last Post: 08-22-2011, 12:22 AM
  3. While loop stops before told?
    By Naxix in forum New To Java
    Replies: 8
    Last Post: 07-08-2011, 07:24 AM
  4. My first Java project, encountering bugs
    By beatzz in forum New To Java
    Replies: 1
    Last Post: 03-17-2009, 08:32 PM
  5. Replies: 6
    Last Post: 01-11-2009, 03:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •