Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Pointer Exception in JUnit test
    primarykey
    data
    text
    <p>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:</p> <pre><code>assertEquals(0.0, TweetClassification.tweetType[TweetClassification.SIGNIF_OTHER].likelihoodA); </code></pre> <p>The beginning of the TweetClassification class it's testing is as follows:</p> <pre><code>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]; ... </code></pre> <p>(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:</p> <pre><code>for (int i=0; i&lt;TCNUMBER; i++) { tweetType[i] = new TweetCategory(); } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload