0

I know that NullPointerExceptions are something you get when trying to give 0 properties it doesn't have. But why do I get a nullpointerexception here? It says it is supposed to be on this part of code:

    class Oblig3A{
    public static void main(String[]args){

    OrdAnalyse oa = new OrdAnalyse();
    String filArgs=args[0];
    oa.analyseMetode(filArgs);
    }
}

class OrdAnalyse{
    void analyseMetode(String filArgs){

    //Begynner med aa opprette alle variabler som trengs, disse deklareres rett under. De ligger her oppe bare for at jeg skal ha oversikten over de.
    Scanner input, innfil;
    String[] ord, fortelling;
    int[] antall;
    int antUnikeOrd;
    PrintWriter utfil;

    //Deklarerer alle bortsett fra de som har med fil aa gjore, disse deklareres inne i en try-catch-loop (printwriter utfil og scanner innfil).
    input=new Scanner(System.in);
    ord=new String[5000];
    antall=new int[5000];
    antUnikeOrd=0;

    try{
        innfil=new Scanner(new File(filArgs));
        //Naa skal jeg dele opp prosessen litt for aa faa inn funksjonaliteten for aa for eksempel sette alle ord til lowercase.

        while(innfil.hasNext()){
        fortelling=innfil.nextLine().toLowerCase().split(" ");
            for(int i=0; i<fortelling.length; i++){
               for(int j=0; j<5000; j++){
               if(fortelling[i].equals(ord[j])){
                   antall[j]+=1;
               }else if(!ord[j].equals(fortelling[i])){ //This is line 39
                   ord[j]=fortelling[i];
                   antall[j]+=1;
                   antUnikeOrd+=1;
               }
               System.out.print(fortelling[i]);
               System.out.print(fortelling.length);
               }
           }
           }
       }catch(Exception e){
           e.printStackTrace();
       }
    }
 }

The stacktrace:

java.lang.NullPointerException
    at OrdAnalyse.analyseMetode(Oblig3A.java:39)
    at Oblig3A.main(Oblig3A.java:9)

I have confirmed now that it is indeed the line in question, though

Makri
  • 331
  • 2
  • 4
  • 15

2 Answers2

0

Assuming innfil is a Scanner:

Try innfil.hasNextLine() instead of innfil.hasNext().

0

You create a String array here

ord=new String[5000];

But never initialise any of the strings in the array.

So this line

}else if(!ord[j].equals(fortelling[i])){

Will throw the NPE if the condition fortelling[i].equals(ord[j]) fails, which it will as its asking 'Does fortelling[i] equal null' Which it can't or it is the culprit throwing the NPE


You could change you if statement to this

if(fortelling[i].equals(ord[j]))
      antall[j]+=1;
else   // remove your if statement here
{
      ord[j]=fortelling[i];
      antall[j]+=1;
      antUnikeOrd+=1;
}

As if this fortelling[i].equals(ord[j] is false is the same as saying this if(!ord[j].equals(fortelling[i])

Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • What about the if statement associated with that else if? `if(fortelling[i].equals(ord[j])){` also accesses `ord`. – Justin Oct 17 '13 at 00:49
  • You can call `String.equals(null)`, it just returns false – Java Devil Oct 17 '13 at 00:50
  • Damnit. How can I accomplish the task of reading one word from a file, check whether it exists in the ord-array and if it does: add +1 to antall-array at the same index as the word in ord-array is located at, and if it doesn't exist: add it to the end of the array? – Makri Oct 17 '13 at 00:53
  • You pretty much have the correct Code there to do that you just need to populate the array `ord` with the words you are comparing with first. – Java Devil Oct 17 '13 at 00:56
  • @Makri Do you have to use arrays? It seems like `Map` would be easier here. Also if you want to read each word from file then use `next` method, instead reading entire line and splitting it. – Pshemo Oct 17 '13 at 00:56
  • I do? Okay, nice. hmmm, not sure if I get it. My thought was to populate the fortelling-array, and then use it to check whether the words was in the ord-array or not. If not, add it to the first available spot. So I must populate the ord-array as well then? Yes, I do :/ I am not allowed to use HashMaps, even though it would have been easier.. :P – Makri Oct 17 '13 at 00:59
  • @Makri See my edit, your second if condition is redundant anyway – Java Devil Oct 17 '13 at 01:00
  • I think it worked! It kinda worked. Doesn't seem like it is stopping though. the array ord is 5000 constant and the counter for number of unique words is currently at 100.000. Something's wrong O.o So the while-loop is infinite :P – Makri Oct 17 '13 at 01:02
  • @Makri So this has at least solved your question about the NullPointerException, That suggests that there is something else wrong. But its one step closer. – Java Devil Oct 17 '13 at 01:12
  • Yeah, that is true. One step at a time. You will get correct answer for this one, though ;) – Makri Oct 17 '13 at 01:17