Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:32:13+00:00 2026-06-13T17:32:13+00:00

I am attempting to read a text file into a linear linked list of

  • 0

I am attempting to read a text file into a linear linked list of objects. the text file(payfile.txt) contains the following info

DEBBIE     STARR           F 3 W 1000.00
JOAN       JACOBUS         F 9 W  925.00
DAVID      RENN            M 3 H    4.75
ALBERT     CAHANA          M 3 H   18.75
DOUGLAS    SHEER           M 5 W  250.00
SHARI      BUCHMAN         F 9 W  325.00
SARA       JONES           F 1 H    7.50
RICKY      MOFSEN          M 6 H   12.50
JEAN       BRENNAN         F 6 H    5.40
JAMIE      MICHAELS        F 8 W  150.00

and i would like to insert the first names into their own list, last names into a list, and so on. I have to do some add and remove modifications to the LLLs throughout the rest of the problem, i am just unsure this is the correct coding for reading the file into the list.

I am getting a nullpointerexception @ firstname.addFirst(lineScanner.next());

so far i have this :

 public class Payroll
 {
 private LineWriter lw;
 private ObjectList output, input;
 private ObjectList firstname, lastname, gender, tenure, rate, salary;

  public Payroll(LineWriter lw)
  {
      this.lw = lw;
  } 
public void readfile()
   {
       File file = new File("payfile.txt");
       try{
           Scanner scanner = new Scanner(file);
           while(scanner.hasNextLine())
           {
               String line = scanner.nextLine();
               Scanner lineScanner = new Scanner(line);
               lineScanner.useDelimiter(" ");
               while(lineScanner.hasNext())
               {
                   firstname.addFirst(lineScanner.next());
                   lastname.addFirst(lineScanner.next());
                   gender.addFirst(lineScanner.next());
                   tenure.addFirst(lineScanner.next());
                   rate.addFirst(lineScanner.next());
                   salary.addFirst(lineScanner.next());
                }
            }
        }catch(FileNotFoundException e)
        {e.printStackTrace();}
    }



// ObjectList.java
public class ObjectList {
    private ListNode list;

    /**
     * Constructs an empty list
     */
    public ObjectList() {
        list = null;
    }

    /**
     * Returns the first node in the list
     */
    public ListNode getFirstNode() {
        return list;
    }

    /**
     * Returns the first element in the list
     */
    public Object getFirst() {
        if (list == null)
            return null;
        return list.getInfo();
    }

    /**
     * Returns the last element in the list
     */
    public Object getLast() {
        if (list == null)
            return null;
        ListNode p = list;
        while (p.getNext() != null)
            p = p.getNext();
        return p.getInfo();
    }

    /**
     * Adds the given element to the beginning of the list
     * @param o - the element to be inserted at the beginning of the list
     */
    public void addFirst(Object o) {
        ListNode p = new ListNode(o, list);
        list = p;
    }

    /** Appends the given element to the end of the list
     * @param o - the element to be appended to the end of the list
     */
    public void addLast(Object o) {
        ListNode p = new ListNode(o);        
        if (list == null)
            list = p;
        else {
            ListNode q = list;
            while (q.getNext() != null)
                q = q.getNext();
            q.setNext(p);
        }
    }

    /**
     * Removes and returns the first element from the list
     */
    public Object removeFirst() {
        if (list == null) {
            System.out.println("removeFirst Runtime Error: Illegal Operation");
            System.exit(1);
        }
        ListNode p = list;
        list = p.getNext();
        return p.getInfo();
    }

    /**
     * Removes and returns the last element from the list
     */
    public Object removeLast() {
        if (list == null) {
            System.out.println("removeLast Runtime Error: Illegal Operation");
            System.exit(1);
        }
        ListNode p = list;
        ListNode q = null;
        while (p.getNext() != null) {
            q = p;
            p = p.getNext();
        }
        if (q == null)
            list = null;
        else
            q.setNext(null);
        return p.getInfo();
    }

    /**
     * Inserts a node after the node referenced by p
     * @param p - reference to node after which the new node will be added
     * @param o - reference to node that will be inserted into the list
     */
    public void insertAfter(ListNode p, Object o) {
        if (p == null) {
            System.out.println("insertAfter Runtime Error: Illegal Operation");
            System.exit(1);
        }
        ListNode q = new ListNode(o, p.getNext());
        p.setNext(q);
    }

    /**
     * Deletes the node after the node referenced by p
     * @param p - reference to node after which the node will be deleted
     */
     public Object deleteAfter(ListNode p) {
        if (p == null || p.getNext() == null) {
            System.out.println("deleteAfter Runtime Error: Illegal Operation");
            System.exit(1);
        }
        ListNode q = p.getNext();
        p.setNext(q.getNext());
        return q.getInfo();
    }

    /**
     * Inserts a node into its correct location within an ordered list
     * @param o - The element to be inserted into the list
     */
    public void insert(Object o) {
        ListNode p = list;
        ListNode q = null;
        while (p != null && ((Comparable)o).compareTo(p.getInfo()) > 0) {
            q = p;
            p = p.getNext();
        }
        if (q == null)
            addFirst(o);
        else
            insertAfter(q, o);
    }

    /**
     * Removes and returns the first occurrence of the specified 
     * element in the list
     * @param o - The object to be removed from the list
     */
    public Object remove(Object o) {
        ListNode p = list;
        ListNode q = null;
        while (p != null && ((Comparable)o).compareTo(p.getInfo()) != 0) {
            q = p;
            p = p.getNext();
        }
        if (p == null)
            return null;
        else return q == null ? removeFirst() : deleteAfter(q);
    }

    /**
     * Returns true if the list contains the specified element.
     * @param o - The object to search for in the list
     */
    public boolean contains(Object o) {
        ListNode p = list;
        while (p != null && ((Comparable)o).compareTo(p.getInfo()) != 0)
            p = p.getNext();
        return p != null;
    }

    /**
     * Returns true if this list contains no elements
     */
    public boolean isEmpty() {
        return list == null;
    }

    /**
     * Removes all elements from the list
     */
    public void clear() {
        list = null;
    }

    /**
     * Returns the number of elements in the list
     */
    public int size() {
        int count = 0;
        ListNode p = list;
        while (p != null) {
            ++count;
            p = p.getNext();
        }
        return count;
    }
}

public class Driver
{

    public static void main(String args[]) 
    {
        LineWriter lw = new LineWriter("csis.txt");
        Payroll payroll = new Payroll(lw);

        payroll.readfile();
       // payroll.printer(lw);
        lw.close();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:32:14+00:00Added an answer on June 13, 2026 at 5:32 pm

    You need to initialize ObjectList variable before calling methods on it. By default Object is null in java

    firstname = new ObjectList();
    firstname.addFirst(lineScanner.next());
    

    What you also need is deliminator which is one or more white space

    lineScanner.useDelimiter("\\s+");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to insert an image into a rich text file. I have
import string file = open('Text.txt') dataArray = file.read() file.close() dataArray = str(dataArray) letters =
I am currently attempting to read in Hex values from a text file. There
I am attempting to read hex values from a text file. There can be
I am attempting to read a file and set the text of a richTextBox
Well, I am attempting to read a text file that looks like this: FTFFFTTFFTFT
I am attempting to save an array of longs as a text file using
I am attempting to parse a text (CSS) file using fscanf and pull out
I am attempting to read and process the contents of a csv file in
I'm attempting to pass the contents of a file into a Webview. Using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.