Reputation:
My code used to work, my object was saved properly into the array, but now out of nowhere it doesn't work, despite the fact that I have an identical piece of coding further down that does work. This is how it originally looks:
if (a == 2) {
ObjectOutputStream utström = new ObjectOutputStream
(new FileOutputStream("register.data"));
int n = 0;
while (true) {
j[n] = new nyHund();
String s1 = JOptionPane.showInputDialog("Hundens namn?");
if (s1 == null)
break;
j[n].namn = s1;
s1 = JOptionPane.showInputDialog("Hundens mor?");
j[n].mor = s1;
s1 = JOptionPane.showInputDialog("Hundens far?");
j[n].far = s1;
}
n++;
}
utström.writeObject(j);
utström.close();
}
When I put:
JOptionPane.showMessageDialog(null, j[n].namn + j[n].mor + j[n].far);
After "write.object" it just gives me "nullnullnull", but when i put it directly after
j[n].far = s1;
It works. So it just doesn't save properly. I have tried removing, adding, moving around braces and code, it just isn't working. I'm still suspecting it has something to do with the braces though. Here's the entire program. The reason why I have local input/output streams and things like that is attempts of solving this, declaring them in the beginning won't do anything. And yes it is very basic but this is like the first thing I'm doing in Java.
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Kennel2 {
public static void main(String[] args) throws IOException {
Object [] fönster = {"Avregistrera hund","Registrera intressent",
"Registrera hund"};
int a = JOptionPane.showOptionDialog(null, "Välkommen till Hellbergs Kennel!",
"Hellbergs Kennel", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, fönster, fönster[2]);
nyIntr[]i = new nyIntr[100];
nyHund[]j = new nyHund[100];
boolean ok = true;
if (a == 1) {
ObjectOutputStream utström = new ObjectOutputStream
(new FileOutputStream("register.data"));
int n = 0;
while (true) { //Registrera ny intressent
i[n] = new nyIntr();
String s1 = JOptionPane.showInputDialog("Intressentens namn?");
if (s1 == null)
break;
i[n].namn = s1;
s1 = JOptionPane.showInputDialog("Intressentens telefonnummer?");
i[n].tlfNr = s1;
s1 = JOptionPane.showInputDialog("Vilken valp är hen intresserad av?");
i[n].valp = new nyHund();
if (i[n].valp.aktiv = true) {
j[n].intressent.namn = i[n].namn;
j[n].namn = i[n].valp.namn;
}
else {
JOptionPane.showMessageDialog(null, "Denna hund är ej tillgänglig");
break;
}
n++;
}
JOptionPane.showMessageDialog(null, i[n].namn + i[n].tlfNr);
utström.writeObject(i);
utström.close();
}
if (a == 0) {
ObjectInputStream inström = new ObjectInputStream
(new FileInputStream("register.data"));
ObjectOutputStream utström = new ObjectOutputStream
(new FileOutputStream("register.data"));
Scanner data = new Scanner (new File("register.data"));
int n = 0;
while (data.hasNext()) {
j[n] = new nyHund();
j[n].namn = data.next();
n++;
}
while (true) {
String sökt = JOptionPane.showInputDialog("Vilken hund söker du?");
if (sökt == null)
break;
int k = 0;
for (; k<n; k++)
if (j[n].namn.equals(sökt))
break;
if (k < n) {
j[n].aktiv = false;
JOptionPane.showMessageDialog(null, "Hunden är avaktiverad"); }
else {
JOptionPane.showMessageDialog(null, "Hunden kunde ej hittas");
break;
}
utström.writeObject(j);
data.close();
inström.close();
utström.close();
}
}
if (a == 2) {
ObjectOutputStream utström = new ObjectOutputStream
(new FileOutputStream("register.data"));
int n = 0;
while (true) { //Registrera ny hund
String s1 = JOptionPane.showInputDialog("Hundens namn?");
if (s1 == null)
break;
j[n] = new nyHund();
j[n].namn = s1;
s1 = JOptionPane.showInputDialog("Hundens mor?");
j[n].mor = s1;
s1 = JOptionPane.showInputDialog("Hundens far?");
j[n].far = s1;
s1 = JOptionPane.showInputDialog("Hundens födelseår?");
for (int k=1; k<s1.length(); k++)
if (s1.charAt(k) < '0' || s1.charAt(k) > '9'){
ok = false;
JOptionPane.showMessageDialog(null, "Ogiltigt format");
break;
}
else {
int l = Integer.parseInt(s1);
j[n].födelsedatum = l;
n++;
}
}
utström.writeObject(j);
utström.close();
}
}
}
Upvotes: 0
Views: 370
Reputation: 9946
Because your code is not correctly implemented
This is your while loop and in it you are not incrementing n
while (true) {
j[n] = new nyHund();
String s1 = JOptionPane.showInputDialog("Hundens namn?");
if (s1 == null)
break;
j[n].namn = s1;
s1 = JOptionPane.showInputDialog("Hundens mor?");
j[n].mor = s1;
s1 = JOptionPane.showInputDialog("Hundens far?");
j[n].far = s1;
}
So all the values you are putting are at 0
index and each time it gets replaced with new object.
After while loop is broken you increment it to 1 by n++
and at index 1
you do not have any values hence you are seeing null.
To correct put the increment n++
in your while loop and correct placement of new nyHund()
.
while (true) {
String s1 = JOptionPane.showInputDialog("Hundens namn?");
if (s1 == null)
break;
j[n] = new nyHund();
j[n].namn = s1;
s1 = JOptionPane.showInputDialog("Hundens mor?");
j[n].mor = s1;
s1 = JOptionPane.showInputDialog("Hundens far?");
j[n].far = s1;
n++;
}
And for printing the values use correct index.
Hope it helps.
Upvotes: 1
Reputation: 1759
You've iterated n
past the last element in your array in the last loop call. If your array contains 60 elements (0-59), after the loop ends, n = 60
, and you're not going to get meaningful data (since you do n++
after setting the data for j[n]
).
Either add n--
after the loop finishes, or iterate n
only when creating a new element.
Upvotes: 0