File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why is My Program Running in parts? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is My Program Running in parts?" Watch "Why is My Program Running in parts?" New topic
Author

Why is My Program Running in parts?

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Hi People, I have a Program that does the following functions:
1. Reads from a folder and split a text file into many text files:
2. Read from the many text files and validate if they contain an e-mail address. If not they are deleted.
3. The remaining text files(with valid E-mail addresses) are converted to PDF and e-mailed to respective persons.

This is my problem. When I run the program, it does the function number one and stops.
When I run it again, it does Number 2 and stops
I run it the third time and it dos number 3.

So I have to run the Program three times for it to run the complete cycle. No errors or exceptions. What am I going to do???


Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
What am I going to do???

If you want help, I recommend you post some code.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I hope you are not running three different classes at different times ;)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Why are you reading the files twice? Why don’t you read the lines, and if they are valid e-mail addresses, store them in a List and use that List to decide whom to send the .pdfs to?
You can validate e-mail addresses with a simple regex; somebody posted it here once and it occupied a whole page!
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Am trying to post My code but It simply wont come..



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Stanley Mungai wrote:Am trying to post My code but It simply wont come.. . . .
I think you would have to explain it more briefly, because people won’t bother reading 400 lines of code.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

I think if you've got a main method (or any other method) that's over 400 lines long, you've got problems whether it works or not. If you break your program into smaller, more manageble, pieces there's a good chance that you'll realise what the problem is. And if you don't, then it will be easier for others to help you.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

I think you would have to explain it more briefly, because people won’t bother reading 400 lines of code.


This what I was avoiding when I wrote My First Post which Is an Explanation of the Entire code.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanley Mungai wrote:Hi People, I have a Program that does the following functions:
1. Reads from a folder and split a text file into many text files:
2. Read from the many text files and validate if they contain an e-mail address. If not they are deleted.
3. The remaining text files(with valid E-mail addresses) are converted to PDF and e-mailed to respective persons.

This is my problem. When I run the program, it does the function number one and stops.
When I run it again, it does Number 2 and stops
I run it the third time and it dos number 3.


If it's the same program, then it's following the same steps each time. So if you're seeing different behavior on subsequent runs, then the program must be changing something in its environment each time it's run. For example, at startup it might be checking for the existence of the "many text files" and if they're not there, executing a code path that creates them and then exits.

Since you wrote this code yourself, you ought to be aware of what the flow is. If it's not doing what you expected it to do when you wrote the code, then you need to observe the code at various steps along the way to see what's actually happening. This means adding copious print statements or using a debugger. So, for instance, at the point where it's about to start step 2, you would print out the file names that step 2 uses, and you would print out the condition that controls step 2's loop, and you would print something on entry to that loop to know whether you got into it at all and then died, never entered it in the first place.

If your initial printouts don't show you what's happening, then you you need to add more.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

What exactly happens when you run the code the first, second and third time? Where does it stop? Do you get an error message anywhere? Have you tried running your code with a debugger, stepping through it line by line to see exactly what it is doing?

