[英]How can I get my program to print? I get a NullPointerException when I try to access .toString()
When I run my program it gives me this error. 当我运行程序时,它给了我这个错误。 The errors are located in the MergeInventories class as well as the CombInv class.
错误位于MergeInventories类以及CombInv类中。 I have labeled them in the classes provided below.
我已经在下面提供的类中对它们进行了标记。
java.lang.NullPointerException
at CombInv.print(CombInv.java:19)
at MergeInventories.main(MergeInventories.java:17)
I am simply trying to print out the data. 我只是想打印出数据。 My program essentially takes two arrays and MergeSorts them.
我的程序本质上采用两个数组,并对它们进行MergeSorts处理。
Here are the multiple classes of my program. 这是我程序的多个类。
import java.io.*;
public class MergeInventories
{
public static void main()
{
header();
try
{
Inventory store1 = new Inventory(new File("F:\\M359 AP Computer Science\\JAVA\\Merge Inventories\\Store1.txt"));
Inventory store2 = new Inventory(new File("F:\\M359 AP Computer Science\\JAVA\\Merge Inventories\\Store2.txt"));
CombInv mergedInventory = mergeInventories(store1,store2);
//store1.print();
//store2.print();
mergedInventory.print(); //********THIS IS LINE 17 OF THE ERROR*************
}
catch(Exception e)
{
e.printStackTrace();
//System.out.print("Error.");
}
}
private static CombInv mergeInventories(Inventory a, Inventory b)
{
CombInv inv = new CombInv();
int i=0,j=0,k=0;
while(i<a.size()&&j<b.size())
{
if(a.getProduct(i).getID() == b.getProduct(j).getID())
{
inv.add(new Product(a.getProduct(i).getID(),a.getProduct(i).getQty()+b.getProduct(j).getQty(),a.getProduct(i).getUP()));
i++;
j++;
}
else if(a.getProduct(i).getID() > b.getProduct(j).getID())
{
inv.add(b.getProduct(j));
j++;
}
else
{
inv.add(a.getProduct(i));
i++;
}
k++;
}
if(j<b.size())
{
for(j=j;j<b.size();j++)
{
inv.add(b.getProduct(j));
}
}
else if(i<a.size())
{
for(i=i;i<a.size();i++)
{
inv.add(a.getProduct(i));
}
}
return inv;
}
private static void header()
{
System.out.println("Varun Pinto \n8th Hour \nM359 AP Comp Sci \n");
}
}
Then we have the Inventory class 然后我们有库存类
import java.util.*;
import java.io.*;
import java.text.*;
public class Inventory
{
protected int increment = -1;
protected int size = 0;
protected Product[] inventory = new Product[11];
protected String header;
protected static String CATEGORIES = "ID\tQTY\tU\\P";
public Inventory()
{
header = "TOTAL INVENTORY";
}
public Inventory(File file)throws FileNotFoundException
{
Scanner fileScan = new Scanner(file);
header = fileScan.nextLine();
String throwaway = fileScan.nextLine();
while(fileScan.hasNextLine())
{
Scanner lineScan = new Scanner(fileScan.nextLine());
Product p = new Product(lineScan.nextInt(), lineScan.nextInt(), lineScan.nextDouble());
increment+=1;
inventory[increment]=p;
}
}
public void add(Product p)
{
increment +=1;
inventory[increment]=p;
size +=1;
}
public Product getProduct(int i)
{
return inventory[i];
}
public int size()
{
return size;
}
public void print()
{
System.out.println(header);
System.out.println(CATEGORIES);
for(Product p: inventory)
{
System.out.println(p.toString());
}
System.out.println();
}
}
Next I have the CombInv class where one of the errors is located. 接下来,我找到了其中一个错误所在的CombInv类。
import java.text.*;
public class CombInv extends Inventory
{
public void print(){
System.out.println(header);
System.out.println(CATEGORIES + "\tVALUE");
double total = 0;
NumberFormat f = NumberFormat.getNumberInstance();
f.setMinimumFractionDigits(2);
f.setMaximumFractionDigits(2);
for(Product p: inventory)
{
System.out.println(p.toString() + "\t" + f.format(p.getQty() * p.getUP()));
//******THERE IS AN ERROR ON THE LINE ABOVE**************************
total += p.getQty() * p.getUP();
}
System.out.println("Total Value of Stock:\t$" + total);
System.out.println();
}
}
Finally we have the Product class which is a basic superclass where I am trying to access .toString() but cannot. 最后,我们有了Product类,它是一个基本的超类,在该类中,我尝试访问.toString()但不能。
import java.text.*;
public class Product
{
private int idNum;
private int quantity;
private double unitPrice;
public Product()
{
idNum = 0;
quantity = 0;
unitPrice = 0;
}
public Product(int id, int q, double price)
{
idNum = id;
quantity = q;
unitPrice = price;
}
public int getID()
{
return idNum;
}
public int getQty()
{
return quantity;
}
public double getUP()
{
return unitPrice;
}
public String toString()
{
NumberFormat f = NumberFormat.getNumberInstance();
f.setMinimumFractionDigits(2);
f.setMaximumFractionDigits(2);
return idNum + "\t" + quantity + "\t" + f.format(unitPrice);
}
}
Please be aware that the use of ArrayLists in the inventory do make this program work, however as this is a project I am NOT ALLOWED TO USE ARRAYLISTS only Arrays. 请注意,在清单中使用ArrayList确实可以使该程序正常工作,但是由于这是一个项目,因此我不允许仅使用ARRAYLISTS数组。 Also I MUST use a MergeSort to sort the data.
另外,我必须使用MergeSort对数据进行排序。
Here are the two text files from which i read the data: Store1.txt 这是我从中读取数据的两个文本文件:Store1.txt
STORE 1 INVENTORY
ID QTY U/P
362 5 12.98
471 2 6.70
792 13 25.00
901 7 2.98
and Store2.txt 和Store2.txt
STORE 2 INVENTORY
ID QTY U/P
163 4 7.20
209 5 11.98
471 5 6.70
608 4 5.90
627 2 9.99
792 1 25.00
812 4 6.00
Here is what I expect to be printed 这是我希望打印的内容
My Name
8th Hour
M359 AP Comp Sci
TOTAL INVENTORY //It only prints out
ID QTY U\P VALUE //these two lines.
163 4 7.20 28.80
209 5 11.98 59.90
362 5 12.98 64.90
471 7 6.70 46.90
608 4 5.90 23.60
627 2 9.99 19.98
792 14 25.00 350.00
812 4 6.00 24.00
901 7 2.98 20.86
Total Value of Stock: $638.94
It took me while to answer this because I essentially put it off for the longest time. 我花了一些时间来回答这个问题,因为我基本上推迟了最长的时间。 The error is a NullPointerException and i fixed it with a simple addition of:
该错误是NullPointerException,我通过简单添加以下内容对其进行了修复:
if(p != null)
before all the instances in which I called the p.toString(). 在我调用p.toString()的所有实例之前。 The answer is so simple I can't believe I lost my mind over it, all I had to do was check that the value wasn't a null and it would continue looping till it found actual values.
答案是如此简单,以至于我不敢相信我对此一无所知,我所要做的就是检查该值是否不是null,并且它将继续循环直到找到实际值。 I knew my Merge Sort worked.
我知道我的合并排序工作了。 In the end it was a really simple fix and didn't require me to change the bulk my code at all.
最后,这是一个非常简单的修复程序,根本不需要我更改大量代码。
Also, I had to increment size again in the while loop of the constructor method of the Inventory class. 另外,我还必须在Inventory类的构造方法的while循环中再次增加大小。 I forgot to do this, but my code worked, despite what people said about rewriting the MergeSorter!
我忘了这样做,但是尽管人们说过要重写MergeSorter,但我的代码仍然有效!
When you merge two sorted lists, perhaps you should consider using the following strategy: 合并两个排序列表时,也许应该考虑使用以下策略:
[Homework .. so no code supplied] [作业..所以没有提供代码]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.