Java 包含嵌套对象的ObjectInputStream不读取所有内容
在我的应用程序中,我正在编写/读取一个Java 包含嵌套对象的ObjectInputStream不读取所有内容,java,android,Java,Android,在我的应用程序中,我正在编写/读取一个arrayList对象列表,其中也包含对象。(包含BingoCells的BingoPages列表,两者都实现了可序列化)。当在列表中的一个BingoPages对象内打印值时,日志工作正常,但当我调用处理内部BingoCells的方法时,我得到一个空指针异常。非常感谢您提供的任何帮助,我们一直在尝试让此保存/加载工作太久 我保存数据的saveAll()方法包含以下代码: String fileName = "bingoPages"; FileOutputStr
arrayList
对象列表,其中也包含对象。(包含BingoCells
的BingoPages
列表,两者都实现了可序列化)。当在列表中的一个BingoPages
对象内打印值时,日志工作正常,但当我调用处理内部BingoCells
的方法时,我得到一个空指针异常。非常感谢您提供的任何帮助,我们一直在尝试让此保存/加载工作太久
我保存数据的saveAll()
方法包含以下代码:
String fileName = "bingoPages";
FileOutputStream outputStream;
try
{
outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.writeObject(allSheets);
oos.flush();
oos.close();
}
加载数据的myreadAll()
包含以下内容:
try
{
FileInputStream in = openFileInput("bingoPages");
ObjectInputStream ois = new ObjectInputStream(in);
allSheets = (ArrayList<BingoPage>)ois.readObject();
ois.close();
}
catch(Exception e)
{
allSheets = new ArrayList<BingoPage>();
}
以及BingoPage
类:
公共类BingoPage实现了可序列化{
public int-pageID;
公共BingoCell[]]表;
公共整数指数;
公共字符串winnerLocation;
公共BingoPage(内部id)
{
表=新宾戈赛尔[5][5];
pageID=id;
winnerLocation=null;
//自由空间
表[2][2]=新宾戈赛尔(999);
表[2][2]。调用=1;
}
公共void insertNum(int num、int列、int行)
{
表[行][列]=新BingoCell(num);
}
公共字符串stringMe()
{
StringBuilder测试=新建StringBuilder();
对于(int i=0;i<5;i++)
{
对于(int j=0;j<5;j++)
{
test.append(表[i][j].num);
测试。附加(“|”);
}
test.append(“\n\r”);
}
返回测试.toString();
}
public void markNum(int-markNum,int-idx)
{
指数=idx;
对于(int i=0;i<5;i++)
{
对于(int j=0;j<5;j++)
{
if(表[i][j].num==markNum)
{
表[i][j]。调用=1;
checkForBingo();
}
}
}
}
私有void checkForBingo()
{
testRows();
testColumns();
测试对角线();
}
私有void testRows()
{
int hasBingo=1;
对于(int i=0;i<5;i++)
{
hasBingo=1;
对于(int j=0;j<5;j++)
{
if(表[i][j]。调用==0)
{
hasBingo=0;
break;//跳到下一列
}
}
if(hasBingo==1)
{
//如果第pageID页出现第i+1行的宾果,则提醒该宾果
alertWinner(“列”+(i+1));
}
}
}
私有void testColumns()
{
int hasBingo=1;
对于(int i=0;i<5;i++)
{
hasBingo=1;
对于(int j=0;j<5;j++)
{
if(表[j][i]。调用==0)
{
hasBingo=0;
break;//行
}
}
if(hasBingo==1)
{
//如果第pageID页出现第i+1行的宾果,则提醒该宾果
alertWinner(“行”+(i+1));
}
}
}
私有void testDiagonals()
{
if(表[0][0]。调用==1&&table[1][1]。调用==1&&table[2][2]。调用==1
&&表[3][3]。调用==1&&表[4][4]。调用==1)
{
//左上->右下宾果!
alertWinner(“左上->右下对角线”);
}
else if(表[0][4]。调用==1&&表[1][3]。调用==1&&表[2][2]。调用==1
&&表[3][1]。调用==1&&表[4][0]。调用==1)
{
//右上->左下宾果!
alertWinner(“右上->左下对角线”);
}
}
私有void alertWinner(字符串位置)
{
winnerLocation=新字符串(底部“+索引+”处的pageID+”;位置:“+位置);
}
公共空间清除()
{
对于(int i=0;i<5;i++)
{
对于(int j=0;j<5;j++)
{
表[i][j]。调用=0;
}
}
winnerLocation=null;
}
捕获(例外e)
{
allSheets=newarraylist();
}
问题肯定就在这里。通过忽略异常并创建一个虚拟对象来处理异常并不是调试代码的方法,甚至不是一开始就编写代码的方法。此时,您应该打印堆栈跟踪并修复任何问题。请发布
BingoPage
类和类当用户第一次运行应用程序时,不会有保存文件,因此当加载失败时,我会创建一个新的arrayList。这说明了FileNotFoundException。
你真的想在EOFEException,
IOException
,ClassNotFoundException,
InvalidClassException,
,RuntimeException,
?
public class BingoCell implements Serializable{
public int num;
public int called;
public BingoCell(int id)
{
num = id;
called = 0;
}
}
public int pageID;
public BingoCell[][] table;
public int index;
public String winnerLocation;
public BingoPage(int id)
{
table = new BingoCell[5][5];
pageID = id;
winnerLocation = null;
//the free space
table[2][2] = new BingoCell(999);
table[2][2].called = 1;
}
public void insertNum(int num, int column, int row)
{
table[row][column] = new BingoCell(num);
}
public String stringMe()
{
StringBuilder test = new StringBuilder();
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
test.append(table[i][j].num);
test.append(" | ");
}
test.append("\n\r");
}
return test.toString();
}
public void markNum(int markNum, int idx)
{
index = idx;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
if(table[i][j].num == markNum)
{
table[i][j].called = 1;
checkForBingo();
}
}
}
}
private void checkForBingo()
{
testRows();
testColumns();
testDiagonals();
}
private void testRows()
{
int hasBingo = 1;
for(int i = 0; i < 5; i++)
{
hasBingo = 1;
for(int j = 0; j < 5; j++)
{
if(table[i][j].called == 0)
{
hasBingo = 0;
break; //Skip to the next column
}
}
if(hasBingo == 1)
{
//Alert that bingo on row i+1 if page pageID
alertWinner("Column " + (i+1));
}
}
}
private void testColumns()
{
int hasBingo = 1;
for(int i = 0; i < 5; i++)
{
hasBingo = 1;
for(int j = 0; j < 5; j++)
{
if(table[j][i].called == 0)
{
hasBingo = 0;
break; //row
}
}
if(hasBingo == 1)
{
//Alert that bingo on row i+1 if page pageID
alertWinner("Row " + (i+1));
}
}
}
private void testDiagonals()
{
if(table[0][0].called == 1 && table[1][1].called == 1 && table[2][2].called == 1
&& table[3][3].called == 1 && table[4][4].called == 1)
{
//Top left->bottom right bingo!
alertWinner("Top left->bottom right diagonal");
}
else if(table[0][4].called == 1 && table[1][3].called == 1 && table[2][2].called == 1
&& table[3][1].called == 1 && table[4][0].called == 1)
{
//Top right->bottom left bingo!
alertWinner("Top right->bottom left diagonal");
}
}
private void alertWinner(String location)
{
winnerLocation = new String(pageID + " at " + index + " sheets from bottom; location: " + location);
}
public void clear()
{
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
table[i][j].called = 0;
}
}
winnerLocation = null;
}
catch(Exception e)
{
allSheets = new ArrayList<BingoPage>();
}