Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Problem extracting data from file

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem extracting data from file

    List<Object> variable = ArrayList<Object>();
    Scanner fileScan;
    File inputFile = null;

    I checked my inputFile to see if it was in the directory. If yes then the code continues below.

    try {

    fileScan = new Scanner(inputFile);

    //scans the datafile and adds the data to the ArrayList
    while (fileScan.hasNext()) {

    String callType = fileScan.next();

    //DEBUG CODE
    System.out.println("Calltype: " + callType);

    if (callType == "D" || callType == "L") {

    // more code

    Here's an example of a line of data I am reading. The times are imported as strings, tested for validity, then I create an object from it in HH:MM:SS. The two time objects are then used to create a total time object.

    D 08:00:00 09:00:00

    The problem is that when I am trying to read the first letter for D or L the whole data file is read in. I want to be able to read and separate between the "D" or "L" and the two times.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem extracting data from file

    Quote Originally Posted by hello_world View Post
    I checked my inputFile to see if it was in the directory. If yes then the code continues below.
    This isn't really necessary as Scanner will throw a FileNotFoundException if the file doesn't exist.
    if (callType == "D" || callType == "L") {
    You rarely want to use == to compare objects. Use the equals method instead.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    okay, I fixed that to look like this "if (callType.equals("D") || callType.equals("L")) {"
    , but now it's creating a new error below and I am still am having the issue with extracting the strings in the line of data one at a time. The change to the code to .equals() created a NullPointerException in my abstract class. in my toString() method below. This method is also an overridden class in my original object class above.

    public abstract class MyAbstract {

    public String toString() {

    String costOutput = "";

    String startTimeOutput = startTime.toString(startTime); //send PreciseTime objects to 'toString()'
    String endTimeOutput = endTime.toString(endTime);

    costOutput += this.computeCost();

    return startTimeOutput + "\t"
    + endTimeOutput + "\t"
    + costOutput;
    } // End toString() method

    public abstract double computeCost();

    } //end MyAbstract class

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem extracting data from file

    Copy and paste the full and exact error message. Also indicate on which line the error occurs.

    Where do you get startTime and endTime variables from?
    Improving the world one idiot at a time!

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem extracting data from file

    Wouldn't it be

    List<Object> variable = new ArrayList<Object>();

    Also, as it appears you've never specified the file name and left it as null, it will indeed throw a FileNotFoundException.
    Last edited by javapenguin; August 17th, 2011 at 06:26 PM.

  6. #6
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    The startTime and endTime are objects created by another class CheckedTime which extends one class, which itself extends to a third class. The third class would be considered the first class (original parent) in line as I progress from class to class creating objects. It's a pretty complex class project as projects go.

    here's the error message, but it refers to probable problems in some of my other classes when some of my objects are created. All of my files compile, but I get this runtime error.
    Exception in thread "main" java.lang.NullPointerException
    at LocalCall.toString(LocalCall.java:58)
    at java.lang.String.valueOf(String.java:2826)
    at java.lang.StringBuilder.append(StringBuilder.java: 115)
    at java.util.AbstractCollection.toString(AbstractColl ection.java:422)
    at java.lang.String.valueOf(String.java:2826)
    at java.lang.StringBuilder.append(StringBuilder.java: 115)
    at MonthlyPhoneBill.main(MonthlyPhoneBill.java:119)

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem extracting data from file

    I'm guessing the problem is either something you did in the toString() of LocalCall that's not being met or you passed it a wrong parameter or something in your main method of MonthlyPhoneBill.

    Please post LocalCall and MonthlyPhoneBill classes.

  8. #8
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    not sure why I had my List<Object> that way. I do have "new" before my ArrayList<OBject>. As for the file name, that's above the code I originally listed in this thread. Here's the code that is above the try-block above.

    do {
    System.out.print("\nEnter file name to read: ");

    try {

    fileName = stdin.nextLine();
    inputFile = new File(fileName);
    }

    catch (Exception ex) {

    System.out.println("File does not exist.");
    }

    } while (!inputFile.exists());

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem extracting data from file

    Hello McFly are you listening?

    Exception in thread "main" java.lang.NullPointerException
    at LocalCall.toString(LocalCall.java:58)

    The error is occuring on line 58 in your LocalCall class. All the code you have posted so far has nothing to do with the problem.
    Improving the world one idiot at a time!

  10. #10
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    I see that Junky, but that's the issue that I can't figure out. I have toString() in my abstract class and my LocalCall class and another similar class LongDistanceCall. I'm having trouble tracking which instance variables are being used and which toString() is being used. I've tried deleting the body of my toString() in my PhoneCall abstract class, but that fails. I think it's in how I'm using and accessing my instance variables to be honest. To get to LocalCall, the final step as the data traverses the classes.

    Initially I create a Time object of which the variables hours and minutes are derived from a Time class. In my second class in the hierarchy PreciseTime extends Time, the instance variables of Precise time access the super(hours, minutes) from Time and add seconds, to instantiate a PreciseTime object. The next level is CheckedTime which also extends PreciseTime. CheckedTime does some error checking and it uses super(hours, minutes, seconds) to instantiate a CheckedTime object using the super instance variables.

  11. #11
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem extracting data from file

    Are you saying that LocalCall class extends the abstract class and does not provide its own toString method, thus it uses the toString method in the abstract class?

    Either way what you need to do is add a bunch of print statements before the exception is thrown to find out exactly which variable is null. Then trace backwards and find out why it is null.
    Improving the world one idiot at a time!

  12. The Following User Says Thank You to Junky For This Useful Post:

    hello_world (August 18th, 2011)

  13. #12
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    I'm still getting nullpointer exceptions in my LocalCall class and don't know why. Does it have something to do with my abstract class or from the objects created by my MonthlyPhoneBill class. I'm stuck and can't find the problem.

  14. #13
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Problem extracting data from file

    Every time you post source code to a java programming forum that has been changed since you posted your Exception stack trace, a Java pony dies. Post the Exception output from the version of the source that you've just posted.

  15. #14
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    Yes sir, LocalCall extends the abstract class PhoneCall, but I have a toString() method in both of them.

  16. #15
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem extracting data from file

    OK and somewhere in the toString of the LocalCall class you are using a variable that is null. Did you do what I suggested and add a bunch of print statements to find out which variable is null?
    Improving the world one idiot at a time!

  17. #16
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    Will try that tomorrow. I have my final to sleep for my final, then will be back to finalize my project for Saturday. I appreciate the help.

  18. #17
    Member
    Join Date
    Jul 2011
    Posts
    31
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem extracting data from file

    I've added print statements throughout and can see what's causing the problem, but can't determine why. The problem is that I get the NullPointerException on line 40 of this code. Here is line 40:

    40 callLength = endTime.toSeconds() - startTime.toSeconds();

    I think the problem is that use of the call to super variables is causing the problem because they may not have access variables which are actually instances of CheckedTime. The variables in the constructor startTime and endTime are objects created by CheckedTime. CheckedTime objects are created with this constructor CheckedTime(int, int, int). I think my NullPointer Exception is trying to access object instances that aren't there. Any ideas how to get access to the checkedTime objects. The objects are actually instantiated in my main.

    ///My Class
    public class LocalCall extends PhoneCall {

    //Class variables
    CheckedTime startTime;
    CheckedTime endTime;
    private double totalCallLength;
    private double totalCost;

    //Constants
    static final double LOCAL_COST = .01;
    static final int HUNDMIN_to_SEC = 6000;

    //LocalCall constructor
    public LocalCall(CheckedTime startTime, CheckedTime endTime) {

    super(startTime, endTime);

    } //end constructor

    public double computeCost() {

    double callCost = 0;
    int callLength = 0;

    callLength = endTime.toSeconds() - startTime.toSeconds();
    totalCallLength += callLength;

    if (totalCallLength > HUNDMIN_to_SEC) {
    callCost = callLength * LOCAL_COST; //Calculate call cost
    }
    else {
    totalCost += callCost;
    }

    return callCost;

    } // End computeCost() method

  19. #18
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem extracting data from file

    public class LocalCall extends PhoneCall {
        CheckedTime startTime;
        CheckedTime endTime;
     
        public LocalCall(CheckedTime startTime, CheckedTime endTime) {
            super(startTime, endTime);
    Your problem is that you have startTime and endTime variables in your LocalCall class as well as in the parent Phonecall class (assumption). In the LocalCall constructor you pass the values for start and end upto the parent class via the call to the super constructor but the variables in the LocalCall class remain uninitialised and therefore are null.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  2. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  3. Data from file into ArrayList
    By humbug in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 4th, 2011, 08:46 AM
  4. Using File Data
    By computercoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2010, 07:51 PM
  5. Extracting the BINDING element from WSDL file
    By Sai in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2010, 02:56 AM