I suspect that the following might be happening (I didn't look at the code in detail):

- The first part of your code picks up files from a certain directory (D:/statements/) and creates files that are written in another directory (D:/DFCU Statements/).
- The second part of your code picks up the files generated by the first part (D:/DFCU Statements/) and does something.
- But what you are doing is listing the files in D:/DFCU Statements/ before (line 80) the first part of your code (lines 100 - 131) has been run. Ofcourse the files that part 2 has to pick up are not there yet when part 1 has not yet been run.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Thank you Jesper de Jong for that insight. I now have the Program Running in one Cycle. But I got a Null Pointer Exception on Line 193. What could be the Issue. I am really not able to see why a null pointer While that Line has got content. Note the Program is not stopping even after the Null Pointer exception it works just fine.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanley Mungai wrote:Thank you Jesper de Jong for that insight. I now have the Program Running in one Cycle. But I got a Null Pointer Exception on Line 193.


If the line numbers in the code you posted match your actual line numbers, then that's this line, right?


Either br is null, or br.readLine() returned null.

Note the Program is not stopping even after the Null Pointer exception it works just fine.


Then you must be doing something like


Catching an exception doesn't fix the problem. It just transfers control to code that you know only gets invoked where there is a problem. That code has to either fix the problem (usually not possible) or else the exception shouldn't have been caught in the first place (or it needs to be wrapped and rethrown).

In general, we should not catch unchecked exceptions--RuntimeException and its descendants (like NullPointerException) and Error and its descendants. RuntimeException indicates a bug in our code. Rather than catching the exception, we should fix the bug. This is one of the reasons why we should catch individual checked exceptions, rather than just catching the overarching Exception.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Thank you Jeff. I handled the Exception.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2327
    
  28

Matthew Brown wrote:I think if you've got a main method (or any other method) that's over 400 lines long, you've got problems whether it works or not. If you break your program into smaller, more manageble, pieces there's a good chance that you'll realise what the problem is. And if you don't, then it will be easier for others to help you.


TO add to this point. Not only do you need to break out the code into module, you need to test each module independently. If you test each part by itself then you can isolate the problem much faster.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanley Mungai wrote:Thank you Jeff. I handled the Exception.


By "handled" I hope you mean that you make sure that NPE doesn't occur in the first place, such as fixing the bug that allowed something to be null when it shouldn't have been, or testing for null if it can legitimately be null and then bailing out if it is. If you're catching NPE, you're (usually) doing it wrong.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Did



And the Exception Disappeared.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Stanley Mungai wrote:Did



And the Exception Disappeared.

Of couse it did. You've just swept your problem under the rug; you haven't done anything to fix it.

Honestly I think it may be time to completely trash this code and start over from scratch with awareness of every line you're writing and why it's there.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

What is the best way then to handle that Kind of an exception?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Stanley Mungai wrote:What is the best way then to handle that Kind of an exception?

The best way to handle it, as Jeff has said, is to stop it from happening. Before you use the thing that must not be null, find out whether it is null or not.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

I can tell you it is returning null for a line that is not null. I already Indicated that the first line of that text file is not null.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanley Mungai wrote:Did



And the Exception Disappeared.


No, it didn't. Your code just stuck its head in the sand and said, "When this exception occurs, pretend it didn't, and just go on as if everything is fine, even though it's not."
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanley Mungai wrote:I can tell you it is returning null for a line that is not null. I already Indicated that the first line of that text file is not null.


If readLine() is returning null, then, as its documentation says (you did read it, right? ), you have reached the end of the input. If you think otherwise, it means you have a bogus assumption. Trust the documentation, the compiler, and the runtime. Don't trust your own assumptions. If you think it should be reading the first line of a file but it's returning null, then there's nothing left to read. Period.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Well, I have thousands of text files am reading Probably there is one that is returning null, But check am removing blank lines from the files on line 120 of the Code pasted above. Unless there is a line that has not been removed which I seriously doubt because I am using that first Line for authentication password and all the files are password encrypted.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
When I cook, I rarely follow a recipe. I just throw things into the pot until the dish seems right. Unless it's something fussy like a cake, I don't even measure quantities. I taste test to see if I need to add more garlic, thyme, red wine, whatever. I get good results because I know from years of experience what works and why. This code looks like it was written the way I cook. This isn't meant as a reproach. I'm just trying to get you to see what an awful predicament you are making for yourself. You've got 500 lines of code that you don't really understand, and there's a problem in the code that you don't really understand, and yes it's possible you could fix the problem you see by sprinkling salt and pepper until it tastes right, but the underlying problem is still there: this code is a huge stew with no recipe.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Thank you Dennis I Love you too.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanley Mungai wrote:Well, I have thousands of text files am reading Probably there is one that is returning null, But check am removing blank lines from the files on line 120 of the Code pasted above. Unless there is a line that has not been removed which I seriously doubt because I am using that first Line for authentication password and all the files are password encrypted.


I have no idea what you're saying here. All I'm saying is that if calling readLine() returns null, then you have, absolutely and without question, reached the end of the file. There are two possible correct courses of action. Which one is actually correct for you here depends on your specific requirements.

1) Make sure that readLine() cannot return null at that point. In other words, your business logic requires that there is a line to be read, and since we're seeing that this is not the case, you have a bug somewhere else, such that the line that is required to be there, isn't. Maybe you didn't close() an OutputStream or Writer in a finally block. Maybe you swallowed another exception (that is, caught it but didn't actually handle it). Maybe something else.

2) Check the results of readLine(), and if it's null, skip that step. This would be the approach if you don't require or can't know ahead of time that there will be something to read there.

Either of the above may be correct because the prevent the NPE from happening in the first place. The wrong approach is to catch the NPE.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is My Program Running in parts?