0

I am trying to get a list of the longest words from each line in a block of text. I cant get the information to print from my main which is in processing.class. The information is processed in Tools.class. and there is also a Counters.class that is involved but it is immutable.

However, it gives me this warning: The value of the local variable longestWords is not used in my counter code, on the line where i declare longestWords =h;. Why is this, and how can I fix it?

This is my code:

(Processing)

longestWords = stat.getLongestWords();
    for(i = 0; i < longestWords.size(); i++){
        System.out.print(longestWords.get(i));
    }

Error thrown: Exception in thread "main" java.lang.NullPointerException at deol5210_a1.Processing.main(Processing.java:66)

which points to for(i = 0; i < longestWords.size(); i++){ (Tools)

}else {

            currentWord = lineScanner.next();

            if(currentWord.contains("$")){

                currentWord.replace("$", "");
                moneySpent += Double.parseDouble(currentWord.replace("$", ""));

            }else if (currentWord.length() > lineLongestWord.length()){
                lineLongestWord = currentWord;

            }

            if(currentWord.length() > longestWord.length()){

                longestWord = currentWord;
                lineLongestWord = currentWord;
                wordCount++;

            } else {

                wordCount++;

            }

(Counters)

    public ArrayList<String> getLongestWords(){
    return longestWords;
}

public Counters(int a, double b, int c, int d, double e, double f, String g, ArrayList<String> h){
    lineCount = a;
    avgWordCount = b;
    wordCount = c;
    purchaseCount = d;
    moneySpent = e;
    avgSpent = f;
    longestWord = g;
    ArrayList<String> longestWords = h;

}
  • What's your question? – shmosel Jan 25 '17 at 20:42
  • 1
    It's just a warning that you are never Using that variable. – Gatusko Jan 25 '17 at 20:43
  • I cant get the contents of the Array to print in my processing class. It gives me the nullpointerexception. i noticed i was printing it wrong. I changed the printing code to use longestWords.get(i) but now its throwing the NullPointerException. – Onkar Deol Jan 25 '17 at 20:49
  • Welcome to StackOverflow! When asking a question, please state your problem clearly. I've tried to edit to make the only question stated as clear as possible, but an edit of your own to make your intention known would be much appreciated. – AJF Jan 25 '17 at 20:53
  • Take a look at [MCVE], include the actual error, your code and the question. – pvg Jan 25 '17 at 20:55
  • In your Counters method you are declaring a brand new variable of type `ArrayList` that exists only within the scope of the Counters method. I'm guessing maybe that wasn't your intention? Right now the longestWords from this line: `longestWords = stat.getLongestWords();` has nothing to do with the longestWords from this line: `ArrayList longestWords = h;` because of variable scope. – Sam Hazleton Jan 25 '17 at 21:03
  • I realized that but my problem is I don't know how to get longestWords = stat.getLongestWords(); to get the data from longestWords = h;. I'm about to just make a string with \n to seperate each word lol... – Onkar Deol Jan 25 '17 at 21:08

3 Answers3

1

Initializing a variable is different from using it, the warning will disappear when you use the variable (print it, pass it to a method, etc').

Basically it's the same as saying:

int i = 0; 

and then not doing anything with i.

The statement

ArrayList<String> longestWords = h;

creates a new local variable longestWords and then does nothing with it, if you have a member called longestWords the statementshould be simply:

longestWords = h;
monkeyStix
  • 620
  • 5
  • 10
0

Another criticism, you never declare the variable i, but you try to assign it to a value. You need to change the following:

for(i = 0; i < longestWords.size(); i++) 

to

for(int i = 0; i < longestWords.size(); i++)

As for your warning, you are declaring longestWords within the block scope of your Counters constructor. This is most likely supposed to be an instance variable within Counters, not a private variable within the constructor.

Get rid of the declaration there, and instead use the variable that is most likely already defined in your class. From:

ArrayList<String> longestWords = h;

to

longestWords = h;
Brendan Lesniak
  • 2,271
  • 4
  • 24
  • 48
0

Java variables have something called "scope". A variable declared inside a method, like your longestWords variable, is a local variable. Its scope is restricted to that method. That means that outside of that method, the definition of longestWords as it is declared in the method does not exist. You are trying to access a local variable from outside the method in which it was declared. By definition, a local variable does not exist outside of the method in which it is declared.

If you want to use a variable in multiple methods, you need to declare it outside of any methods, making it a global variable.

If there is ever a local variable that is declared with the same name as a global variable, that variable name will refer to the local variable within the method, and to the global variable outside the method (because the local variable doesn't exist outside the method in which it was declared). So if you initialize that variable inside the method, it will not affect the value of the global variable.

Sam Hazleton
  • 470
  • 1
  • 5
  • 21