Link to home
Start Free TrialLog in
Avatar of no158
no158

asked on

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;
  }
}
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of borislavmarkov
borislavmarkov

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.
Avatar of no158

ASKER

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?
if(sorter1[i].charAt(i) != '')


try this one
Avatar of no158

ASKER

Error: empty character literal
Error: unclosed character literal
:-)