• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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;
  }
}
0
no158
Asked:
no158
3 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now