1

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 :)

5
  • If i runs beyond 24 (the last index in v) you will get this exception. What is the value of y in process()? Commented Feb 27, 2012 at 12:03
  • If v is not null, then a must be different between the setpackTotal and setpackType calls. Add System.out.println calls to see the values, or debug through the code. Commented Feb 27, 2012 at 12:03
  • After reformatting your code - the i++ statement should be inside the while block. But that doesn't explain the NPE, here we have an infinite loop. Commented Feb 27, 2012 at 12:09
  • BTW - as the code you've shown, doesn't compile, it's hard to tell, if the problem is inside this draft/fragment or in the real code that we haven't seen yet... Commented Feb 27, 2012 at 12:11
  • y is 4. i++ actually is inside the while block. The whole code is pretty long, I can't put it here, I think :/ but it's compiled. Commented Feb 27, 2012 at 13:01

3 Answers 3

2

Arrays in Java are zero indexed, try changing your i variable in your process method to begin from 0 instead:

int i = 0;
while ( ... ) { 
   ...
   i++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I changed it but there was not any different. But thank you for trying.
0

Ignoring that is missing a ) here:

setpackTotal(i, scanner.nextInt();

The NullPointerException can be caused because of the scanner.nextInt().

Try to debug the scanner to solve the problem.

Also, Peter's answer resolves part of the problem.

Comments

-1

You need to change setpackTotal() to:

public void setpackTotal(int a, int b) {
    v[a] = new Pack();
    v[a].total=b;
}

1 Comment

he is doing this: v[i]=new Pack();. it would override the current instance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.