1

Whenever I try to test my code with JUnit, I receive a NullPointerException - even though I don't get that exception when I run the actual program. The line that gives me the exception is:

assertEquals(0.0, TweetClassification.tweetType[TweetClassification.SIGNIF_OTHER].likelihoodA);

The beginning of the TweetClassification class it's testing is as follows:

public class TweetClassification
{

// CONSTANTS =============================================
public static final int TCNUMBER = 5; // number of TweetCategories (including the null category)

// using constants to indicate the names of the TweetCategories, so that they could be renumbered 
public static final int NULLTWEET = 0;
public static final int SIGNIF_OTHER = 1;
public static final int FRIENDS = 2;
public static final int WORK = 3;
public static final int FOOD = 4;

public static final TweetCategory[] tweetType = new TweetCategory[TCNUMBER];
...

(TweetCategory is another class that is defined separately within the package.) So I realize that this code initializes the array but not its members, and that's probably why I'm getting the exception(?) But the thing is, I do initialize the members of the array within the main method of TweetClassification, as follows:

for (int i=0; i<TCNUMBER; i++)
{
tweetType[i] = new TweetCategory();
}

But if I try to move this for loop outside the main method with the constants I get a syntax error - I presume you're not supposed to use a for loop outside of a method. So I'm not sure how to initialize the class properly for JUnit to work - either I do it outside the main method and get a syntax error, or I do it inside the main method and get a NullPointerException. Any ideas?

1
  • Please show the code inside the main method too. Commented Mar 24, 2012 at 14:30

2 Answers 2

5

You need to move the init code into a static initializer block, like this:

public class TweetClassification
{
  //...

  public static final TweetCategory[] tweetType = new TweetCategory[TCNUMBER];

  static
  {
    for (int i=0; i<TCNUMBER; i++)
    {
      tweetType[i] = new TweetCategory();
    }
  }

  //...
}

This ensures that the static variable is initialized properly when the class is loaded (i.e. before it is first used anywhere within your program or tests).

Sign up to request clarification or add additional context in comments.

Comments

3

Yo might find some use of the static initialzier block:

private static Integer arr[] = new Integer[2];
static {
    for (int i = 0; i < 2; i++) {
        arr[i] = 2;
    }
}
public static void main(String[] args) {
    System.out.println(arr[1]);
}

Ouputs:

2

This is proper java and is meant exactly for initializing static variables, though it is not very commonly used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.