R
Rich Wahl
So I threw out my Idea about using JDBC, and went to Serialization of
a Class and using just a File/Object reader/writer.
I have this snippet of code running when I click a View button, I want
the code to read in the Objects from a File and then output them into
a TextField on the Frame. The Code is Syntaxically correct, But when
it gets to the
"outputField.append(tempCritter.toString());" line.
It throws a null pointer exception. But The tempCritter cant be null
if it gets to that line.
Am I understanding Serializable wrong? Thanks in advance for any
advice.
Here is my Code for the ViewButton. (The Index prints are just to
'debug' to make sure they are being called and to find out where the
problem really is)
----------------------------
if (event.getActionCommand() == "View") {
try {
ObjectInputStream
instream = new ObjectInputStream( new
FileInputStream("CritterDB.rdb"));
int index = 0;
System.out.println(index);
CritterClass tempCritter = new CritterClass();
while((tempCritter = (CritterClass)instream.readObject()) != null) {
outputField.append(tempCritter.toString());
index++;
System.out.println(index);
}//close while
instream.close();
} // Try in View
catch (Exception d) {
System.out.println(d.getMessage());
d.printStackTrace();
}
}// View Button
------------------------------------
And here is my Class Definition.
import java.io.Serializable;
public class CritterClass implements Serializable
{
// What is transient?
private static final long serialVersionUID = -7704164558093534885L;
private String _name;
private String _area;
private int _minRank;
private int _capRank;
private int _box;
private int _skin;
private int _biped;
private float _avgLoot;
public CritterClass(String name, String area, int minRank, int
capRank,
int box, int skin, int biped, float avgLoot)
{
_name = name ;
_area = area ;
_minRank = minRank;
_capRank = capRank;
_box = box;
_skin = skin;
_biped = biped;
_avgLoot = avgLoot;
}
public CritterClass() {
/*
_name = "";
_area = "";
_minRank = 0;
_capRank = 0;
_box = 0;
_skin = 0;
_biped = 0;
_avgLoot = 0;
*/
}
public String toString()
{
String result = " " + _name + " " + _area + "\t" + _minRank + "
" + _capRank + " " + _box + " " + _skin + " "
+ _biped + "\t" + _avgLoot + "\n";
return result ;
}
}
a Class and using just a File/Object reader/writer.
I have this snippet of code running when I click a View button, I want
the code to read in the Objects from a File and then output them into
a TextField on the Frame. The Code is Syntaxically correct, But when
it gets to the
"outputField.append(tempCritter.toString());" line.
It throws a null pointer exception. But The tempCritter cant be null
if it gets to that line.
Am I understanding Serializable wrong? Thanks in advance for any
advice.
Here is my Code for the ViewButton. (The Index prints are just to
'debug' to make sure they are being called and to find out where the
problem really is)
----------------------------
if (event.getActionCommand() == "View") {
try {
ObjectInputStream
instream = new ObjectInputStream( new
FileInputStream("CritterDB.rdb"));
int index = 0;
System.out.println(index);
CritterClass tempCritter = new CritterClass();
while((tempCritter = (CritterClass)instream.readObject()) != null) {
outputField.append(tempCritter.toString());
index++;
System.out.println(index);
}//close while
instream.close();
} // Try in View
catch (Exception d) {
System.out.println(d.getMessage());
d.printStackTrace();
}
}// View Button
------------------------------------
And here is my Class Definition.
import java.io.Serializable;
public class CritterClass implements Serializable
{
// What is transient?
private static final long serialVersionUID = -7704164558093534885L;
private String _name;
private String _area;
private int _minRank;
private int _capRank;
private int _box;
private int _skin;
private int _biped;
private float _avgLoot;
public CritterClass(String name, String area, int minRank, int
capRank,
int box, int skin, int biped, float avgLoot)
{
_name = name ;
_area = area ;
_minRank = minRank;
_capRank = capRank;
_box = box;
_skin = skin;
_biped = biped;
_avgLoot = avgLoot;
}
public CritterClass() {
/*
_name = "";
_area = "";
_minRank = 0;
_capRank = 0;
_box = 0;
_skin = 0;
_biped = 0;
_avgLoot = 0;
*/
}
public String toString()
{
String result = " " + _name + " " + _area + "\t" + _minRank + "
" + _capRank + " " + _box + " " + _skin + " "
+ _biped + "\t" + _avgLoot + "\n";
return result ;
}
}