views:

74

answers:

5

Hi guys, I have a problem when setting a variable inside a Java class

Here is my code

This is where I create the instances (IdeaInfo is a class that acts similar to a Struct):

IdeaInfo[] IDEAS = new IdeaInfo[100];
String[] TITLES = new String[100];

This is the function that will use those instances:

    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
            // This is adding title to array Ideas and Titles
            if(mode % 3 == 0)   {
                IDEAS[ideas_pos].setTitle(sb.toString());
                TITLES[titles_pos] = sb.toString();
                titles_pos++;
                mode++;
            }
            // This is adding the content to array Ideas
            else if(mode % 3 == 1)  {
                IDEAS[ideas_pos].mContent = sb.toString();
                mode++;
            }
            // This is adding the rating to array Ideas
            else if(mode % 3 == 2)  {
                IDEAS[ideas_pos].mRating = Float.valueOf(sb.toString().trim()).floatValue();
                ideas_pos++;
                mode++;
            }
        }
    }

This is what I have inside IdeaInfo class:

public class IdeaInfo {

    public String mTitle = new String();        // Store the Idea's title
    public String mContent = new String();  // Store the Idea's title
    public float mRating;       // Store the Idea's Rating

    /*
     * Function that set the Idea's title
     */
    public void setTitle(String temp){
      mTitle = temp;
    }
}

Apparently, the error occurred inside the try, exactly at IDEAS[ideas_pos].setTitle(sb.toString()); The debugger indicated that I am accessing a NullPointerException, this does not really make any sense to me since I already initialize those variables in the class.

By the way, I initialized ideas_pos to 0.

+7  A: 

When you initialize an array, it doesn't mean you've initialized its members.

IDEAS[x] is null. You'd need to initialize it by:

IDEAS[ideas_pos] = new IdeaInfo();
Bozho
+2  A: 

It looks like the items in the list are null. Try appending:

if(IDEAS[ideas_pos] == null) {
    IDEAS[ideas_pos] = new IdeaInfo();
}

Same would apply for titles.

Jared
+3  A: 

I hate to say it but your code should really be re-designed and re-written.

  1. For start sb.append(text + "/n") is a complete misuse of StringBuilder.Use: sb.append(text).append('\n').
  2. You never clear or create a new StringBuilder, when you'll reach the Float.valueOf() part your builder will have a lot of text appended to it.
  3. I suggest creating IdeaInfo on the 0 switch part and store in the array so you can later reference its values
  4. IdeaInfo - don't create empty strings (prefer to simply assign "").
Bivas
A: 

Sammm

  1. As above, you need to create IdeaInfo instances for each array "slot".

  2. [Improving the code] Make all the data fields private - provide public accessors (or better yet, methods that act properly on the class - get/set methods are EVIL!). Encapsulation is the number 1 golden rule in OO.

    • Ask yourself what happens when you need to change the implementation of IdeaInfo in 6 months time and you have lots of other code directly depending upon (changing) the member variable state - you have a maintenance headache!
    • Encapsulating data makes reasoning about any class easier - you know how the state is changed (via methods only) - the class is easier to get right.
  3. Initialise the string data fields to "", not new String() which is wasting memory (all empty strings will reference a single String object).

DecaniBass
+1  A: 

Arrays in java are initialized "clean", that is, with all elements set to null or "zero" (whatever is appropriate for the type of array). When you write

IdeaInfo[] IDEAS = new IdeaInfo[100];

the JVM will treat it as if you wrote

IdeaInfo[] IDEAS = new IdeaInfo[100];
for (int i = 0; i < 100; i++) {
    IDEAS[i] = null;
}

This takes some getting used to if you are coming to Java from a language like C or C++ which has different conventions for initializing arrays.

socket puppet