NullPointerException

> NullPointerException:
  at Expand.fib(Expand.java:53) = sorter[counter] = (sorter[counter] + filename.charAt(i));
  at Expand.main(Expand.java:16) = sorter1 = fib("header.h");
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)

import java.io.*;
import java.util.*;
import java.lang.*;

public class Expand {
  private static String word;
  private static Scanner s;
  private static String holder;
  private static String a;
  private static final String pattern = "#include";
  private static String filename = "";
  private static String [] sorter;
  private static String [] sorter1;
 
  public static void main(String[] args) throws IOException {
    sorter1 = fib("header.h");
   /*
    * Recursion method here
    */
   

    System.out.println(sorter1);
   }
 
  public static String getToken() {
    word = s.next();
    word = word.replaceAll("[\\*/\"]", "");
    return word;
  }
 
  public static String [] fib(String file) throws IOException {
    s = new Scanner(new BufferedReader(new FileReader(args[0]+file)));
    while (s.hasNext()) {
      holder = holder + (a=getToken());
     
      if(a.matches(pattern)) {
        if(filename == null) {
          filename = getToken();
        }
       
        if(filename != null) {
          filename = filename + " " + getToken();
          //System.out.println(filename);
        }
      }
    }
    int counter = 0;
    for(int i = 0; i < filename.length(); i++) {
      if(filename.charAt(i) == ' '){
        counter++;
      }
      if(filename.charAt(i) != ' ') {
        sorter[counter] = (sorter[counter] + filename.charAt(i));
      }
    }
    s.close();
    return sorter;
  }
}
no158Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
if (counter < sorter.length && ............
0
CEHJCommented:
But first

sorter = new String[50];

(or whatever)
0
objectsCommented:
you don't initialise your variables, so there values are null.
So when they are accessed you get a NullPointerException
you also don't pass in your args
and your trying to display an array using println, which won't display that meaningfully

try this:

import java.io.*;
import java.util.*;
import java.lang.*;

public class Expand {
  private static String word;
  private static Scanner s;
  private static String holder;
  private static String a;
  private static final String pattern = "#include";
  private static String filename = "";
  private static String [] sorter;
  private static String [] sorter1;
 
  public static void main(String[] args) throws IOException {
    sorter1 = fib("header.h", args);
   /*
    * Recursion method here
    */
   

    System.out.println(Arrays.asList(sorter1));
   }
 
  public static String getToken() {
    word = s.next();
    word = word.replaceAll("[\\*/\"]", "");
    return word;
  }
 
  public static String [] fib(String file, String[] args) throws IOException {
    s = new Scanner(new BufferedReader(new FileReader(args[0]+file)));
    holder = "";
    while (s.hasNext()) {
      holder = holder + (a=getToken());
     
      if(a.matches(pattern)) {
        if(filename == null) {
          filename = getToken();
        }
       
        if(filename != null) {
          filename = filename + " " + getToken();
          //System.out.println(filename);
        }
      }
    }
    int counter = 0;
    sorter = new String[filename.length()];
    for(int i = 0; i < filename.length(); i++) {
      if(filename.charAt(i) == ' '){
        counter++;
      }
      if(filename.charAt(i) != ' ') {
        sorter[counter] = (sorter[counter] + filename.charAt(i));
      }
    }
    s.close();
    return sorter;
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

borislavmarkovCommented:
Hallo,
I think you are trying to do something illegal here.
In method Expand.fib(String) variable "args" is not defined(So basically this should not compile).

Can you describe what is the goal of this file, maybe I can think of better solution.
0
no158Author Commented:
Well you guys help me get on track but there seems to be a problem still with the output

the program will pull up the two file names, null is in the name, how do i get that out
nullfilename1
nullfilename2

I think it with this line of code that things are getting messed up
if(sorter1[i].charAt(i) != ' ') {

Since ' ' does not = null and you can't have null == char, i'm not sure what will work

Suggestions?
0
Manikandan ThiagarajanSenior consultantCommented:
if(sorter1[i].charAt(i) != '')


try this one
0
no158Author Commented:
Error: empty character literal
Error: unclosed character literal
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.