我收到以下错误。 ReadFile.main中的ReadFile.createDogs(ReadFile.java:41)中的线程“main”java.lang.NullPointerException中的异常(ReadFile.java:55) 该文件正在打开桌面。我觉得我很遗憾 简单。我也包括了类文件。该错误是特定的 dogOutput.println(dogInfo);
谢谢。
import java.io.*;
import java.io.PrintWriter;
public class ReadFile
{
private static Canine[] getCanine()
{
Canine[] canines= new Canine[5];
canines[0]=new Canine("Thoedore", "Male", "Medium", 7, 50);
canines[1]=new Canine("Magellan", "Male", "Medium", 5, 50);
canines[2]=new Canine("Duffy", "Male", "Small", 10, 10);
canines[3]=new Canine("Sammie", "Female", "Medium", 7, 65);
canines[4]=new Canine("Snowball", "Male", "Small", 13, 17);
return canines;
}
private static PrintWriter createFile(String fileName)
{
try{
File listOfDogs = new File(fileName);
PrintWriter infoToWrite= new PrintWriter
( new BufferedWriter
(new FileWriter(listOfDogs)));
}
catch (IOException e)
{
System.out.println(" An I/O Error Occurred.");
System.exit(0);
}
return null;
}
private static void createDogs(Canine dog, PrintWriter dogOutput)
{
String dogInfo= dog.name + " " + dog.gender+ " " + dog.size +" ";
dogInfo += Integer.toString(dog.age)+ " "+
Double.toString(dog.weight);
dogOutput.println(dogInfo);
}
public static void main (String[] args)
{
Canine[] canines= getCanine();
System.out.println("Here1");
PrintWriter dogOutput =
createFile("/Users/Teacher/Desktop/Dogs.text");
for(Canine dogs: canines)
{
createDogs(dogs, dogOutput);
System.out.println("Here5");
}
dogOutput.close();
}
private static class Canine extends Pet
{
public static final String type="Dog";
public Canine()
{
super.type=this.type;
}
public Canine(String name, String gender, String size, int age, double
weight)
{
super.type=this.type;
super.name=name;
super.gender=gender;
super.size=size;
super.age=age;
super.weight=weight;
}
public static void setType(String word)
{
}
}
private static class Pet
{
public static String type;
public static String color;
public static String gender;
public static String temperment;
public static String food;
public static int age;
public static double weight;
public static String name;
public static String size;
public Pet()
{
}
public Pet(String type,String color,String gender,String temperment,
String food,int age,double weight,String name,String size)
{
type=type;
color=color;
gender=gender;
temperment=temperment;
food=food;
age=age;
weight=weight;
name=name;
size=size;
}
public Pet(String name, String gender, String size, int age, double
weight)
{
name=name;
gender=gender;
size=size;
age=age;
weight=weight;
}
public static String getType()
{
return type;
}
public static String getName()
{
return name;
}
public static String getSize()
{
return size;
}
public static String getColor()
{
return color;
}
public static String getTemperment()
{
return temperment;
}
public static String getFood()
{
return food;
}
public static String getGender()
{
return gender;
}
public static int getAge()
{
return age;
}
public static void setType(String word)
{
type=word;
}
public static void getName(String word)
{
name=word;
}
public static void setSize(String word)
{
size=word;
}
public static void setColor(String word)
{
color=word;
}
public static void getTemperment(String word)
{
temperment=word;
}
public static void setFood(String word)
{
food=word;
}
public static void setGender(String word)
{
gender=word;
}
public static void setAge(int ageSet)
{
age=ageSet;
}
public static void setWeight(double weightSet)
{
weight=weightSet;
}
public static void setName(String NameSet)
{
name=NameSet;
}
public String toString()
{
return ("Type "+type+"\n" +" Name "+name+"\n"+" Age "+age+"\n"+
"Size "+ size+"\n"+" Gender "+gender+"\n"+" Weight "+weight);
}
}
}
答案 0 :(得分:0)
简单的答案:在createFile()中,您始终返回null
,而不是刚刚创建为PrintWriter
的{{1}}实例。
完整答案:您需要加强对OOP的了解。例如,不必要地使用infoToWrite
成员可能会产生不良影响。
答案 1 :(得分:0)
尝试重命名dogOutput变量。在java中,您不能在具有相同名称的静态方法之间传递变量。