0

Im working on a school project where I have to implement recursion with arrays and I have done everything but im getting a null error when I am running it. The error points to the Recursion class on Line:

result += packetList[n].idNumber + " " + packetList[n].weight + " " + packetList[n].Destination;

I tried tracing the recursion method to see if it would actually make sense and its looks solid but i'm still getting a null error.

Recursion Class:

import java.io.*;
public class Recursion
{
    public String toString(Packet[] packetList, int n)
    {
       String result = "";
       if (n < 0)
       {
           return result;
       }
       result += packetList[n].idNumber + " " + packetList[n].weight + " " + packetList[n].Destination; // Uncomment if you want the values from last-to-first (last index to 0 index)
       result += toString(packetList, n-1);
       //result += packetList[n].idNumber + " " + packetList[n].weight + " " + packetList[n].Destination; // Uncomment if you want the values from first-to-last (0 index to last index)
       return result;
    }
}

Packet Class

public class Packet
{
    public int idNumber;
    public double weight;
    public String Destination;
    public Packet(int id, double w, String D)
    {
         idNumber = id;
         weight = w;
         Destination = D;
    }
    public boolean isHeavy()
    {
        if (weight > 10)
            return true;
        else
            return false;
    }
    public String toString()
    {
         return idNumber + " " + weight + " " + Destination;
    }
    public double getWeight()
    {
        return weight;
    }
    public String getDestination() 
    {
       return Destination;
    }
}

Test Class

import java.io.*;
import java.util.*;
import java.util.Scanner;
public class TestPackages 
{
    public static void main (String[] args) throws IOException 
    {
       Packet[] packetList = new Packet[100];
       int idNumber;
       double weight;
       String Destination;
       Scanner fileInput;
       fileInput = new Scanner (new File("packetData.txt"));
       int counter = 0;
       while (fileInput.hasNextLine())
       {
          idNumber = fileInput.nextInt();
          weight = fileInput.nextDouble();
          Destination = fileInput.nextLine();
          Packet myPacket = new Packet (idNumber, weight, Destination);
          packetList[counter++] = myPacket;
       }
       Recursion recursion = new Recursion();
       System.out.println(recursion.toString(packetList, packetList.length - 1));
       recursion.displayHeavyPackages(packetList, packetList.length - 1);
       recursion.displayPacketsToDest(packetList, packetList.length - 1, "CT");
       recursion.countPacketsToDest(packetList, packetList.length - 1, "CT");
    } 
}
8
  • Your code is assuming there are 100 lines in the input file. Is there any basis for that? Commented Jul 27, 2017 at 0:26
  • that's what the professor wants on the project notes. Commented Jul 27, 2017 at 0:29
  • What's what he wants? Commented Jul 27, 2017 at 0:32
  • "Your application should also have class TestPackages with only main method in it, in addition to classes Packet and Recursion.In the main method, create an array packetList that can store up to 100 Packets. Next read data for packets from the input file and assign initial part array of Packets. Also maintain counter variable which will be the number of lines in the input file, and also the number of occupied positions in the array packetList. Next invoke each of recursive methods from class Recursion." On the assignment sheet Commented Jul 27, 2017 at 0:33
  • There's your mistake. Your code is ignoring the "up to" detail. Commented Jul 27, 2017 at 0:33

2 Answers 2

1

It looks like you are sending the length of the array to your toString() function, but it might not have the 100 elements initialized, try sending your ´counter´ instead :

System.out.println(recursion.toString(packetList, counter-1))
Sign up to request clarification or add additional context in comments.

1 Comment

Seems you are asking for n<0 srry use counter -1
1
  1. Please verify that packetData.txt has exactly 100 lines otherwise the program will throw a null pointer Exception.

  2. The displayHeavyPackage method should validate if n==0 to avoid Array Index Out Bound exception

displayHeavyPackage

public void displayHeavyPackages(Packet[] packetList, int n) {
    if (packetList[n].isHeavy() == true && n>0) {
        System.out.println(packetList[n]); displayHeavyPackages(packetList, n-1);
    }  else if (packetList[n].isHeavy() == true && n==0){
        System.out.println(packetList[n]);
    }
}
  1. My final suggestion is try to debug your code, it will help a lot to clarify the root cause of the exceptions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.