Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3282336
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:55:01+00:00 2026-05-17T19:55:01+00:00

I have a problem when setting a variable inside a Java class Here is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:55:02+00:00Added an answer on May 17, 2026 at 7:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with setting a starting position, that depends on the class
I have this weird problem with setting up cookies with PHP. Everything worked fine
I am having problem setting up tomcat context variable. I have tried: in web.xml
Suppose I have JS code inside a jsp files, such as this: <% String
I have problem with setting proper charset on my jsf pages. I use MySql
I have a problem with setting the HTTPBody of a request correctly. I'm using
I have a problem with setting Browsing Path in Delphi 2009: When I install
i have problem to correctly bind data to WPF Chart. When i'm setting ItemsSource
I have a weird problem where after setting nocheck on a foreign constraint and
I have a problem with the android alarmmanager. I'm setting the alarmmanager with two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.