I'm about to make a class about parsing, here's part of my code.
public class Parsing {
//some other atributes here
public class Pack {
String type;
int[] brand;
int total;
}
Pack[] v = new Pack[25];
public void setpackType(int a, String b) {
v[a].type = b;
}
public String getpackType(int a) {
return v[a].type;
}
public int getpackTotal(int a) {
return v[a].total;
}
public void setpackTotal(int a, int b) {
v[a].total = b;
}
public void setpackBrand(int a, int b, int c) {
v[a].brand[b] = c;
}
and
public final void process(String s) throws FileNotFoundException {
Scanner scanner;
scanner = new Scanner(new File(s));
try {
if (scanner.hasNext()) {
int y = scanner.nextInt();
int i = 1;
while (i <= y) {
v[i] = new Pack();
setpackType(i, scanner.next());
setpackTotal(i, scanner.nextInt();
int k = 0;
while (k < hh) {
setpackBrand(i, k, scanner.nextInt());
k++;
}
i++;
}
}
} finally{
scanner.close();
}
}
}
It's compiled with no error but when I tried to run, I got this:
Exception in thread "main" java.lang.NullPointerException
at Parsing.setpackTotal(Parsing.java:112)
at Parsing.process(Parsing.java:153)
at Parsing.main(Parsing.java:202)
I already tested it line by line. setpackType works just fine!
But I don't understand why setpackTotal AND setpackBrand can't work.
Thank you so much for help :)
iruns beyond 24 (the last index inv) you will get this exception. What is the value ofyinprocess()?vis not null, thenamust be different between thesetpackTotalandsetpackTypecalls. AddSystem.out.printlncalls to see the values, or debug through the code.i++statement should be inside the while block. But that doesn't explain the NPE, here we have an infinite loop.