1 Replies - 223 Views - Last Post: 08 October 2012 - 10:29 PM Rate Topic: -----

#1 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Exception in thread "main" java.lang.NullPointerException

Posted 08 October 2012 - 07:27 PM

Error:

What is the name of the data file?
> input.txt
Finished reading the file.

Next command
> show line
Exception in thread "main" java.lang.NullPointerException
at CLLOfRiders.showWaiting(CLLOfRiders.java:35)
at Driver2.commands(Driver2.java:69)
at Driver2.userPrompt(Driver2.java:61)
at Driver2.main(Driver2.java:18)


Driver2.java
import java.util.*;
import java.io.*;

public class Driver2 {
    static String fileName;
    static CLLOfRiders riders = new CLLOfRiders();
    static Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args) {
        askFile();
        userPrompt();
    }
    
    public static void readFile(String fileName) {
        try {
            Scanner file = new Scanner(new File(fileName));
            Rider info;
            while (file.hasNext()) {
                String tempD = file.nextLine();
                String delims = "[ - ]";
                String[] tokens = tempD.split(delims);
                String name = tokens[0];
                char pass = tokens[0].charAt(0);
                info = new Rider(name, pass);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void askFile() {
        Scanner input = new Scanner(System.in);
        boolean check = false;
        while (check == false) {
            try {
                System.out.println("What is the name of the data file?");
                System.out.print("> ");
                fileName = input.nextLine();
                readFile(fileName);
                check = true;
                System.out.println("Finished reading the file.");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void userPrompt() {
        String command;
        do {
            System.out.println("\nNext command");
            System.out.print("> ");
            command = keyboard.nextLine();
            commands(command);
        } while (!command.equals("quit"));
    }
    
    public static void commands(String command) {
        switch (command) {
            case "add rider": addRider();
                break;
            case "show line": System.out.println(riders.showWaiting());
                break;
        }
    }
    
    public static void addRider() {
        System.out.println("Name\n");
        System.out.print("> ");
        String name = keyboard.nextLine();
        System.out.println("SpeedPass\n");
        System.out.print("> ");
        char pass = keyboard.nextLine().charAt(0);
        Rider person = new Rider(name, pass);
        riders.addRider(person);
    }
}


CLLOfRiders
public class CLLOfRiders {

    private Node tail;
    
    public CLLOfRiders () {
        tail = null;
        tail.setNext(tail);
    }
    
    public void addRider(Rider r) {
        Node n = new Node(r);
        if (n.getData().getSpeedPassStatus() == 'Y') {
            n.setNext(tail.getNext());
            tail.setNext(tail);
        } else {
            Node tmp = tail;
            while (tmp.getNext() != tail) {
                tmp = tmp.getNext();
            }
            tmp.setNext(n);
            n.setNext(tail);
        }
    }
    
    public String showWaiting() {
        Node tmp;
        Node end = tail.getNext();
        String result = "";
        for(tmp=end; tmp.getNext()!=end; tmp=tmp.getNext()) {
            result = result + tmp.getData().getName() + "\n";
        }
        return result;
    }
    
    public void processRiders(int queue) {
        for (int n = 0; n < queue; n++) {
            if (tail.getNext() != null || tail.getNext().getNext() != null)
                tail.setNext(tail.getNext().getNext());
        }
    }
}


Node
public class Node {
    private Rider r;
    private Node next;
    public Node (Rider data) {
        r = data;
        next = null;
    }
    public void setNext (Node nd) {
        next = nd;
    }
    public Node getNext () {
        return next;
    }
    public Rider getData() {
        return r;
    }
}


Rider
public class Rider {
    private String name;
    private char speedPassStatus;
    public Rider() {
        name = "Unknown";
        speedPassStatus = 'N';
    }
    public Rider(String name, char speedPassStatus)
    {
        this.name = name;
        this.speedPassStatus = speedPassStatus;
    }
    public String getName() {
        return name;
    }
    public char getSpeedPassStatus() {
        return speedPassStatus;
    }
    public void setSpeedPassStatus(char status) {
        speedPassStatus = status;
    }
}

This post has been edited by lilVaratep: 08 October 2012 - 07:28 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: Exception in thread "main" java.lang.NullPointerException

Posted 08 October 2012 - 10:29 PM

It may happen when tmp=tmp.getNext() returns null, so when tmp.getNext()!=end; gets evaluated tmp is null
Your for loop doesn't really work with your 2x getNext()

for(tmp=end; tmp.getNext()!=end; tmp=tmp.getNext()) {




It doesn't make much sense to me that you loop while tmp.getNext()!=end when your starting condition is tmp = end
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1