Java 需要帮助查找此错误的来源吗
这是我关于StackOverflow的第一篇文章。 我目前正在编写一个程序,其中我创建了三个程序;客户、宠物和司机。驱动程序从文本文件(clientdata.txt)读取数据 客户机需要一个Pets数组作为字段,客户机和Pets列表在文本文件中有不同的安排,因此需要将其分开 我遇到问题的代码是当我将数据分成五个客户端的数组和一个Pets数组时。当我尝试时,我得到一个NullPointerException 客户端[count].pet[0]=新的pet(input2[0]、input2[1]、input2[2]、input2[3]、input2[4]);而在第一个for循环中。 NullPointerException来自何处?如何修复它 我需要使用BlueJ Java程序 这是驱动程序java文件,错误发生在上面代码行所在的第一个for循环中Java 需要帮助查找此错误的来源吗,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,这是我关于StackOverflow的第一篇文章。 我目前正在编写一个程序,其中我创建了三个程序;客户、宠物和司机。驱动程序从文本文件(clientdata.txt)读取数据 客户机需要一个Pets数组作为字段,客户机和Pets列表在文本文件中有不同的安排,因此需要将其分开 我遇到问题的代码是当我将数据分成五个客户端的数组和一个Pets数组时。当我尝试时,我得到一个NullPointerException 客户端[count].pet[0]=新的pet(input2[0]、input2[1]、i
import java.util.*;
import java.io.*;
import java.util.Scanner;
/**
* Write a description of class Driver here.
*
* @author
* @version 11-20-15
*/
public class Driver
{
public static void main(String[] args) throws IOException
{
Pet[] pet;
//Client[] client;
Client[] client = new Client[5];
String[] input = new String[16];
String holder;
String line;
String line2;
String line3;
int line4;
int line5;
double line6;
String fileName;
Scanner keyboard;
Scanner dataFile;
//StringTokenizer token;
int size1;
int size2;
int m = 0;
int n = 0;
int count = 0;
keyboard = new Scanner(System.in);
//System.out.println("Enter in the size of the pet array: ");
//size1 = keyboard.nextInt();
// pet = new Pet[size1]; //must be 16 to work with the given clientdata.txt file
// System.out.println("Enter in the size of the client array: ");
//size2 = keyboard.nextInt();
System.out.println("Please enter in the name of the data file. ");
fileName = keyboard.nextLine();
dataFile = new Scanner(new File(fileName));
// BufferedReader reader = new BufferedReader(fileName);
while(dataFile.hasNextLine())
{
input[m] = dataFile.nextLine();
m++;
}
m = 0;
///where to sort data
while(m < 16)
{
String[] data = input[m].split(",");
client[count] = new Client(data[0],data[1], data[2], data[3], data[4], data[5], data[6]);
if(client[n].numberOfPets == 1)
{
m++;
String [] input2 = input[m].split(",");
client[count].pet[0] = new Pet(input2[0], input2[1], input2[2], input2[3], input2[4]);
}
else if(client[n].numberOfPets > 1)
{
for(int p = 0; p < client[n].numberOfPets; p++)
{
m++;
String [] input2 = input[m].split(",");
client[count].pet[0] = new Pet(input2[0], input2[1], input2[2], input2[3], input2[4]);
//pet = new Pet(input2[0], input2[1], input2[2], input2[3], input2[4]);
}
}
}
for(int i = 0; i < client[n].numberOfPets; i++)
{
System.out.println(client[n].toString());
for(int i2 = 0; i2 < client[n].numberOfPets; i2++)
{
System.out.println(client[n].pet[n].toString());
}
}
}
这是宠物节目
public class Pet
{
private String name;
private String animalType;
private double weight;
private String lastRabiesShot;
private String lastVisit;
public Pet()
{
name = new String("unknown");
animalType = new String("unknown");
weight = 0;
lastRabiesShot = new String("unknown");
lastVisit = new String("unknown");
}
public Pet(String inName, String inType, String inWeight, String inRabies, String inVisit)
{
name = new String(inName);
animalType = new String(inType);
weight = Double.parseDouble(inWeight);
lastRabiesShot = new String(inRabies);
lastVisit = new String(inVisit);
}
public String toString()
{
return "\nPet Information: " + name + "," + animalType + "," + weight + "," + lastRabiesShot + "," + lastVisit;
}
}
这是示例文本文件(行间没有空格)
Joe,Johnson,Heyo大道124号,15368,4258.20,2
阿诺德,斗牛犬,58,4-2018-19,6-4-2020
露西,猎犬,14,4-11-2010,5-4-2010
刘易斯,刘易斯,厄普恰克路145号,78452,3,185.51,1
布莱克,猫,12,01-6-09,02-09-2011
Miller,Millerson,136 Gunghans Everywhere Drive,96257,2,0.02,3
少校,实验室,102,07-12-2010,07-11-2013
上校,科利,48,02-15-2018,03-15-2008
船长,实验室,34,02-15-2018,03-15-2008
罗杰斯,史蒂夫,181抄袭奇迹公寓B,47895,1,25.08,1
波比,猫,104,10-10-2009,10-10-2013
船长,酋长,343不值当前巷,28257,4343.00,4
欣欣向荣,鹦鹉,5,北美,3-13-2014
有罪,德国谢泼德,88,11-25-2011,11-25-2015
科尔塔纳,斑猫,15,12-15-2012,05-15-2014
罗兰,科利,8,北美,2-5-2014
如果代码格式看起来有点不稳定,我道歉
客户端[count].pet[0]=新的pet(input2[0]、input2[1]、input2[2]、input2[3]、input2[4]);而在第一个for循环中。NullPointerException来自何处?如何修复它
client
为空,或者client[count]
为空,或者client[count].pet
为空。祝你好运。m=0;
m = 0;
///where to sort data
while(m < 16)
{
String[] data = input[m].split(",");
client[count] = new Client(data[0],data[1], data[2], data[3], data[4], data[5], data[6]);
if(client[n].numberOfPets == 1)
{
m++;// **
look at here it starts to read 1, goes to loop check 15<16? yes it moves on m++ made m 16 and here it is you have 15 rows but it is looking for 16th row.
**
String [] input2 = input[m].split(",");
client[count].pet[0] = new Pet(input2[0], input2[1], input2[2], input2[3], input2[4]);
}
///在何处对数据进行排序
而(m<16)
{
字符串[]数据=输入[m]。拆分(“,”;
客户机[计数]=新客户机(数据[0]、数据[1]、数据[2]、数据[3]、数据[4]、数据[5]、数据[6]);
if(客户端[n].numberOfPets==1)
{
m++;//**
看这里,它开始读1,转到循环检查15您正在跟踪客户端中的pet变量:
class Client {
// ....
public Pet[] pet; // it's null here
public Client() {
// ...
pet = null; // here it stays null
}
public Client(String inLast, String inFirst, String inAddress, String inID, String inVisits, String inBalance,
String inPet) {
// ...
// here you **re-declare** the variable!!!! Don't do this.
Pet[] pet = new Pet[numberOfPets]; // the field remains null!!
}
不要这样做,不要重新声明它,因为这意味着该字段将始终保持为空
改变
// you're setting a local variable here, not the pet field
Pet[] pet = new Pet[numberOfPets];
到
您的代码还存在其他问题,包括过度使用公共字段,允许外部类直接访问它们随后操作的字段。这可能会增加代码复杂性和副作用,这可能是严重错误的来源。@HovercraftFullOfEels Ok…要点:-)无论如何,阅读(我正在删除以前的评论)谢谢!这有助于修复错误。我知道公共字段问题,如果可以的话,我会尝试返回并修复。现在我运行代码时遇到错误java.lang.ArrayIndexOutOfBoundsException:5在client[count]=new client(数据[0],数据[1]等行。我知道这是因为[count]不会重复,因为它始终保持在0。如果我将其放入for循环(仅围绕该行代码),错误将传输到客户端[count]。pet[0]如果我把它放在整个while循环中,它会考虑我试图创建的任何一个PET对象作为一个客户端对象,并且在PET构造函数中有问题。你有什么想法可以解决这个问题吗?@ RevrayyOn:一个新的问题值得一个新的问题。但是在你问它之前,检查抛出错误的行,检查TH。e导致抛出异常的值,搜索与此类似的问题,然后首先尝试自己修复它。
// you're setting a local variable here, not the pet field
Pet[] pet = new Pet[numberOfPets];
// Now you're setting the pet field
pet = new Pet[numberOfPets];