Java 为什么这个数组是空的?
为什么我的花瓣是空的? myArray很好,但是idk解释了为什么petArray为空 这是我的.txt文件:Java 为什么这个数组是空的?,java,arrays,Java,Arrays,为什么我的花瓣是空的? myArray很好,但是idk解释了为什么petArray为空 这是我的.txt文件: 112|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|N|N 332|D|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N 237|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N 165|C|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|N|N 1
112|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|N|N
332|D|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N
237|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
165|C|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|N|N
113|C|Fluffy|499/1/2011|Tabby|Gray|M|Y|N|N
333|X|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N
238|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
166|C|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|G|N
114|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|N|N
334|D|12/4/2005|Phydeaux|Collie|White/Tan|M|N|N|N
239|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
167|P|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|E|N
115|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|
335|D|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N
240|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
168|C|Sylvester|1/12/20085|Tuxedo|Black/White|F|N|N|N
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.*;
import java.awt.TextArea;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* Makes a new JFrame thats makes two textareas, fills in the textareas
* from the array and linkedlist.
*/
public class PetGUI extends JFrame{
static int number;
static String[] animalArray;
static TextArea north;
static TextArea south;
public PetGUI(TextFileInput tfi){
int lines = 0;
TextFileInput tfil = new TextFileInput("project4.txt");
String rlines = tfil.readLine();
while(rlines != null){
lines++;
rlines = tfil.readLine();
}
tfil.close();
String [] myArray = new String[100];
number = lines;
animalArray = myArray;
this.setSize(400,400);
this.setLocation(200, 200);
this.setTitle("Adoptable Pets");
createFileMenu();
createEditMenu();
TextArea north = new TextArea(9,20);
TextArea south = new TextArea(9,20);
this.getContentPane().add(north,BorderLayout.NORTH);
this.getContentPane().add(south,BorderLayout.SOUTH);
north.setEditable(false);
south.setEditable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Cat c;
Dogs d;
AdoptablePet [] petArray = new AdoptablePet[lines];
for(int i = 0; i < lines; i++){
myArray[i] = tfi.readLine();;
}
tfi.close();
for(int i = 0; i < lines; i++){
if(myArray[i].charAt(4) == 'C'){
String[] r = myArray[i].split("\\|");
if(r.length != 9){
try{
throw new IllegalPetInput("Not the right length of input");
}
catch(IllegalPetInput a){
}
}
else
try{
c = new Cat(Integer.parseInt(r[0]),r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8],Boolean.parseBoolean(r[9]));
petArray[i] = c;
}
catch(AdoptablePetException a){
System.out.println("Wrong information for the cat.");
}
}
if(myArray[i].charAt(4) == 'D'){
String[] r = myArray[i].split("\\|");
if(r.length != 9){
try{
throw new IllegalPetInput("Not the right length of input");
}
catch(IllegalPetInput a){
}
}
else
try{
d = new Dogs(Integer.parseInt(r[0]),r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8],Boolean.parseBoolean(r[9]));
petArray[i] = d;
}
catch(AdoptablePetException a){
System.out.println("Wrong information for the cat.");
}
}
}
LinkedList l = new LinkedList();
for(int i = 0; i < lines; i++)
System.out.println(myArray[i]);
System.out.println();
for(int i = 0; i < lines; i++)
System.out.println(petArray[i]);
//Appends the north side of the texarea with all of the lines
for(int i = 0; i < l.size(); i++)
north.append((String) l.get(i));
}
PetGUI.java:
112|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|N|N
332|D|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N
237|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
165|C|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|N|N
113|C|Fluffy|499/1/2011|Tabby|Gray|M|Y|N|N
333|X|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N
238|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
166|C|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|G|N
114|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|N|N
334|D|12/4/2005|Phydeaux|Collie|White/Tan|M|N|N|N
239|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
167|P|Sylvester|1/12/2008|Tuxedo|Black/White|F|N|E|N
115|C|Fluffy|4/1/2011|Tabby|Gray|M|Y|
335|D|Phydeaux|6/3/2009|Collie|White/Tan|M|N|N|N
240|C|Snagglepuss|5/13/2010|Siamese|Sable|M|Y|Y|N
168|C|Sylvester|1/12/20085|Tuxedo|Black/White|F|N|N|N
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.*;
import java.awt.TextArea;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* Makes a new JFrame thats makes two textareas, fills in the textareas
* from the array and linkedlist.
*/
public class PetGUI extends JFrame{
static int number;
static String[] animalArray;
static TextArea north;
static TextArea south;
public PetGUI(TextFileInput tfi){
int lines = 0;
TextFileInput tfil = new TextFileInput("project4.txt");
String rlines = tfil.readLine();
while(rlines != null){
lines++;
rlines = tfil.readLine();
}
tfil.close();
String [] myArray = new String[100];
number = lines;
animalArray = myArray;
this.setSize(400,400);
this.setLocation(200, 200);
this.setTitle("Adoptable Pets");
createFileMenu();
createEditMenu();
TextArea north = new TextArea(9,20);
TextArea south = new TextArea(9,20);
this.getContentPane().add(north,BorderLayout.NORTH);
this.getContentPane().add(south,BorderLayout.SOUTH);
north.setEditable(false);
south.setEditable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
Cat c;
Dogs d;
AdoptablePet [] petArray = new AdoptablePet[lines];
for(int i = 0; i < lines; i++){
myArray[i] = tfi.readLine();;
}
tfi.close();
for(int i = 0; i < lines; i++){
if(myArray[i].charAt(4) == 'C'){
String[] r = myArray[i].split("\\|");
if(r.length != 9){
try{
throw new IllegalPetInput("Not the right length of input");
}
catch(IllegalPetInput a){
}
}
else
try{
c = new Cat(Integer.parseInt(r[0]),r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8],Boolean.parseBoolean(r[9]));
petArray[i] = c;
}
catch(AdoptablePetException a){
System.out.println("Wrong information for the cat.");
}
}
if(myArray[i].charAt(4) == 'D'){
String[] r = myArray[i].split("\\|");
if(r.length != 9){
try{
throw new IllegalPetInput("Not the right length of input");
}
catch(IllegalPetInput a){
}
}
else
try{
d = new Dogs(Integer.parseInt(r[0]),r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8],Boolean.parseBoolean(r[9]));
petArray[i] = d;
}
catch(AdoptablePetException a){
System.out.println("Wrong information for the cat.");
}
}
}
LinkedList l = new LinkedList();
for(int i = 0; i < lines; i++)
System.out.println(myArray[i]);
System.out.println();
for(int i = 0; i < lines; i++)
System.out.println(petArray[i]);
//Appends the north side of the texarea with all of the lines
for(int i = 0; i < l.size(); i++)
north.append((String) l.get(i));
}
导入java.awt.BorderLayout;
导入java.awt.Component;
导入java.util.*;
导入java.awt.TextArea;
导入java.util.LinkedList;
导入javax.swing.*;
导入javax.swing.JFrame;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
/*
*创建一个新的JFrame,它创建两个文本区域,填充文本区域
*从数组和linkedlist。
*/
公共类PetGUI扩展了JFrame{
静态整数;
静态字符串[]动画数组;
北区;
南部地区;
公共PetGUI(TextFileInput tfi){
int行=0;
TextFileInput tfil=新的TextFileInput(“project4.txt”);
字符串rlines=tfil.readLine();
while(rlines!=null){
行++;
rlines=tfil.readLine();
}
tfil.close();
字符串[]myArray=新字符串[100];
数量=行数;
animalArray=myArray;
这个。设置大小(400400);
此设置位置(200200);
本文件的标题为“可收养宠物”;
createFileMenu();
createEditMenu();
TextArea北=新TextArea(9,20);
TextArea south=新TextArea(9,20);
this.getContentPane().add(north,BorderLayout.north);
this.getContentPane().add(south,BorderLayout.south);
north.setediate(false);
南部。可编辑(假);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此.setVisible(true);
c类;
狗d;
可收养宠物[]petArray=新可收养宠物[行];
对于(int i=0;i
我在您的数据中计算了10个字段,您的检查显示应该只有9个字段,然后抛出一个异常,该异常被捕获并丢弃
if(r.length != 9)
{
try
{
throw new IllegalPetInput("Not the right length of input");
}
catch(IllegalPetInput a)
{
// you're throwing this away! bad form!
}
}
稍后,您将看到10个字段的数据
c = new Cat(Integer.parseInt(r[0]),r[1],r[2],r[3],
r[4],r[5],r[6],r[7],r[8],
Boolean.parseBoolean(r[9])); // field number 10!
在计算行数后,您还将关闭输入流:
TextFileInput tfil = new TextFileInput("project4.txt");
String rlines = tfil.readLine();
while(rlines != null){
lines++;
rlines = tfil.readLine();
}
tfil.close(); // stream closed! any reads after this should hypothetically have failed!
你永远不会打开它,所以其他任何东西都不会起作用
摆脱数组并使用ArrayList,不要一次读取文件来查看您将提前有多少行。我怀疑问题是,您已经读取文件来计算行数。再次,要将它们存储在数组中,您正在读取它们。但是,当您读取行数时,光标已经在文件末尾。现在,当您这次读取字符串时,
readline()
返回null,这可能是数组为null的原因。因为r.length总是10,而不是9,就像if语句条件一样。你说
if(r.length != 9){
try{
throw new IllegalPetInput("Not the right length of input");
}
catch(IllegalPetInput a){
}
所以,如果语句的计算结果为true,则抛出异常,然后立即捕获(为什么要这样做?)在代码中放入一些调试语句或使用调试器,您可能会看到数组为空的原因。虽然不是问题的原因,但您在尝试中使用空的catch执行throw作为唯一的一行。在这些情况下,您可能只想在if中进行一次抛出,并取消try/catch?您正在捕获并抛出一组ex假设,我打赌每一行都无法解析,因此所有内容都是空的。数组本身不是
null
。如果输入此if
块,则其内容可能是null
,if(r.length!=9){。出于调试目的,在该块中有一些输出。您的数组实际上是null
,还是空的?它在代码中的什么地方是null
(或空的),而它不应该是空的?这仍然不能解释petArray
是null
。也许它只是空的(长度0)?我修复了这些问题,但数组仍然为空。还有其他想法吗?不要使用数组,而是使用ArrayList,并在运行时添加项。这样,您就不必读取文件2x(一次以计算数组的大小)@sans481在读取了有多少行之后关闭了TextFileInput。然后我打开了一个新的TFI,它再次读取了所有行,使数组成为myArray。myArray中包含了所有数据,所以这不是问题。sans481已关闭。您实际上正在关闭流。我不确定为什么在这之后的任何读取行都不会完全抛出异常奥斯。