-2

Hello I am trying to create a link list using a txt file. I when I try to print the link list I created. I am getting this error

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "PlanetLinkedList.printLL()" because "<local3>" is null
    at test.main(test.java:12)

I do not know why I am getting this error, I double checked my method and I don't see anything wrong with it. Here is the main

import java.io.File;
import java.util.Scanner;

class test{
    public static void main(String[] args){
    // You are not allowed to change the main method.
    
    String inputfilename = "input.txt";    
    String outputfilename= "output.txt";
    PlanetLinkedList solarSystem = constructLLFromFile(inputfilename);
    
    solarSystem.printLL();
    
    System.out.println();
    
  }
  
static PlanetLinkedList constructLLFromFile(String theInputFile){
    System.out.println("Constructing the linked list from "+theInputFile);
    PlanetLinkedList head = null;
    try{
      File file = new File(theInputFile);
      Scanner scan = new Scanner(file);
      int data = 0;
      head = new PlanetLinkedList("", 0, data); 
      int index = 0;
      while(scan.hasNext()){
        data = Integer.parseInt(scan.nextLine());
        String name = scan.nextLine();
        long  diamater = Long.parseLong(scan.nextLine());
        int moons  = Integer.parseInt(scan.nextLine());
        Planet insertee = new Planet(name,diamater,moons);
        head.insert(insertee,index);
        index++;
      }
    }catch(Exception eee){
        
    }

    
    return null;    // cannot change

  }
}

Here is the link list

import java.io.FileWriter;

class PlanetLinkedList{
 private Planet head;
 
 PlanetLinkedList(){    
 }
 
 PlanetLinkedList(Planet initial){
   head=initial;
 }
 
 PlanetLinkedList(String n, long d, int m){
   head = new Planet(n, d, m);
 }
}

Here is the object class

class Planet{
  private String name;
  private long diameter;
  private int moon;
  
  Planet next;
  
  Planet(String n, long d, int m){    
    name = n;
    diameter = d;
    moon = m;
  }
  
  
  String getName(){
    return name;
  }
  
  long getDiameter(){
    return diameter;
  }
  
  int getMoon(){
    return moon;
  }
  
  public String toString(){
    return "Name: "+name+
           "\nDiameter: "+diameter+
           "\nMoon: "+moon;
  }
}

Just note I can't change the object or link list, only the method creating the link list. Here is the txt file

Mercury
4879
0
Venus
12104
0
Earth
12756
1
Mars
6805
2
Saturn
120536
62
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – fantaghirocco Oct 09 '22 at 08:46

1 Answers1

0

constructLLFromFile() is returning null and you are doing solarSystem.printLL(); after returning the value, this is raising the NPE.

public static void main(String[] args){
    // You are not allowed to change the main method.
    
    String inputfilename = "input.txt";    
    String outputfilename= "output.txt";
    PlanetLinkedList solarSystem = new PlanetLinkedList("", 0, data);
    constructLLFromFile(inputfilename, solarSystem);
    
    solarSystem.printLL();
    
    System.out.println();
    
  }
  
static PlanetLinkedList constructLLFromFile(String theInputFile, PlanetLinkedList solarSystem){
    System.out.println("Constructing the linked list from "+theInputFile);
    PlanetLinkedList head = null;
    try{
      File file = new File(theInputFile);
      Scanner scan = new Scanner(file);
      int data = 0;
      int index = 0;
      while(scan.hasNext()){
        data = Integer.parseInt(scan.nextLine());
        String name = scan.nextLine();
        long  diamater = Long.parseLong(scan.nextLine());
        int moons  = Integer.parseInt(scan.nextLine());
        Planet insertee = new Planet(name,diamater,moons);
        solarSystem.insert(insertee,index);
        index++;
      }
    }catch(Exception eee){
        
    }

    
    return null;    // cannot change

  }

By sending the solarSystem variable to the method, you don't need to worry by returning null. The above definition of the method will change the value of the variable and then print it.