0

The program looks slightly advanced; it is not. Simple manipulation of array. The program compiles correctly, however, it encounters an exception run-time.

Exception in thread "main" java.lang.NullPointerException
at Ordliste.leggTilOrd(Tekstanalyse.java:85)
at Tekstanalyse.main(Tekstanalyse.java:23)

So there is something wrong with if(s.equalsIgnoreCase(ordArray[k])). I cannot see why. It even provides the correct output.

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class Tekstanalyse {
    public static void main(String[] args) throws FileNotFoundException {

        Ordliste ol = new Ordliste();
        ol.lesBok("scarlet.text");
        ol.leggTilOrd("A");
    }
}

class Ordliste {
    private int i = 0;
    private String[] ordArray = new String[100000];
    private int antForekomster;
    private int arrStorrelse = 0;

    public void lesBok(String filnavn) throws FileNotFoundException {

        File minFil = new File(filnavn);
        Scanner scan = new Scanner(minFil);

        while (scan.hasNextLine()) {
            ordArray[i] = scan.nextLine();
            //System.out.println(ordArray[i]);
            i++;
            arrStorrelse++;
        }
        System.out.println("Array size: " + arrStorrelse + " Capacity: " + ordArray.length);
    }

    public void leggTilOrd(String s) {

        for (int k = 0; k < ordArray.length; k++) {
            if (s.equalsIgnoreCase(ordArray[k])) {
                antForekomster++;
                System.out.println("Den har vi sett for!");
            } else {
                s = ordArray[arrStorrelse];
            }
        }
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
p3ob2lem
  • 129
  • 2
  • 11
  • 1
    You can use any code formatting/indentation you want in your own code, but when asking other people for help, take the time to format the code reasonably. I've done so for you this time (and added two missing `}` from the end -- if they're not in your real code, use the "edit" link to remove them, but of course then you'll have a compilation error). – T.J. Crowder Oct 12 '14 at 11:39

4 Answers4

0

I'm pretty sure the error is right here:

for (int k = 0; k < ordArray.length; k++) {
    if (s.equalsIgnoreCase(ordArray[k])) {
        antForekomster++;
        System.out.println("Den har vi sett for!");
    } else {
        s = ordArray[arrStorrelse]; // <- dangerous
    }
}

As I said in the comment, ordArray could contain null elements if the read text file does not contain 100.000 lines of text. If this is the case, the above line would write null to s because s.equalsIgnoreCase(null) is false.

You should think about using a list instead of an array.

private List<String> ordList = new ArrayList<String>();

(or use a different variable name, it is up to you)

Then you can add new entries to the list using ordList.add(scan.nextLine());. This list won't contain any null elements and your mentioned problem should be gone.

Tom
  • 16,842
  • 17
  • 45
  • 54
0

I would highly recommend you to simply use a debugger to debug your code , but here goes: in your lesBok method you fill your array with strings and make a counter arrStorrelse. to be the amount of elements in the array you made. however the array is filled for indexes 0 to n-1. and arrStorrelse is equal to N you did however allocate space in the array for this. so when in leggTilOrd() you iterate the first time and you enter the else clause you do this

for (int k = 0; k < ordArray.length; k++) {
            if (s.equalsIgnoreCase(ordArray[k])) {

                antForekomster++;
                System.out.println("Den har vi sett for!");
            }

            else {
                int arrStorrelse2=arrStorrelse;
                s = ordArray[arrStorrelse];

            }

in that else clause s is set to ordArray[arrStorrElse]; however arrStorrElse is at that moment one higher than the last intialised element of your array. so it sets s to the null pointer. then the next iteration of your loop in the if clause

if (s.equalsIgnoreCase(ordArray[k])) {

                    antForekomster++;
                    System.out.println("Den har vi sett for!");

the s.equalsIgnoreCase() call is done on an s that is null that's where the null pointer exception comes from.

you need to change the arrStorrElse assignment you haven't explained what it should do so i can't do that for you also try to learn how to debug your code here is a usefull link:

http://www.tutorialspoint.com/eclipse/eclipse_debugging_program.htm

Liam Clark
  • 106
  • 1
  • 2
  • 8
0

I have compiled and tested your code.

s.equalsIgnoreCase(null)

throws nullPointerException.

Maybe you should try to use ArrayList instead of Array to avoid iterating through nulls.

  • This line does not throw a NPE if `s` is not null. – Tom Oct 12 '14 at 12:06
  • @Tom that is right, but in the given code `s` can be `null` (by the code given in the `else`-branch) – Stefan Freitag Oct 12 '14 at 12:36
  • @StefanFreitag I know that (see my answer). His posted code won't throw the exception like he said. His code implies that the null argument causes the NPE. – Tom Oct 12 '14 at 12:37
0

It took me some time to figure out why the NullPointerException occurs and I have to agree with Piotr and Tom: The NPE is caused by the line

s.equalsIgnoreCase(ordArray[k])

and a side-effect in your code. This side-effect is introduced by reassigning the parameter s in the else-branch to null (this value comes from ordArray[arrStorrelse]). After this reassignment happened, you will have something like this:

null.equalsIgnoreCase(ordArray[k])

And voila, there is the NullPointerException.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33