尝试使用数组实现递归时获取错误

时间:2017-07-27 00:17:43

标签: java arrays testing recursion

我在一个学校项目上工作,我必须用数组实现递归,我已经做了所有事情,但是当我运行它时我得到一个null错误。该错误指向Line上的Recursion类:

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

我尝试跟踪递归方法以查看它是否真的有意义并且它看起来很稳固但我仍然得到null error

递归类:

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;
    }
}

数据包类

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;
    }
}

测试类

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");
    } 
}

2 个答案:

答案 0 :(得分:1)

看起来您正在将数组的长度发送到toString()函数,但它可能没有初始化100个元素,请尝试发送“计数器”:

System.out.println(recursion.toString(packetList, counter-1))

答案 1 :(得分:1)

  1. 请验证packetData.txt正好有100行,否则程序会抛出空指针Exception。

  2. displayHeavyPackage方法应该验证是否n == 0以避免数组索引输出异常

  3. 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. 我的最后建议是尝试调试您的代码,这将有助于澄清异常的根本原因。